GHC: Ticket #3033: numerical error introduced by -O1
http://ghc.haskell.org/trac/ghc/ticket/3033
<pre class="wiki">f x = x * 1000000.1
g y True = y
g y False = g y True
z = let x = 100000000000.0 in
f x - g (f x) True
main = putStrLn (show z)
</pre><p>
This program prints "-2.328125" when compiled with "-O1".
Running <tt>main</tt> from within ghci gives "0.0", as it should.
</p>
<p>
Occurs under Mac OS and Linux (both x86).
</p>
en-usGHChttp://ghc.haskell.org/trac/ghc/chrome/site/ghc_logo.png
http://ghc.haskell.org/trac/ghc/ticket/3033
Trac 1.0.7guestTue, 17 Feb 2009 09:18:04 GMT
http://ghc.haskell.org/trac/ghc/ticket/3033#comment:1
http://ghc.haskell.org/trac/ghc/ticket/3033#comment:1
<p>
So, interestingly gcc prints non-zero with no optimization and 0 with optimization.
</p>
<p>
The problem is due to the fact the floating point on the 387 compatible FPU stores a double in 64 bits, but internally computes with 80 bits. So the first (f x) is computed and stored, then the second one is computed, and the first one is subtracted. But one of the (f x) calls has 80 bits all the way and the other has been truncated to 64 bits and lost some bits.
</p>
<p>
This is life with the x86, unless you really want to cripple performance and truncate after each FP operation.
</p>
TicketsimonmarTue, 24 Feb 2009 12:50:23 GMTstatus changed; difficulty, resolution set
http://ghc.haskell.org/trac/ghc/ticket/3033#comment:2
http://ghc.haskell.org/trac/ghc/ticket/3033#comment:2
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>difficulty</strong>
set to <em>Unknown</em>
</li>
<li><strong>resolution</strong>
set to <em>invalid</em>
</li>
</ul>
<p>
Thanks guest (Lennart?).
</p>
Ticket