6
$\begingroup$

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

$\endgroup$
  • $\begingroup$ SetPrecision[qq, MachinePrecision] > 1 $\endgroup$ – Feyre Dec 6 '16 at 11:54
  • $\begingroup$ @Feyre Still False. $\endgroup$ – corey979 Dec 6 '16 at 12:02
  • $\begingroup$ @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. $\endgroup$ – Feyre Dec 6 '16 at 12:06
  • 3
    $\begingroup$ This comes about because Equal is fuzzy and mutually exclusive trichotomy is enforced. $\endgroup$ – Daniel Lichtblau Dec 6 '16 at 14:47
  • 1
    $\begingroup$ 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". $\endgroup$ – RonH Dec 6 '16 at 15:50
5
$\begingroup$

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  *)
$\endgroup$
  • $\begingroup$ Could you expand a bit on mutually exclusive trichotomy please? $\endgroup$ – anderstood Apr 4 '18 at 1:21
  • 2
    $\begingroup$ @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. $\endgroup$ – Michael E2 Apr 4 '18 at 1:33
  • 1
    $\begingroup$ 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). $\endgroup$ – anderstood Apr 4 '18 at 2:40
  • $\begingroup$ @anderstood Yes, that's why Daniel singles out Equal. $\endgroup$ – Michael E2 Apr 4 '18 at 2:42
  • 1
    $\begingroup$ Yes to what I meant by trichotomy and yes to the expected behavior and rationale. $\endgroup$ – Daniel Lichtblau Apr 4 '18 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.