in reply to When is a 2 not a 2?
OK; it's not weird; it's normal behavior for floating point numbers. Back when I started programming (on a Univac 1108, which used 48 bit reals, and could distinguish 0 from -0), one of the first comments by the instructors was "Do not test floating point numbers for equality." It's still good advice; see "What Every Computer Scientist Should Know About Floating-Point Arithmetic"(Goldberg, David, "What Every Computer Scientist Should Know About Floating-Point Arithmetic", Computing Surveys, March 1991).
As a (believe it or not) relevant aside: Fortran-77 permitted floating point numbers be used for do loop counters (== Perl's for(($x=1; $x <= 2; $x += 0.1)); I believe that they're being removed from the latest standard because there was a) no way they could get them to work portably (a given loop may iterate 1000 times on one platform and 1003 on another) and b) they make it difficult to parallelize the do-loop.
|
|---|