Problem with float numbers

1 view
Skip to first unread message

Cuong Nguyen

unread,
Aug 1, 2010, 4:17:19 AM8/1/10
to PSUComputerGraphics
Hi,

I'm having some troubles with using floating point numbers in OpenGL.
Sometimes by adding or subtracting them, I get very weird results.

For example I have a vector A(2, 0, 7) and I try to decrease the value
of Ax by 0.1, that is:
2, 0, 7
1.9, 0, 7
1.8, 0, 7
...
until I started to get these strange numbers:
0.0999997 , 0 , 7
-3.12924e-07 , 0 , 7

From that point on, the calculation goes terribly wrong and I don't
know how to fix it.

Thank you for any helps/hints.

Brian Delgado

unread,
Aug 1, 2010, 12:24:44 PM8/1/10
to psucomput...@googlegroups.com
I've run into the same thing with my program. I also increment/decrement floating point values (GLfloat) by 0.1.
 
In my program, this caused problems because for collision detection I was checking to see if two floating point numbers were equal. Often they would be just slightly off. I've worked around it (although it's kludgy) by doing the following checks to see if the results are "close enough" to be equal.
 
// Close enough check
if (((BALL_TL_X - BL_X) < 0.0000001) && ( (BALL_TL_X - BL_X) > 0.0) {
 // They're "equal"
}
 
// more "equal" checks

If there's a better way to deal with this, I'm interested too.

Regards, Brian




--
You received this message because you are subscribed to the Google Groups "PSUComputerGraphics" group.
To post to this group, send email to psucomput...@googlegroups.com.
To unsubscribe from this group, send email to psucomputergrap...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/psucomputergraphics?hl=en.


Reply all
Reply to author
Forward
0 new messages