float comparison gives wrong result in loop?
Bradley Smith
digitalmars-com at baysmith.com
Tue Sep 19 01:42:51 PDT 2006
With the following code, why does the assert fail?
import std.stdio;
void main() {
const float STEP_SIZE = 0.2f;
float j = 0.0f;
while (j <= ( 1.0f / STEP_SIZE)) {
j += 1.0f;
writefln(j <= ( 1.0f / STEP_SIZE));
}
assert(!(j <= ( 1.0f / STEP_SIZE)));
}
The loop exits early, but it should not. The result of
"j <= ( 1.0f / STEP_SIZE)" is somehow different in the while statement
versus in the call to writefln.
Interestingly, if the code is optimized, the assert passes.
Is this a bug?
Thanks,
Bradley
More information about the Digitalmars-d-learn
mailing list