Keep It Simple, Stupid PerlMonks

Re: adding numbers and floating point errors

by imp (Priest)
 on Nov 22, 2006 at 21:42 UTC ( #585618=note: print w/replies, xml ) Need Help??

BrowserUK provided an excellent explanation for why this occurs a few years ago.

To (mostly) fix the issue for your script you can use bignum.

```use bignum;

my \$c = 0;
for (\$x = 0; \$x <= 4; \$x += 0.05){
\$c++;
print "[\$c] x= \$x (".sprintf("%20.40f",\$x).") \n";
}
```[1] x= 0 (0.0000000000000000000000000000000000000000)
[2] x= 0.05 (0.0500000000000000027755575615628913510591)
[3] x= 0.1 (0.1000000000000000055511151231257827021182)
[4] x= 0.15 (0.1499999999999999944488848768742172978818)
[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.4000000000000000222044604925031308084726)
[10] x= 0.45 (0.4500000000000000111022302462515654042363)
[11] x= 0.5 (0.5000000000000000000000000000000000000000)
[12] x= 0.55 (0.5500000000000000444089209850062616169453)
[13] x= 0.6 (0.5999999999999999777955395074968691915274)
[14] x= 0.65 (0.6500000000000000222044604925031308084726)
[15] x= 0.7 (0.6999999999999999555910790149937383830547)
[16] x= 0.75 (0.7500000000000000000000000000000000000000)
[17] x= 0.8 (0.8000000000000000444089209850062616169453)
[18] x= 0.85 (0.8499999999999999777955395074968691915274)
[19] x= 0.9 (0.9000000000000000222044604925031308084726)
[20] x= 0.95 (0.9499999999999999555910790149937383830547)
[21] x= 1 (1.0000000000000000000000000000000000000000)
[22] x= 1.05 (1.0500000000000000444089209850062616169453)
[23] x= 1.1 (1.1000000000000000888178419700125232338905)
[24] x= 1.15 (1.1499999999999999111821580299874767661095)
[25] x= 1.2 (1.1999999999999999555910790149937383830547)
[26] x= 1.25 (1.2500000000000000000000000000000000000000)
[27] x= 1.3 (1.3000000000000000444089209850062616169453)
[28] x= 1.35 (1.3500000000000000888178419700125232338905)
[29] x= 1.4 (1.3999999999999999111821580299874767661095)
[30] x= 1.45 (1.4499999999999999555910790149937383830547)
[31] x= 1.5 (1.5000000000000000000000000000000000000000)
[32] x= 1.55 (1.5500000000000000444089209850062616169453)
[33] x= 1.6 (1.6000000000000000888178419700125232338905)
[34] x= 1.65 (1.6499999999999999111821580299874767661095)
[35] x= 1.7 (1.6999999999999999555910790149937383830547)
[36] x= 1.75 (1.7500000000000000000000000000000000000000)
[37] x= 1.8 (1.8000000000000000444089209850062616169453)
[38] x= 1.85 (1.8500000000000000888178419700125232338905)
[39] x= 1.9 (1.8999999999999999111821580299874767661095)
[40] x= 1.95 (1.9499999999999999555910790149937383830547)
[41] x= 2 (2.0000000000000000000000000000000000000000)
[42] x= 2.05 (2.0499999999999998223643160599749535322189)
[43] x= 2.1 (2.1000000000000000888178419700125232338905)
[44] x= 2.15 (2.1499999999999999111821580299874767661095)
[45] x= 2.2 (2.2000000000000001776356839400250464677811)
[46] x= 2.25 (2.2500000000000000000000000000000000000000)
[47] x= 2.3 (2.2999999999999998223643160599749535322189)
[48] x= 2.35 (2.3500000000000000888178419700125232338905)
[49] x= 2.4 (2.3999999999999999111821580299874767661095)
[50] x= 2.45 (2.4500000000000001776356839400250464677811)
[51] x= 2.5 (2.5000000000000000000000000000000000000000)
[52] x= 2.55 (2.5499999999999998223643160599749535322189)
[53] x= 2.6 (2.6000000000000000888178419700125232338905)
[54] x= 2.65 (2.6499999999999999111821580299874767661095)
[55] x= 2.7 (2.7000000000000001776356839400250464677811)
[56] x= 2.75 (2.7500000000000000000000000000000000000000)
[57] x= 2.8 (2.7999999999999998223643160599749535322189)
[58] x= 2.85 (2.8500000000000000888178419700125232338905)
[59] x= 2.9 (2.8999999999999999111821580299874767661095)
[60] x= 2.95 (2.9500000000000001776356839400250464677811)
[61] x= 3 (3.0000000000000000000000000000000000000000)
[62] x= 3.05 (3.0499999999999998223643160599749535322189)
[63] x= 3.1 (3.1000000000000000888178419700125232338905)
[64] x= 3.15 (3.1499999999999999111821580299874767661095)
[65] x= 3.2 (3.2000000000000001776356839400250464677811)
[66] x= 3.25 (3.2500000000000000000000000000000000000000)
[67] x= 3.3 (3.2999999999999998223643160599749535322189)
[68] x= 3.35 (3.3500000000000000888178419700125232338905)
[69] x= 3.4 (3.3999999999999999111821580299874767661095)
[70] x= 3.45 (3.4500000000000001776356839400250464677811)
[71] x= 3.5 (3.5000000000000000000000000000000000000000)
[72] x= 3.55 (3.5499999999999998223643160599749535322189)
[73] x= 3.6 (3.6000000000000000888178419700125232338905)
[74] x= 3.65 (3.6499999999999999111821580299874767661095)
[75] x= 3.7 (3.7000000000000001776356839400250464677811)
[76] x= 3.75 (3.7500000000000000000000000000000000000000)
[77] x= 3.8 (3.7999999999999998223643160599749535322189)
```[78] x= 3.85 (3.8500000000000000888178419700125232338905)
[79] x= 3.9 (3.8999999999999999111821580299874767661095)
[80] x= 3.95 (3.9500000000000001776356839400250464677811)
[81] x= 4 (4.0000000000000000000000000000000000000000)

Replies are listed 'Best First'.
Re^2: adding numbers and floating point errors
by smeenz (Sexton) on Nov 22, 2006 at 21:56 UTC
Thanks. That explanation does indeed explain the issue.

Create A New User
Node Status?
node history
Node Type: note [id://585618]
help
Chatterbox?
 [shmem]: see my scratchpad. Look for userDict [shmem]: use strict and my %userDict; would be a compile time error in the perl equivalent. [shmem]: note that userDict wasn't declared previously before the for block

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2018-03-20 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (256 votes). Check out past polls.

Notices?