Ticket #5764 (closed bug: invalid)

Opened 5 months ago

Last modified 5 months ago

Double addition error

Reported by: jimstutt Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.2.2
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: x86
Type of failure: Incorrect result at runtime Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

ghci>0.1+0.2 ghci>3.000000000000004

ghc produces the same result with:

foldl (+) [0.1,0.2]

Change History

Changed 5 months ago by jimstutt

Sorry foldl (+) 0 [0.1,0.2]

Changed 5 months ago by ross

  • status changed from new to closed
  • resolution set to invalid

It's an artifact of fixed-precision floating point not being able to represent decimal fractions exactly; you get the same from C:

#include <stdio.h>

int main() {
        printf("%.17f\n", 0.1+0.2);
        return 0;
}

Changed 5 months ago by simonpj

  • difficulty set to Unknown

Well, if 0.1+0.2 really gave result 3.0000000004, as reported, THAT would be a bug. But it doesn't. It gives 0.3000000004. Wich as Ross says is fine.

Changed 5 months ago by jimstutt

Embarrassment. Didn't mean to waste time - didn't know or realise ghc stuck to IEEE representation nor that addition has rounding errors, not just division. Back to "what every scientist should know about floating point..". And too blind to see 3.0 not .3. Apologies.

Changed 5 months ago by maeder

ghc is not stuck to IEEE (it only defaults to Double) but you can do

Prelude> 0.1 + 0.2 :: Rational
3 % 10

The output is not printed nicely, but how should it print 1/3 as decimal?

Prelude> 1 / 3 :: Rational
1 % 3
Note: See TracTickets for help on using tickets.