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


In reply to On precision in Math::Pari by tlm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.