Change your the print to
printf "%.16e\n", $x;
and you get
1.0000000000000000e+000 1.1000000000000001e+000 1.2000000000000002e+000 1.3000000000000003e+000 1.4000000000000004e+000 1.5000000000000004e+000 1.6000000000000005e+000 1.7000000000000006e+000 1.8000000000000007e+000 1.9000000000000008e+000 Stop: x=2.0000000000000009e+000 No, x is not 2.

1/10 is a periodic number in binary, just like 1/3 is a periodic number in decimal.

You'll get better results if you avoid accumulating the error:

for ( 1*10 .. 2*10 ) { my $x = $_/10; printf "%.16e\n", $x; }
1.0000000000000000e+000 1.1000000000000001e+000 1.2000000000000000e+000 1.3000000000000000e+000 1.3999999999999999e+000 1.5000000000000000e+000 1.6000000000000001e+000 1.7000000000000000e+000 1.8000000000000000e+000 1.8999999999999999e+000 2.0000000000000000e+000

Basically, introduce decimal numbers as late as possible. For example, work with cents instead of dollars.


In reply to Re: When is a 2 not a 2? by ikegami
in thread When is a 2 not a 2? by RockM66

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.