J
jonas.email
Im am doing a major project and after a long time debugging i found
this rather funny (=annoying) bug. In its simple form, this is the main
problem:
int main(void) {
float f1 = 0.3f;
float f2 = 0.1f;
printf("%f, %.10f, %f, %.10f\n", f1, f1, f2, f2);
if(f1 == 3*f2)
printf("%f == 3*%f\n", f1, f2);
if(f1 == 3.f*f2)
printf("%f == 3.0*%f\n", f1, f2);
return 0;
}
Output: 0.300000, 0.3000000119, 0.100000, 0.1000000015
Using MS Visual Studio 6 on a Windows XP box
So the problem is, that i need to do a more precise assignment of the
float variables, because the variable f1 is not 0.3 (and f2 is not 0.2)
but bigger. But how?
this rather funny (=annoying) bug. In its simple form, this is the main
problem:
int main(void) {
float f1 = 0.3f;
float f2 = 0.1f;
printf("%f, %.10f, %f, %.10f\n", f1, f1, f2, f2);
if(f1 == 3*f2)
printf("%f == 3*%f\n", f1, f2);
if(f1 == 3.f*f2)
printf("%f == 3.0*%f\n", f1, f2);
return 0;
}
Output: 0.300000, 0.3000000119, 0.100000, 0.1000000015
Using MS Visual Studio 6 on a Windows XP box
So the problem is, that i need to do a more precise assignment of the
float variables, because the variable f1 is not 0.3 (and f2 is not 0.2)
but bigger. But how?