## All is good [Software]

Hi Ohlbe,

» … the same happens in :

So you installed this goody at last?

»

0==(0.5-0.4-0.1)
» [1] FALSE
» 0==(-0.4-0.1+0.5)
» [1] TRUE

» Are they first calculating -0.4-0.1=-0.5, which is nicely binarily coded, then -0.5+0.5=0 ?

Would have to dive into the lexer of the source of base-. Likely not clever enough anyway. If you want to explore the matter, type ?Syntax, ?Arithmetic, ?Comparison, and their relatives. See also the FAQ 7.31. However, the == operator should not be used to compare two objects for identity. Otherwise we fall into the binary trap:

x1 <- (0.5-0.4-0.1) x2 <- (-0.4-0.1+0.5) x1; x2 [1] -2.775558e-17 [1] 0 x1 == x2 [1] FALSE identical(x1, x2) [1] FALSE all.equal(x1, x2) [1] TRUE

Only the last function compares to the numeric precision of the machine* and this is what we want and the best we can hope for…

• On my machine with 64bit  …
sqrt(.Machine$double.eps) [1] 1.490116e-08 … which is orders of magnitude larger than abs(-2.775558e-17) – which is just “noise”. Hence, the test passes. x1 <- (0.5-0.4-0.1) x2 <- (-0.4-0.1+0.5) sqrt(.Machine$double.eps) > abs(x1 - x2) [1] TRUE
That’s a crude example. The source is more tricky. Try getAnywhere(all.equal.numeric) to see the 77 lines of source code fo comparing numbers.

Dif-tor heh smusma 🖖
Helmut Schütz

The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes