I htink this looks horrible, but is not :-)
You noticed the first time the error gets less than zero! If you calculate the error, you'll see it's around 10E-15 at this point (at last on my pc).
#!/usr/bin/perl my $x=0.0; my $y=0; my $e_min=1.0; my $e_max=0.0; my $n=0; my $e; for ( 1 .. 999 ) { $x += 0.05; $y += 5; $e = abs($x - $y / 100); $n++ if $e == 0.0; $e_min = $e if $e > 0.0 && $e < $e_min; $e_max = $e if $e > $e_max; printf "%4d x = %.30f e = %E\n", $_, $x, $e; } printf "min %E max %E exact %d\n", $e_min, $e_max, $n; result 998 x = 49.899999999999302247033483581617 e = 6.963319E-13 999 x = 49.949999999999299404862540541217 e = 7.034373E-13 min 2.775558E-17 max 7.034373E-13 exact 16
You'll find the lines arround 190 interesting:
 189 x = 9.449999999999999289457264239900 e = 0.000000E+00
 190 x = 9.500000000000000000000000000000 e = 0.000000E+00
 191 x = 9.550000000000000710542735760100 e = 0.000000E+00

In reply to Re: adding numbers and floating point errors by Anonymous Monk
in thread adding numbers and floating point errors by smeenz

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.