Skip to main content
gamedev.net
🔒 Locked

float inaccuracy

Started by tomba May 1, 2006 at 11:31 PM 4 replies 1.1k views
Original Post
tomba
tomba
i am using vc++ and working with float numbers since i am working on a opengl project, the coordinates for a sprite is incremented by 0.1f (1 pixel). when I add something like x = x + 0.1f; there is a major loss in accuracy after a while. when values go to like 1.199999999 then worst like 1.32999999. Basically accuracy is always lost as i add more. is there any way to add float numbers accurately? I tried some previous suggestion on the forum, none worked. mine game is similar to that of pokemon and final fantasy where u move by tile. so if they press left, the character moves 1 whole tile (10 pixel). so i need add 0.1 – 10 times and they finally rounded in the end. when i do the rounded off in the end it causes the graphics to jerk when the value is rounded. Reason it jerks is because my camera is set to this value. So when I rounded off, the camera shifts by a little to the accurate amount. So my final ques is : is there a better way to work with accuracy with float?
Zipster
Zipster
Instead of keeping track of the floating-point location, keep track of the pixel location, and divide by ten whenever you pass OpenGL the location. That way your internal representation maintains perfect precision.
bubu LV
bubu LV
Use double :)
Digitalfragment
Digitalfragment
bubu LV: Using a double isnt really a solution. Its still a fixed precision, and is now double the size, and may even hamper peformance (albeit on a very small scale)

unfortunately, float and double cannot be accurate to cirtain multiples - solely because of the way that the processor actually determines the values from the bits. to keep the most accurate reading, your best bet isnt too keep modifying the variable incrementally, but recalculating it (x = i * 0.1f). Not to sure if even that will work though...
Fred304
Fred304
A float takes up 32 bits, so there are 2^32 different float numbers, and 0.1f is not one of them.

BTW you would not have this problem if your tiles were 16 pixels wide, because 1/16 = 0.0625f = 1*2^(-4) = can be represented exactly.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.