I need to have some float divisions that must be accurate like double version of them. I can change divided value - it represents a mapping and I can offset it - to correct eventual floating point errors.
To correct the errors, I use following code:
do
{
float fValue = float(x) / 1024.f;
double oldFValue = fValue;
double dValue = double(x) / 1024.0;
if(oldFValue != dValue)
{
x += 1;
}
else
{
break;
}
}while(1);
With this code, for
x = 11
I have in debugger (Visual Studio 2010):
fValue = 0.010742188
oldFValue = 0.010742187500000000
Can you please explain why double value is different from float value? Is this a debugger problem or a floating point conversion problem? I'm asking this because:
if(oldFValue != dValue)
is never true, even though it should be. Should I compare the float value with the double value in some other way? I need the result of float division to be exactly same as the double division.