Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by syphilis (Archbishop)
on Sep 21, 2018 at 02:07 UTC ( [id://1222770]=note: print w/replies, xml ) Need Help??


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

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-16 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found