Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-24 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found