TransWikia.com

Why am I getting that $0.999999999999988 geq 1.0$ is True?

Mathematica Asked on May 30, 2021

I am getting these results:

0.999999999999988 < 1.0  (*False*)
0.999999999999988 >=  1.0  (*True*)
0.999999999999988 ===  1.0  (*False*)

Block[{$MinPrecision = $MachinePrecision, $MaxPrecision=$MachinePrecision}, 0.999999999999988 >=  1.0] (*False*)


N[0.999999999999988 -  1.0]  (*small negative number, but larger in magnitude than machine precision*)

PossibleZeroQ[N[0.999999999999988 -  1.0]]  (*False*)

Is there a workaround?

3 Answers

You can lower the value of Internal`$EqualTolerance:

Block[{Internal`$EqualTolerance = 0},
  0.999999999999988 >= 1.0 
]
False

This can lead to unexpected behaviors too:

Block[{Internal`$EqualTolerance = 0},
  0.1 + 0.2 == 0.3 
]
False

Maybe there's a better sweet spot that fits your needs. For these two examples, this works:

Block[{Internal`$EqualTolerance = Internal`$SameQTolerance},
  0.999999999999988 >= 1.0 
]
False
Block[{Internal`$EqualTolerance = Internal`$SameQTolerance},
  0.1 + 0.2 == 0.3 
]
True

If you have a nice representative sample of values you're comparing, you can estimate a value for Internal`$EqualTolerance by plotting. These two examples return correct comparisons for values between Log10[5/3] and Log10[108]:

correctEquals[x_?NumericQ] := 
  Block[{Internal`$EqualTolerance = x}, 
    Boole[Not[0.999999999999988 >= 1.0] && (0.1 + 0.2 == 0.3)]
  ]

Plot[correctEquals[x], {x, 0, Internal`$EqualTolerance}]

Correct answer by Chip Hurst on May 30, 2021

From the help.

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).

This explains the fist two cases: 0.999999999999988 and 1. differ by less than 7 digits.

SameQ (===): SameQ requires exact correspondence between expressions, except that it still considers Real numbers equal if they differ in their last binary digit.

As 0.999999999999988 and 1. differ by more than one digits they, are considered different.

Answered by Daniel Huber on May 30, 2021

Sign[ 0.999999999999988 - 1.0]

-1

Answered by user64494 on May 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP