in reply to \$var == 1 fails when \$var = 1

floating-point numbers have rounding errors and our decimal system isn't too compatible to binary representation

```DB<8> printf '%.20f', 0.688 + 0.289 + 0.023
0.99999999999999989000

so \$var != 1

Perl's DWIM is sometimes trying to round the string representation, that's why eq seemingly "works".

Rule of thumb, add integers and correct afterwards to the right magnitude.

```  DB<15> p 1 == (688 + 289 + 23) /1000
1

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: \$var == 1 fails when \$var = 1
by mjlush (Novice) on Sep 20, 2018 at 15:23 UTC
Why does the order of addition matter?
```DB<8> printf '%.20f', 0.688
0.68799999999999994493
DB<9> printf '%.20f', 0.688 + 0.023
0.71099999999999996536
DB<10> printf '%.20f', 0.688 + 0.023  + 0.289
1.00000000000000000000
DB<11> printf '%.20f', 0.688 + 0.289 + 0.023
0.99999999999999988898
```
PS and thanks for the prompt answer!
Because you are accumulating different rounding errors and (most likely) a shifting mantissa changes the last bit to be rounded.

##### update

Rounding is often not very intuitive, try to avoid if possible.

If you want to track the details you need to look into the binary representation and the underlying C lib.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

That sort of makes sense :-) Is there a module that covers over these sort of cracks? It would seem to be a routine problem.