1.1 > 1.1?
by Eric Hartman · in Torque Game Engine · 01/22/2006 (4:41 pm) · 3 replies
$crap = 1.1;
echo($crap > 1.1);
result: 1
Strange.
echo($crap > 1.1);
result: 1
Strange.
About the author
#2
01/22/2006 (5:03 pm)
I know that you cant get exactly 0.1 in binary because it's a repeating decimal and that that will lead to precision problems. But in this case the constant is being interpreted differently than the same constant stored in a variable- which is definitely an error.
#3
01/22/2006 (5:10 pm)
There might be some precision issues here but I'd definitely write my code so as not to care too much about precision with floats. Like Orion said they can have very odd behavior. You could also consider tossing both sides into a variable to get identical behavior, if you must have that. :)
Associate Orion Elenzil
Real Life Plus
this is a good example of why it's a good idea to not rely on the precision of floating-point numbers.
even in straight C++, they're basically not precise, and if you find your code relying on the fact that say 2.2 - 1.1 == 1.1, then you may want to do things differently.
but yeah, this is pretty extreme example.
interestingly, the result of
echo ($crap > 1.1000001);
is the expected 0,
but
echo ($crap > 1.10000001);
is back to 1 again.