Correct comparison of real number for n digits precision (absolute vs relative difference)

462 Views Asked by At

To compare if $2$ real numbers are equal, we define a desirable precision e.g. $n$ digits and then check if the following condition holds: $-\frac{1}{10^n} \lt x - y \lt \frac{1}{10^n}$
Now I was reading a piece of code that does the following (I will translate it in pseudo code, the original is in Java):

function compare (x, y)  {  
  epsilon = 0.000001  

  diff = (x - y) / max(|x|, |y|)   

  if(diff < -epsilon) // comment that this means x is smaller smaller  
  else if(diff > epsilon) // comment that this x is larger
  else // comment that they are equal    
}    

So it is the application of the aforementioned condition (where epsilon is $\frac{1}{10^6}$ i.e. precision up to $6$ digits).

But I don't understand this line:

  diff = (x - y) / max(|x|, |y|),   

Is there any benefit to divide the $(x - y)$ by the max absolute value of the $2$ before doing the comparison instead of just doing the direct comparison?
It seems some kind of normalization but I don't understand what is the benefit

Update
After the comments I can see that there is an absolute/relative difference approach when comparing real numbers.
It seems to me that relative difference is always correct/better than absolute. Is there a case when it is better to use absolute difference?

1

There are 1 best solutions below

5
On BEST ANSWER

We have two usual ways of comparing numbers. We either compute the relative error or the absolute error. The absolute error between $x$ and $y$ is $|x-y|$. The relative error is this quantity divided by $x,y,$ or something like that. Your routine uses $\max (|x|,|y|)$, which is a very reasonable choice. If $x$ and $y$ are close, it doesn't matter much what you choose.

The advantage of absolute error is that it plays well with addition. If I add up four numbers that each have a maximum absolute error of $\frac 18$ the sum has a maximum error of $\frac 12$. You can think of trying to pack four boxes across the trunk of your car. If you wonder whether they will fit, measure each one with a certain absolute error. You know the error of the sum is no more than four times that.

Relative error is what you get when you say something is accurate to one part in a hundred, thousand, or million. Relative error has two advantages. One is that it plays well with multiplication and division. If you multiply four numbers that are each good to one part in a thousand, the product is good (almost) to four parts in a thousand. The almost comes from second order terms that are small if the individual measurements are rather accurate. The other advantage is that it doesn't care about the scale of the numbers. If I measure a length as 1000.0mm, the maximum absolute error is $0.05mm$. If I measure in meters, it becomes $1.0000m$ and the maximum absolute error is $0.00005m$. It seems strange that changing the unit of measure, when I have no idea what a reasonable size is, should change the error. Both of these have a relative error of $5\cdot 10^{-5}$, which seems more reasonable. If you blindly ask for absolute error of no more than $10^{-6}$, which seems pretty good, you would accept $10^{-6}$ as a square root of $10^{-100}$ because it is within $10^{-6}$ of the true answer $10^{-50}$.