Welcome to the Monastery | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
The problem is that 0.001 cannot be exactly stored as a floating-point number, just as you cannot store 1/3 exactly as a decimal number. See What Every Computer Scientist Should Know About Floating-Point Arithmetic for details, or search for floating point here on perlmonks. A way around is to start $a as 22400, add 1 to it at each iteration, and remember to divide by 1000 in the end before using the result (or never divide by 1000, but use the result in a way that accounts for the additional factor) Note that Perl 6 solves the problem by storing 0.001 as a rational number by default.
produces 22.443 as output with Rakudo and Niecza. In reply to Re: limit of for loop!!
by moritz
|
|