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.