I am wondering about floating point arithmetic in C#/XNA. If I trace into my XNA code the value assigned to “finalAnswer” below is equal to 79.99999999. If I do the calculation on my calculator, the value comes out to the number 80.00.
Also if I break the code up slightly it also comes out to 80.00. See line 2 below. It appears that the number is rounded down, and I don’t unerstand why, because really it should come out as 80.00 instead of 79.99999?
I don’t want to make a big deal out of this, but I am working on something that requires accurate pixel location placement on my screen.
1 float finalAnswer= ((800 / 2) - ((400 * 1.6f) / 2)); // comes out to 79.99999
float pico = (400 * 1.6f)/2;
float finalAnswer = (800/2) - pico; // Comes out to 80.00
This precision loss is due to the way floating point numbers are represented. If you want to do pixel perfect stuff, it’s good practice to always round your numbers. They will often not align exactly to integer values, especially after a lot of calculations.
You will probably find that the value is actually 80, but the string version resolves to 79.9999999999
To see what I mean try doing several
At some point the value will go from 79.9999999 to 80.0
Yep, as above, its to do with floating point accuracy I think.
Thanks Guys. I discovered that the rounding issue was from an older version of Visual Studio. Yes, rounding will resolve the issue.