When I want to compare two floating points values. I get strange results.
My example compiled with x86_64-w64-mingw32-g++ test.c -mfpmath=387:
#include "math.h"
#include "stdio.h"
int main(int argc, char **args) {
double t1 = atof("999990") * 1e-9;
double t2 = atof("999990") * 1e-9;
printf("1) Compare of two variables \"t1 != t2\": ");
if (t1 != t2) {
printf("Not Equal\n");
}
else {
printf("Equal\n"); //This is taken
}
printf("2) Compare of Variable with calculated value \"t1 != atof(\"999990\") * 1e-9\": ");
if (t1 != atof("999990") * 1e-9) {
printf("Not Equal\n"); //This is taken
}
else {
printf("Equal\n");
}
printf("3) Compare of Variable with casted calculated value \"t1 != (double)(atof(\"999990\") * 1e-9)\": ");
if (t1 != (double)(atof("999990") * 1e-9)) {
printf("Not Equal\n"); //This is taken??
}
else {
printf("Equal\n");
}
return 0;
}
First comparison: OK
Second comparison: OK (assuming right term is a 80Bit precision)
Why evaluates the third comparison to unequal? Why is the cast to double not recognised? Why is the result of the comparison different than the first case?