GCC optimized code gives strange floating point results
Comment 109 has a good exposition I&#x27;ll just copy here:<p>Jan Lachnitt 2008-05-20 16:59:31 UTC<p>I also encountered such problems and was going to report it as a bug in GCC...
 But in the GCC bug (not) reporting guide, there is fortunately a link to this page and here (comment #96) is a link to David Monniaux&#x27;s paper about floating-point computations. This explains it closely but it is maybe too long. I have almost read it and hope I have understood it properly. So I&#x27;ll give a brief explanation (for those who don&#x27;t know it yet) of the reasons of such a strange behaviour. Then I&#x27;ll assess where the bug actually is (in GCC or CPU). Then I&#x27;ll write the solution (!) and finally a few recommendations to the GCC team.<p>EXPLANATION<p>The x87 FPU was originally designed in (or before) 1980. I think that&#x27;s why it is quite simple: it has only one unit for all FP data types. Of course, the precision must be of the widest type, which is the 80-bit long double.
Consider you have a program, where all the FP variables are of the type double. You perform some FP operations and one of them is e.g. 1e-300&#x2F;1e300, which results in 1e-600. Despite this value cannot be held by a &quot;double&quot;, it is stored in an 80-bit FPU register as the result. Consider you use the variable &quot;x&quot; to hold that result. If the program has been compiled with optimization, the value need not be stored in RAM. So, say, it is still in the register.
Consider you need x to be nonzero, so you perform the test x != 0. Since 1e-600 is not zero, the test yields true. While you perform some other computations, the value is moved to RAM and converted to 0 because x is of type &quot;double&quot;. Now you want to use your certainly nonzero x... Hard luck :-(
Note that if the result doesn&#x27;t have its corresponding variable and you perform the test directly on an expression, the problem can come to light even without optimization.
<i>It could seem that performing all FP operations in extended precision can bring benefits only. But it introduces a serious pitfall: moving a value may change the value!!!</i><p>WHERE&#x27;S THE BUG<p>This is really not a GCC bug. <i>The bug is actually in the x87 FPU because it doesn&#x27;t obey the IEEE standard.</i><p>SOLUTION<p>The x87 FPU is still present in contemporary processors (including AMD) due to compatibility. I think most of PC software still uses it. But <i>new processors have also another FPU, called SSE, and this do obey the IEEE</i>. GCC in 32-bit mode compiles for x87 by default but it is able to compile for the SSE, too. So the solution is to add these options to the compilation command:
-march=* -msse -mfpmath=sse<p>Yes, this definitely resolves the problem - but not for all processors. The * can be one of the following: pentium3, pentium3m, pentium-m, pentium4, pentium4m, prescott, nocona, athlon-4, athlon-xp, athlon-mp, k8, opteron, athlon64, athlon-fx and c3-2 (I&#x27;m unsure about athlon and athlon-tbird). Beside -msse, you can also add some of -mmmx, -msse2, -msse3 and -m3dnow, if the CPU supports them (see GCC doc or CPU doc).<p>If you wish to compile for processors which don&#x27;t have SSE, you have a few possibilities:<p>(1) A very simple solution: Use long double everywhere. (But be careful when transfering binary data in long double format between computers because this format is not standardized and so the concrete bit representations vary between different CPU architectures.)<p>(2) A partial but simple solution: Do comparisons on volatile variables only.<p>(3) A similar solution: Try to implement a &quot;discard_extended_precision&quot; function suggested by Egon in comment #88.<p>(4) A complex solution: Before doing any mathematical operation or comparison, put the operands into variables and put also the result to a variable (i.e. don&#x27;t use complex expressions). For example, instead of { c = 2<i>(a+b); } , write { double s = a+b; c = 2</i>s; } . I&#x27;m unsure about arrays but I think they should be OK. When you have modified your code in this manner, then compile it either without optimization or, when using optimization, use -ffloat-store. In order to avoid double rounding (i.e. rounding twice), it is also good to decrease the FPU precision by changing its control word in the beginning of your program (see comment #60). Then you should also apply -frounding-math.
 (5) A radical solution: Find a job&#x2F;hobby where computers are not used at all.