5

I am writing a function in Haskell that deals with numbers beyond the length of a 32 bit int. I cannot find the type to do this and I seem to be searching for the wrong terms.

It needs to be able to hold numbers with the length of about 2^40 without any loss of precision

Example:

addTwo :: Int -> Int -> Int
addTwo a b = a + b

main :: IO()
main = do
    putStrLn ( show ( addTwo 700851475143 1 ) )
Don Stewart
  • 137,316
  • 36
  • 365
  • 468
Yacoby
  • 54,544
  • 15
  • 116
  • 120
  • 4
    ironically, if you hadn't put the signature for addTwo in, it would have worked, because addTwo would have been inferred to be polymorphic (i.e. Num a => a -> a -> a), and then for the numbers, it would have defaulted to the most general type, which is Integer – newacct Jun 15 '09 at 00:38
  • 5
    But I wouldn't have learnt anything, which is the whole point – Yacoby Jun 15 '09 at 08:46

3 Answers3

21

For unbounded precision, use the Integer type. For 64 bits of precision, across platforms, use Data.Int.Int64. Both will be easy to find with Hoogle: http://haskell.org/hoogle/

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
7

You want the Integer data type instead of Int:

addTwo :: Integer -> Integer -> Integer
Samir Talwar
  • 14,220
  • 3
  • 41
  • 65
0

Use Integer, which is unlimited precision, instead of Int.

Andrew Jaffe
  • 26,554
  • 4
  • 50
  • 59