Passing double types to ceil results in different values for different optimization levels in GCC <p>Below, the result1 and result2 variable values are reporting different values depending upon whether or not you compile the code with -g or with -O on GCC 4.2.1 and on GCC 3.2.0 (and I have not tried more recent GCC versions):</p> <pre><code>double double_identity(double in_double) { return in_double; } ... double result1 = ceil(log(32.0) / log(2.0)); std::cout &lt;&lt; __FILE__ &lt;&lt; ":" &lt;&lt; __LINE__ &lt;&lt; ":" &lt;&lt; "result1==" &lt;&lt; result1 &lt;&lt; std::endl; double result2 = ceil(double_identity(log(32.0) / log(2.0))); std::cout &lt;&lt; __FILE__ &lt;&lt; ":" &lt;&lt; __LINE__ &lt;&lt; ":" &lt;&lt; "result2==" &lt;&lt; result2 &lt;&lt; std::endl; </code></pre> <p>result1 and result2 == 5 only when compiling using -g, but if instead I compile with -O I get result1 == 6 and result2 == 5.</p> <p>This seems like a difference in how optimization is being done by the compiler, or something to do with IEEE floating point representation internally, but I am curious as to exactly how this difference occurs. I'm hoping to avoid looking at the assembler if at all possible.</p> <p>The above was compiled in C++, but I presume the same would hold if it was converted to ANSI-C code using printfs.</p> <p>The above discrepancy occurs on 32-bit Linux, but not on 64-bit Linux.</p> <p>Thanks bg</p>