[Python-bugs-list] Inaccuracy of <= to two or more decimal places (PR#351)
firstname.lastname@example.org (Skip Montanaro)
Fri, 9 Jun 2000 11:13:24 -0500 (CDT)
Raj> Hello. K. Sandretto and I believe we found the "<=" operator
Raj> functions like a "<" when incrementing loop variables with numbers
Raj> smaller than 0.1.
No, you're just experiencing the inexactness of floating point arithmetic.
You'd get the same results if you wrote your code in C.
To avoid this particular case try the following code instead, which avoids
the comparison of floating point numbers and only computes the interesting
value when it's needed:
LoopVariable = 0
Increment = 5
while LoopVariable <= 100:
print LoopVariable / 100.0
LoopVariable = LoopVariable + Increment
Skip Montanaro, email@example.com, http://www.mojam.com/, http://www.musi-cal.com/
On June 24th at 8AM, live your life for an hour as Ricky Byrdsong always lived
his - run/walk in the Ricky Byrdsong Memorial 5K: