4

I try to wrap my head around machine precision calculations in Mathematica (11.2, Linux). I do not understand the following behavior:

$MachineEpsilon
2.22045*10^-16

$MachinePrecision
15.9546

2^-46 // N
1.42109*10^-14

N[1 + 2^-46, $MachinePrecision] == 1
True

Why does this statement still evaluate to True? It is so far away from $MachineEpsilon that it should evaluate to False?

Furthermore, why does the following yield a result which is exactly equal to a mantissa of just 46bit? I guess its the same answer and the same question as above.

one = 1;
eps = 1;
SetPrecision[one, prec];
SetPrecision[eps, prec];
For[
 SetPrecision[eps = one, MachinePrecision],
 SetPrecision[one + eps, MachinePrecision] != 1,
SetPrecision[eps = eps/2, MachinePrecision]
]
eps

1/70368744177664

What I try to understand is what rules Mathematica applies that lead to such results. My problem is that currently I have the impression that I can't rely on SetPrecision[] etc.

1 Answers1

2

This is expected. From the documentation of Equal (==):

Approximate numbers with machine precision or higher are considered equal if they differ in at most their last seven binary digits (roughly their last two decimal digits).

(point 4 under Details)

You can use SameQ (===) (note the . after the 1 to make it a machine precision number):

N[1 + 2^-46, $MachinePrecision] === 1.
(* False *)

N[1 + 2^-52, $MachinePrecision] === 1.
(* True *)

You can also look into Internal`$EqualTolerance as pointed out in the comments. See the linked post for more information.

Lukas Lang
  • 33,963
  • 1
  • 51
  • 97