Dear all, I'm so confused. Just wrote this extremely basic program in Perl and it does not work in the way I expected it to work. Here is the code:
my $range = 0.1; my $i = 0; while( $i < 1 ) { print $i."\n"; $i = $i + $range; }
What does it return?
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Why the "1" at the end? I don't know. But that I can handle. Check out what happens if I want it to loop till $i is lower than 10. The code is now:
my $range = 0.1; my $i = 0; while( $i < 10 ) { print $i."\n"; $i = $i + $range; }
The result is:
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.99999999999999 6.09999999999999 6.19999999999999 6.29999999999999 6.39999999999999 6.49999999999999 6.59999999999999 6.69999999999999 6.79999999999999 6.89999999999999 6.99999999999999 7.09999999999999 7.19999999999999 7.29999999999999 7.39999999999999 7.49999999999999 7.59999999999999 7.69999999999999 7.79999999999999 7.89999999999999 7.99999999999999 8.09999999999999 8.19999999999999 8.29999999999999 8.39999999999999 8.49999999999999 8.59999999999999 8.69999999999999 8.79999999999998 8.89999999999998 8.99999999999998 9.09999999999998 9.19999999999998 9.29999999999998 9.39999999999998 9.49999999999998 9.59999999999998 9.69999999999998 9.79999999999998 9.89999999999998 9.99999999999998
This is crazy!! I had a course on numerical analysis and am aware that if you add very small numbers, some numerical errors can occur. But this? It's like Perl says 2+2 = 3.9999. What the hell? Please tell me what am I doing wrong. I am guessing it's something with types, confusing strings with numericals... All the very best, Wojciech.

In reply to Simple adding numbers by WojciechGajewski

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.