The basic problem is this: "I have this number x and I ask you to give me another number y. If the number you give me is some percentage c different than my number then I do not want it." Given that you will know x and c, how do you calculate whether or not I should take y?
The naive approach I came up with is to just divide y / x < c but this fails for obvious reason (try y bigger than x).
The next approach I is that the percentage difference is really just a ratio of the smaller number divided by the larger number. So thereforce we could try min(x, y) / max(x, y) < c. However this does not work, here is an example:
x = 1.2129 y = 1.81935 c = 50%
If we do the above we get 1.2129 / 1.81935 = 0.67 which is greater than 0.50. The problem here is that I obtained y by multiplying 1.2129 by 1.5, therefore y is only 50% greater than x. Why? I still don't understand why the above formula doesn't work.
Eventually through some googling I stumbled accross the percentage difference formula but even this doesn't suit my needs. It is abs(x - y) / ((x + y) / 2). However, this does not yield the result I am looking for. abs(x - y) = abs(1.2129 - 1.81935 ) = 0.60645. (x + y) / 2 = 3.03225 / 2 = 1.516125 0.60645 / 1.516125 = 0.4
Eventually I ended up writing some code to evaluate x * c < y < x * (1 + c). As the basic idea is that we don't want any y that is 50% less than my number, nor do we want any number that is 50% greater than my number.
Could someone please help me identify what I'm missing here? It seems like there ought to be another way that you can calculate the percentage difference of two arbitrary numbers and then compare it to c.