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.


What you're missing is what you want. The difference between your two numbers is clearly $|x-y|$, but the "percentage" depends on how you want to write $|x-y|/denominator$. You could choose for a denominator $|x|$, $|x+y|$, $\max \{x,y\}$, $\sqrt{x^2 + y^2}$, for all I care, it's just a question of choice. Personally I'd rather use $|x|$ as a denominator, but that's just because I think it'll fit for this problem ; if this is not the solution to your problem, then choose something else.

That is because when you say that you want the difference between $x$ and $y$ to be $c$% or less than your number $x$, for me it means that

$$ |x-y| < \frac c{100} |x| \qquad \Longleftrightarrow \qquad \frac{|x-y|}{|x|} < \frac{c}{100} $$ so that choosing $|x|$ as a denominator makes most sense.

Hope that helps,


As far as I understood, you want the difference between two numbers (x-y) to be c(or less)% different than your number (x).

When you use min(x, y) / max(x, y) < c, you are calculating whether minimum of two numbers is c(or less)% smaller than maximum of them.

abs(x - y) / ((x + y) / 2) calculates whether the difference between two numbers (x-y) is c(or less)% different than average of those two numbers.

Those are completely different problems.

For yours, abs(x-y)/x < c should work.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.