Another approach to this problem would be to avoid perl's limitation in conversion of the number between string and float.

This C code:

int main(void) { double d; d=1.0; while(d>0.0) { printf("%100.80e\n",d); d = d/2.0; } return 1; }
produces the exact same result as this perl code:
my $d; $d=1.0; while($d>0.0) { printf("%100.80e\n",$d); $d = $d/2.0; }
But this is completely different from this perl code, which includes a conversion to string:
my $d; $d=1.0; while($d>0.0) { $d= "$d"; printf("%100.80e\n",$d); $d = $d/2.0; }
The output from the perl program with the string conversion is clearly messed up; it prints these two successive values:
2.38418579101562500000000000000000000000000000000000000000000000000000 +000000000000e-07 1.19209289550780998537093783879586839091757610731292515993118286132812 +500000000000e-07
Where the C code and perl code without the string conversion has the correct result:
2.38418579101562500000000000000000000000000000000000000000000000000000 +000000000000e-07 1.19209289550781250000000000000000000000000000000000000000000000000000 +000000000000e-07
It would be nice if there were a way to tell perl "don't ever convert this number to a string unless I say it's okay." Short of that, you have to either be careful to avoid string conversion, write your numerical code in XS, use BigFloat a lot, or use straight C or FORTRAN. The Inline module also makes calling C easy. C is almost as easy as perl for numerical programming anyway. The PDL module may also do a better job for what you need, especially if you can formulate your problem in terms of vectors or matrices. That way you would gain in both speed and precision!

It should work perfectly the first time! - toma


In reply to Re: Determining when Math::BigFloat is necessary? by toma
in thread Determining when Math::BigFloat is necessary? by jasonk

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.