quote:Original post by MadKeithV
Eh eh, try this one, an error I had in my engine just the other day:
float x = 1.0f;
int y = x/0.1f;
y = .... 9!!!!!!!!!
It''s all down to discrete mathematics. When you create a floating point value of 0.1, the compiler creates the closest floating point value to 0.1 , which in this case is slightly more than 0.1
So, when you divide 1 by slighly more than 0.1, you get slightly less than 10 - and when you cast this to an int it gets truncated to 9.
This is an inherent limitation of converting from decimal to binary and vice-versa . You can improve the accuracy of your engine by:
a) Adding 0.5f before casting to int , ie. y = (int) (0.5f + x/0.1f);
b) Changing y to be floating point (if possible)
c) Switching from single- to double-precision floating point (but this will cost you speed, and will still have some problems)