Hi,

Today I discovered that floating point errors seem to be much more of a problem than I had anticipated.

Specifically, it seems that if I set up a simple loop to add 0.05 to a variable and then display the result, it quickly goes wrong.

For example, the following code:

my $c = 0; for ($x = 0; $x <= 1; $x += 0.05){ $c++; print "[$c] x= $x (".sprintf("%20.40f",$x).") \n"; }

produces this:

[1] x= 0 (0.0000000000000000000000000000000000000000) [2] x= 0.05 (0.0500000000000000027755575615628913510591) [3] x= 0.1 (0.1000000000000000055511151231257827021182) [4] x= 0.15 (0.1500000000000000222044604925031308084726) [5] x= 0.2 (0.2000000000000000111022302462515654042363) [6] x= 0.25 (0.2500000000000000000000000000000000000000) [7] x= 0.3 (0.2999999999999999888977697537484345957637) [8] x= 0.35 (0.3499999999999999777955395074968691915274) [9] x= 0.4 (0.3999999999999999666933092612453037872910) [10] x= 0.45 (0.4499999999999999555910790149937383830547) [11] x= 0.5 (0.4999999999999999444888487687421729788184) [12] x= 0.55 (0.5499999999999999333866185224906075745821) [13] x= 0.6 (0.5999999999999999777955395074968691915274) [14] x= 0.65 (0.6500000000000000222044604925031308084726) [15] x= 0.7 (0.7000000000000000666133814775093924254179) [16] x= 0.75 (0.7500000000000001110223024625156540423632) [17] x= 0.8 (0.8000000000000001554312234475219156593084) [18] x= 0.85 (0.8500000000000001998401444325281772762537) [19] x= 0.9 (0.9000000000000002442490654175344388931990) [20] x= 0.95 (0.9500000000000002886579864025407005101442)

which is... okayish.. because the errors only show up when I force sprintf to display to 40dp... the problem is that even though I'm only adding 0.05 (and not 0.00000000000000000000000005), the error still creeps into the results pretty quickly.

If I change the loop to run from 0 to 4, which is 100 interations, perl tells me at iteration 74 that 3.65 + 0.05 = 3.69999999999999.

I'm not sure what to think of this.. I wouldn't have expected that adding 0.05 to a number 74 times would start to produce errors as soon as the 1st decimal place.


In reply to 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.