# Bug in floating-point number comparisons near $MachineEpsilon? I stumbled over the following weird behavior when comparing floating-point numbers close to 1.0: qq = 1.0 + 60.0$MachineEpsilon;


and then

qq > 1.0


yields False. However,

qq - 1


yields

1.33227*10^-14


which is far above $MachineEpsilon = 2.22045*10^-16 (the smallest number eps satisfying (1.0 + eps > 1.0)==True when using 8-byte floating point numbers). Changing the first command to qq = 1.0 + 65.0$MachineEpsilon;


Leads to the expected behavior of (qq > 1.0) == True.

I'm running Mathematica 10.4.0.0 under Windows on an Intel i5.

Ron

• SetPrecision[qq, MachinePrecision] > 1 Dec 6, 2016 at 11:54
• @Feyre Still False. Dec 6, 2016 at 12:02
• @corey979 Sorry, should be 16 or higher, MachinePrecision only works with a qq rationalized as qq = 1 + 60 $MachineEpsilon, something I did without thought, first idea with precision issues is always using rational numbers. Dec 6, 2016 at 12:06 • This comes about because Equal is fuzzy and mutually exclusive trichotomy is enforced. Dec 6, 2016 at 14:47 • I think this "feature" deserves at least a comment in the documentation of$MachineEpsilon. There's a good section in the Help text to Equal[] under "Possible Issues".
– RonH
Dec 6, 2016 at 15:50

As said in a comment:

This comes about because Equal is fuzzy and mutually exclusive trichotomy is enforced. – Daniel Lichtblau Dec 6 '16 at 14:47

Perhaps this question also deserves some suggestions for workarounds. The tolerance is relative, so comparing with 0 effectively compares with no tolerance.

qq - 1.0 > 0
(*  True  *)


The amount of tolerance is controlled by Internal$EqualTolerance, which can be set to zero. (For a related Q&A, see Machine Epsilon). Block[{Internal$EqualTolerance = 0}, qq > 1.0]
(*  True  *)

• Could you expand a bit on mutually exclusive trichotomy please? Apr 4, 2018 at 1:21
• @anderstood The Trichotomy Law states that only one of $a < b$, $a = b$, and $a > b$ can be true. I assume that's what Daniel means. Apr 4, 2018 at 1:33
• So could the problem come from the fact that qq==1. returns True? Is qq - 1. == 0 $\neq$ qq == 1. and expected behaviour (I understand that it's a consequence from the relative tolerance). Apr 4, 2018 at 2:40
• @anderstood Yes, that's why Daniel singles out Equal. Apr 4, 2018 at 2:42
• Yes to what I meant by trichotomy and yes to the expected behavior and rationale. Apr 4, 2018 at 15:54