These two programs — compiled with Microsoft Visual C++ and run on a 32-bit Intel Core Duo processor — demonstrate an anomaly that occurs when using single-precision floating point variables:
Program 1
#include "stdio.h" int main (void) { float f1 = 0.1f, f2 = 3.0f, f3; f3 = f1 * f2; if (f3 != f1 * f2) printf("Not equal\n"); }
Prints “Not equal”.
Program 2
#include "stdio.h" int main (void) { float f1 = 0.7f, f2 = 10.0f, f3; int i1, i2; f3 = f1 * f2; i1 = (int)f3; i2 = (int)(f1 * f2); if (i1 != i2) printf("Not equal\n"); }
Prints “Not equal”.
In each case, f3 and f1 * f2 differ. But why? I’ll explain what’s going on.