in reply to for loop with float
Obviously, it's possible to use floats as increments in for loops: you're doing it. It's just a bad idea, at least partly because accumulation of errors means that the number of loop iterations cannot be guaranteed to be identical across platforms (or even across two versions of Perl built with different compiler options on the same machine).
As mentioned by prior posters, 0.1 cannot be exactly represented in floating point, as 0.1 cannot be exactly represented as a finite sum of the powers of 2, in the same way that 1/3 cannot be exactly represented as a finite sum of powers of 10. For this reason, x = 5 + 0.1 will look like 5.0999999, because 5.1 can't be exactly represented. Floating point numbers are not real numbers; they are only approximations to real numbers, and that only within a finite range. This article will make this point much clearly than I.
|
|---|