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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.