As rasher and the documentation both say, Equal
has a certain level of fuzziness. The same is true of SameQ
, though it has a more stringent tolerance. The following computations are all done with machine precision numbers. Similar things should hold with arbitrary precision numbers but the analysis might be trickier.
(* 12 zeros, difference = 1.00142*10^(-13) *)
1.0000000000001 == 1.0000000000002
(* Out: False *)
(* 13 zeros, difference = 9.992*10^(-15) *)
1.00000000000001 == 1.00000000000002
(* Out: True *)
(* Same as last with SameQ *)
1.00000000000001 === 1.00000000000002
(*Out: False *)
(* 15 zeros, difference = 2.22045*10^(-16) *)
1.0000000000000001 === 1.0000000000000002
(* Out: True *)
It might also be worth mentioning that more traditional floating point comparisons can be easily emulated. For example, since the "fuzziness" is based on Precision
, we can check if the difference is equal to zero.
x = 0.2 + (0.3 + 0.1);
y = (0.2 + 0.3) + 0.1;
x == y
x - y == 0.0
(* Out1: True *)
(* Out2: False *)
Certain compiler switches also make comparisons more C-like.
test1 = Compile[{}, 0.2 + (0.3 + 0.1) == (0.2 + 0.3) + 0.1];
test2 = Compile[{}, 0.2 + (0.3 + 0.1) == (0.2 + 0.3) + 0.1,
RuntimeOptions -> "Speed"];
test1[]
test2[]
(* Out1: True *)
(* Out2: False *)