On 2010-11-23 08:25:24 +0200, Nikos Chantziaras wrote: > I have a problem figuring out the precision of floating point operations. > Basically, DBL_EPSILON seems to give a wrong value. > > Consider this small C program which tries to detect the smallest possible > value of a double that still makes a difference in a "1 + x > 1" floating > point comparison (some people refer to that value as "machine epsilon"): > > #include <stdio.h> > #include <float.h> > > int main(void) > { > double epsilon = 1.0; > > while (1.0 + (epsilon / 2.0) > 1.0) { > epsilon /= 2.0; > } > printf("epsilon = %e\n", epsilon); > printf("DBL_EPSILON: %e\n", DBL_EPSILON); > return 0; > } Since the C standard allows intermediate computations to be carried out with a higher range and precision (and GCC uses that), your program is buggy. :) Basically, the result of each operation should be stored in a variable or cast to double. And you should compile with -std=c99 or any other option that would remove the excess precision, such as -fexcess-precision=standard I think you need GCC 4.5 or higher. Otherwise you should store the values to variables and use -ffloat-store. > The values don't match and DBL_EPSILON gives a much bigger value then the > detected one. Why is that? It would seem that compiling on 32bit with -O0 > yields higher precision. Is this a result of the FPU being used (or not > used)? The extended precision is provided by the processor in 32-bit mode (FPU instead of SSE). -- Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <http://www.vinc17.net/> 100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/> Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)