On Tuesday, November 4, 2014 8:42:02 PM UTC-5, K leo wrote:
>
> I meant this: to check whether 2 floating point valuables equal I had 
> always had to do something like abs(x-y)<1.e-5 (never simply x==y) in 
> other languages.  I wonder whether checking x==y would be sufficient in 
> Julia? 


It depends on what you want.   A quick floating-point tutorial seems to be 
in order here:

Each floating-point value is not a real number with "uncertainty" or 
"random fuzz" or an "interval".   It is just *a rational number.*  The 
floating-point numbers F are a specific subset of the rationals (plus 
oddballs like ∞ and NaN).   *==* simply compares whether two floating-point 
values are the same rational number.  If that is what you want, great!

However, because F is only a *subset* of the rationals, various operations 
incur a roundoff error.   This includes conversion from a string to a 
floating-point value (for binary floating point, F does not include decimal 
fractions like 0.1, so they are rounded to the nearest value in F, in this 
case to 0.100000000000000005551115....).  It also includes operations like 
x + y: if the result is in F, it is *exact*, but otherwise the results of 
binary operations +/–/*/÷ are rounded to the nearest element of F (this is 
*exact 
rounding*).    And when you do many such operations (e.g. factorizing a 
matrix), the roundoff errors typically *accumulate*, so the final result 
can be very far from the exactly rounded answer.

(A disturbing number of people think that x * 0 != 0 in floating point 
[ignoring Inf/NaN], or that 1.0 + 1.0 != 2.0 in floating point.  Since 
these quantities are represented exactly in F, the computations are exact.)

As a result, if you are comparing two floating point numbers that are the 
result of long calculations, which would be exactly equal in exact 
arithmetic, you typically check whether the relative difference is small: 
|x–y| < ε|x|, where ε is some estimate of the relative accuracy you hope to 
attain.

--SGJ

Reply via email to