#### Archived

This topic is now archived and is closed to further replies.

This topic is 5742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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..

##### Share on other sites
I did what you suggested and I''m still getting wrong answers.

Go on an Intense Rampage

##### Share on other sites
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

##### Share on other sites
Are you wanting "less than or equal" instead of "less than"?
t < 1.0f will be false if t == 1.0f.

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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]

##### Share on other sites
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)

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

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

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.

##### Share on other sites
maybe changing the T for int T?

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631423
• Total Posts
3000003
×