Well... I'm inclined to agree with you that your perl 5.8.6 result is incorrect. The decimal to binary conversion is incorrectly rounded -- so worse than the usual representation error.

On my perl 5.10.0, using IEEE-754 floating point doubles, your 0.99999999976716936 value is converted to the binary floating point value whose bit pattern is 0x3FEF_FFFF_FFDF_FFFF (or +0x1.F_FFFF_FFDF_FFFFp-1, if you prefer). This appears to be the same as your 5.8.6 result.

Looking at that and the next two larger values I see:

0x3FEF_FFFF_FFDF_FFFF == 0.999999999767169245323827908578 (approx) 0x3FEF_FFFF_FFE0_0000 == 0.999999999767169356346130371093 (approx) original value == 0.99999999976716936 0x3FEF_FFFF_FFE0_0001 == 0.999999999767169467368432833609 (approx)
What's odd about this is that the decimal to binary conversion is not 'correctly rounded' (I'm assuming round-to-nearest as the only sensible rounding for the conversion) -- 0x3FEF_FFFF_FFE0_0000 is clearly a lot closer to the original 0.99999999976716936 than the values on either side of it !

I'm damned if I can see a good reason for getting this wrong. I note only that if you truncate the original value to 16 significant digits (ie to 0.9999999997671693) then 0x3FEF_FFFF_FFDF_FFFF is the correctly converted result.

I get the same results on 64-bit Linux machine and 32-bit Winders. So, either two different libraries have the same decimal to binary fault, or something in perl is wrong.

This is bad. IEEE-754 requires the result of a conversion from binary to decimal and back to binary to be the original binary value, provided that at least 17 significant decimal digits are used (for doubles). I find that:

0x3FEF_FFFF_FFE0_0000 -> 0.99999999976716936 -> 0x3FEF_FFFF_FFDF_FFF +F

However, wrong as this is, the general advice -- not to depend on floating point results to be exactly equal to something -- always applies.


In reply to Re: Perl 5.8.x floating point representation error by gone2015
in thread Perl 5.8.x floating point representation error by satish.rpr

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.