tlm has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks,
This node is somewhere in between a SoPW question and a meditation-like cautionary tale. The questions are at the end.
I just got burned pretty good by the following (keep an eye out for those decimal points):
use strict; use warnings; use Math::Pari; my %zero; my %zero_dot; my %delta; @zero { qw( perl pari ) } = ( 0 ) x 2; @zero_dot{ qw( perl pari ) } = ( 0. ) x 2; $delta{ perl } = 1./7E300 ; $delta{ pari } = PARI( 1./7E300 ); $zero { perl } += $delta{ perl }; $zero_dot{ perl } += $delta{ perl }; $zero { pari } += $delta{ pari }; $zero_dot{ pari } += $delta{ pari }; print ' $delta{ perl }: ', $delta { perl }, $/; print '0 + $delta{ perl }: ', $zero { perl }, $/; print '0. + $delta{ perl }: ', $zero_dot{ perl }, $/; print $/; print ' $delta{ pari }: ', $delta { pari }, $/; print '0 + $delta{ pari }: ', $zero { pari }, $/; print '0. + $delta{ pari }: ', $zero_dot{ pari }, $/; __END__ $delta{ perl }: 1.42857142857143e-301 0 + $delta{ perl }: 1.42857142857143e-301 0. + $delta{ perl }: 1.42857142857143e-301 $delta{ pari }: 1.428571428571428547E-301 0 + $delta{ pari }: 1.428571428571428547E-301 0. + $delta{ pari }: 0.E-92
Notice how by adding $delta{ pari } to 0., all the precision is lost. The vague instinct that goes something like this: "by adding a decimal point we somehow indicate that we want floating point operations, therefore we avoid any stray integer division anywhere, therefore we preserve precision as much as possible..." is dead wrong in this case. Unfortunately, my code is rife with numbers written like 0., 1., etc., because, as shown above, Perl handles them in a reasonably predictable way. Switching to Math::Pari has brought on a major headache, due to the need to identify all these innocuous-looking but catastrophic precision leaks.
Where is the behavior above documented? What other dangers await the unsuspecting user of Math::Pari? I looked in perldoc Math::Pari, but no luck.
And more to the...er, point: is there some other way to get the maximum precision one gets by using 0 instead of 0., without having scan through the source for stray decimal points? I tried Math::Pari::setprecision with various arguments, but I got the same results as above every time.
Thanks in advance!
the lowliest monk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: On precision in Math::Pari
by kvale (Monsignor) on May 05, 2005 at 02:55 UTC | |
|
Re: On precision in Math::Pari
by Zaxo (Archbishop) on May 05, 2005 at 02:58 UTC |