I tried to use the following program to estimate the machine epsilon with a simple C program
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main(){
float f = 1.0f;
float prev_f = 1.0f;
while( 1 + f != 1 ){
prev_f = f;
f /= 2;
}
prev_f *= 2;
printf("Calculated epsilon for float is %.10g", prev_f);
printf("The actual value is %.10f", FLT_EPSILON);
return 0;
}
where my output is
Calculated epsilon for float is 2.802596929e-45
The actual value is 0.0000001192
Can anyone explain this deviation to me? Is it architecture specific? Compiler dependent? Am i doing something wrong?
EDIT: The problem seemed to be due to me using the -Ofast optimization in gcc. Using -O instead solves the problem.