Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

FWIW: I've looked at the Perl source (in particular numeric.c) and have submitted a bug report.

It turns out that Perl has a home-grown Perl_my_atof(), which makes a reasonable fist of converting decimal to binary, given that it is trying to do so in a system-independent manner. To produce the required "correctly rounded" result it is necessary to use some extended precision arithmetic, which is hard to do in a system-independent fashion. The code in Perl_my_atof() handles the digits before and after the decimal point as separate floating point values, which is a form of extended precision -- unfortunately, the way it does this can cause multiple roundings to occur...

It goes wrong in in this case because your number 0.99999999976716936 has 17 digits after the decimal point, which is rounded to 99999999976716928 and rounded again when it is divided by 10**17 to give 0.999999999767169245323827908578 (approx).

IMHO Perl_my_atof() should be rewritten to use the atof() C library function to do the actual conversion -- having done all the Perl specific parsing and locale and such, at the text level. That way the local library can use the required local means to get the required precision.


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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 01:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found