Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: $var == 1 fails when $var = 1

by LanX (Saint)
on Sep 20, 2018 at 15:33 UTC ( [id://1222725]=note: print w/replies, xml ) Need Help??


in reply to Re^2: $var == 1 fails when $var = 1
in thread $var == 1 fails when $var = 1

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

Replies are listed 'Best First'.
Re^4: $var == 1 fails when $var = 1
by mjlush (Novice) on Sep 20, 2018 at 15:40 UTC
    That sort of makes sense :-) Is there a module that covers over these sort of cracks? It would seem to be a routine problem.
      Is there a module that covers over these sort of cracks?

      The crack that has perl's print() function often lie to us about a floating point value is rather annoying.
      You just need to remember that the floating point value that print() provides will be the same as that provided by printf "%.14e", whereas, if you want a practically useful value, you need to printf "%.16e".
      Outputting more than 17 decimal digits of a double is generally of no use. If you want to see the exact value, use printf "%a" (which will display that exact value in hex).

      I gather that there was once a $# special variable (see perldoc perlvar) which allowed one to specify the decimal precision that print() would use.
      But it has been abandoned and disabled owing to difficulties in its implementation.
      You can, of course, write your own subroutine for printing floats in the format of your choice:
      sub p17 { printf "%.16e\n", $_; } sub ph { printf "%a\n", $_; }
      The breaking of the Associative Law of Addition that you experienced is something that can happen when floating point operations overflow the fixed precision.
      To be guaranteed of avoiding that, I think you'd need to work in an environment that avoids overflow by allowing the precision to increase as needed.
      Math::BigFloat does appear to provide that.

      Cheers,
      Rob
      Many, but they slow down calculation by using number objects and overloading operators.

      As already said, if it matters, like with currencies, I just keep adding cents.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1222725]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-19 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found