Opened 8 years ago

Closed 8 years ago

## #3033 closed bug (invalid)

# numerical error introduced by -O1

Reported by: | roland | Owned by: | |
---|---|---|---|

Priority: | normal | Milestone: | |

Component: | Compiler | Version: | 6.10.1 |

Keywords: | Cc: | ||

Operating System: | Unknown/Multiple | Architecture: | Unknown/Multiple |

Type of failure: | None/Unknown | Test Case: | |

Blocked By: | Blocking: | ||

Related Tickets: | Differential Rev(s): | ||

Wiki Page: |

### Description

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)

This program prints "-2.328125" when compiled with "-O1".
Running `main`

from within ghci gives "0.0", as it should.

Occurs under Mac OS and Linux (both x86).

### Change History (2)

### comment:1 Changed 8 years ago by

### comment:2 Changed 8 years ago by

difficulty: | → Unknown |
---|---|

Resolution: | → invalid |

Status: | new → closed |

Thanks guest (Lennart?).

**Note:**See TracTickets for help on using tickets.

So, interestingly gcc prints non-zero with no optimization and 0 with optimization.

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.

This is life with the x86, unless you really want to cripple performance and truncate after each FP operation.