# float inaccuracy

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

## Recommended Posts

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?

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

Use double :)

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

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

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

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002228
×