开发者

Float value evaluation completely incorrect

Hey internets. I am having a VERY strange issue in C. I am deri开发者_开发百科ving a float value, and then checking to see if it is greater than 0. However, the comparison is always evaluating to true, even if the value is less than zero. Here is the code:

if (sVel > 0.0f)
{
    sVel += 1.0f;
    sVel -= 1.0f;
    NSLog(@"SEP VEL: %1.6f", sVel);
    return;
}

So, setting sVel to 100 prints the log as expected and hits the return statement; cool. However, setting sVel to -100 does not print the log and still hits the return statement. I am utterly confused and I'm not sure where to start tracking this one down...


It looks like you are tracing the code using a debugger. Most compilers will probably optimize the code and the same return statement will be executed. It would appear then, that you are jumping to the return code regardless of the initial value of sVel.

The real proof is the print statement though. Since the log is not printed, that means, sVel was evaluated correctly. Try to put a log outside of the if block and see when it gets displayed.


However, setting sVel to -100 does not print the log and still hits the return statement.

What return statement ? If sVel is negative, it should not execute anything. What comes after the fallthrough ? Are you sure its just not returning from the function ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜