views:

427

answers:

4

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 ) )
+7  A: 

You want the Integer data type instead of Int:

addTwo :: Integer -> Integer -> Integer
Samir Talwar
+1  A: 

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

Andrew Jaffe
+2  A: 

From Learn You a Haskell for Great Good!:

"Int stands for integer. It's used for whole numbers. 7 can be an Int but 7.2 cannot. Int is bounded, which means that it has a minimum and a maximum value. Usually on 32-bit machines the maximum possible Int is 2147483647 and the minimum is -2147483648.

Integer stands for, er … also integer. The main difference is that it's not bounded so it can be used to represent really really big numbers. I mean like really big. Int, however, is more efficient."

Adam Bernier
+13  A: 

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