Opened 2 years ago

Closed 2 years ago

#6012 closed bug (invalid)

No warning for Incorrect display of 15! (factorial 15) when type is Int -> Int

Reported by: George.Atelier Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.0.4
Keywords: Int Integer recursion Cc:
Operating System: Windows Architecture: x86_64 (amd64)
Type of failure: Incorrect result at runtime Difficulty:
Test Case: factorial 15 Blocked By:
Blocking: Related Tickets:

Description

GHCi version: 7.0.4

Offending function:

factorial :: Int -> Int
factorial 0 = 1
factorial n = n * factorial (n-1)

Correct answer is displayed (as expected) when type is

factorial :: Integer -> Integer

THE BUG IS THE DISPLAY OF A CORRECT-LOOKING INCORRECT ANSWER THAT RESULTED FROM (UNREPORTED) OVERFLOW OF Int (but not of Integer)

Thank you

Change History (1)

comment:1 Changed 2 years ago by dterei

  • Resolution set to invalid
  • Status changed from new to closed

1) Calm down.

2) Please check the Haskell language specification. Int is a bounded integer value guaranteed to be at least 30 bits long. The results of overflow are unspecified by the Haskell language specification and up to implementations to handle how they want. So GHC chooses to silently overflow.

The finite-precision integer type Int covers at least the range
[  −  229, 229  −  1]. As Int is an instance of the Bounded class,
maxBound and minBound can be used to determine the exact
Int range defined by an implementation. Float is
implementation-defined; it is desirable that this type be at least
equal in range and precision to the IEEE single-precision type.
Similarly, Double should cover IEEE double-precision. The
results of exceptional conditions (such as overflow or underflow)
on the fixed-precision numeric types are undefined; an
implementation may choose error (⊥, semantically), a truncated
value, or a special value such as infinity, indefinite, etc.

http://www.haskell.org/onlinereport/haskell2010/haskellch6.html#x13-1350006.4

3) Integer works as you expect as Integer is an unbounded integer value. So it will always be be big enough to represent the value you need. However this makes it a lot slower than an Int.

Note: See TracTickets for help on using tickets.