if() returning wrong answers

Started by
30 comments, last by Graham 21 years, 6 months ago
I am working on a 3d computer game, and while rendering my map many triangles were missing. I have tracked the problem down to an if statement that might be returning true when it should be false. This is what I am doing; float T = 0.0; T = numA / numB; // numA and numB are floats if((T > 0) && (T < 1)) { //do important stuff } I have output the value of numA, numB and T to a file. numA is 12, numB is 12, and T is 1. But the if() is true. Is there anything I can do to make this work. I think that there is a precision problem somewhere in the code. Go on an Intense Rampage
Go on an Intense Rampage
Advertisement
Rule of thumb: when possible, keep all values the same type. Change it to:
if ((T > 0.0f) && (T < 1.0f))

Let us know if it still misbehaves and we can look a little deeper into it.

HTTP 500 error, retry #1..
I did what you suggested and I''m still getting wrong answers.


Go on an Intense Rampage
Go on an Intense Rampage
According to my file output, there are other times when numA and numB are equal to each other, and T is equal to 1, but the if() was false.



Go on an Intense Rampage
Go on an Intense Rampage
Are you wanting "less than or equal" instead of "less than"?
t < 1.0f will be false if t == 1.0f.
I want less than 1.0f
My file output says that T = 1, and if((T > 0.0f) && (T < 1.0f))
statement is true. Then i do a if(T == 1.0f) and it is false.

According to my file output T is 1, but then the first if statement is true, then the (T == 1.0f) is false.
Im very confused.

Go on an Intense Rampage
Go on an Intense Rampage
There is also a time when numA is -9.53674e-007 when it should be zero. This is also causing some errors in my code. It occurs when i subtract one float from another and both floats are the same value.
Go on an Intense Rampage
Are you doing (T > 0) && (T < 1) or (T >= 0) && (T <= 1)?

[edited by - DerekSaw on October 2, 2002 10:50:15 PM]

err... how do you output the floating value?? 0.9999999 might output as 1 sometimes.

[edited by - DerekSaw on October 2, 2002 10:52:27 PM]
"after many years of singularity, i'm still searching on the event horizon"
The reason this occurs is because of the inaccuracy of floating point numbers. Because of how they''re stored, something like 12.0/12.0 can turn out to be something odd, like 1.00000017623 or something.

Recognize when to use equalities/inequalities with floats and when not to. You should consider two floats to be equal if they are within a certain small value, called "epsilon", of each other.
For instance, don''t do:
if((a > b)

instead, do:

if((a-b) > 1.0e-4)

Don''t do:
if(a == b)

instead, do:

if(abs(a-b) < 1.0e-5)

or something like that. Libraries exist to make epsilon calculations easier and more reliable; look in Boost for stuff like that.

Don''t listen to me. I''ve had too much coffee.
maybe changing the T for int T?

This topic is closed to new replies.

Advertisement