Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: adding numbers and floating point errors

by Anonymous Monk
on Nov 23, 2006 at 19:36 UTC ( [id://585769]=note: print w/replies, xml ) Need Help??


in reply to adding numbers and floating point errors

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://585769]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found