Jump to content
  • Advertisement
Sign in to follow this  
tomba

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!