Ok so I don't know if this will help or impress anybody, but a few days ago I thought of a way to check if a floating point value is imprecise and fix it. As you probably know, if you attempt to add or subtract from a float, but the value which you add or subtract is too small, the float value will not change. This could possibly lead to errors. However, I have created a very basic function that should determine the correct amount to subtract from the float in order for its value to change. Just paste this into your code, and it should work:
Code: Select all
float fixFloat(float myFloat, int value, int exponent)
{
/*
If myFloat minus "value" is still the original float,
change the scientific notation (<-- defined as "exponent") of "value"
until myFloat - (value * (10 ^ exponent)) != myFloat
*/
while(myFloat - (value * (10 ^ exponent)) == myFloat)
{
exponent += 1;
}
myFloat -= (value * (10 ^ exponent));
return myFloat;
}
