in reply to Integers sometimes turn into Reals after substraction
First of all, 4/25 is periodic in binary (just like 1/3 is periodic in decimal).
____________________ 4/25 = 0.00101000111101011100 base 2
This means it can't be stored exactly in a floating point number.
Sometimes, floating point number errors get canceled out or reduced to nothing. It turns out that this happens for one of the chains of operations but not the other.
That's why you want to avoid checking if two floating point numbers are equal without allowing for an error margin. Replace
$n1 == $n2
with
abs($n1 - $n2) < $epsilon
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Integers sometimes turn into Reals after substraction (error correction ?)
by LanX (Saint) on May 14, 2016 at 15:54 UTC | |
by pryrt (Abbot) on May 14, 2016 at 17:02 UTC | |
by pryrt (Abbot) on May 14, 2016 at 19:21 UTC |