If I try this oneliner:

perl -e "$a=-50.123456;for($i=0;$i<20;$i++){print qq!$i $a\n!;$a+=0.01}"

I get this:

0 -50.123456 1 -50.113456 2 -50.103456 3 -50.093456 4 -50.083456 5 -50.073456 6 -50.063456 7 -50.053456 8 -50.043456 9 -50.033456 10 -50.023456 11 -50.013456 12 -50.003456 13 -49.993456 14 -49.983456 15 -49.973456 16 -49.963456 17 -49.953456 18 -49.943456 19 -49.933456

But if I try this one:

perl -e "$a=-71.123456;for($i=0;$i<20;$i++){print qq!$i $a\n!;$a+=0.01}"

I get this:

0 -71.123456 1 -71.113456 2 -71.103456 3 -71.093456 4 -71.083456 5 -71.073456 6 -71.063456 7 -71.053456 8 -71.043456 9 -71.033456 10 -71.023456 11 -71.0134559999999 12 -71.0034559999999 13 -70.9934559999999 14 -70.9834559999999 15 -70.9734559999999 16 -70.9634559999999 17 -70.9534559999999 18 -70.9434559999999 19 -70.9334559999999

What's going on here?

Thanks!


In reply to Floating point issue by vitoco

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.