Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain this Integer modular behavior to me in Haskell?

Tags:

haskell

*Test> ((3^40) `mod` 3) :: Int
2
*Test> ((3^40) `mod` 3)
0

Why is this so? I am using GHCi 7.0.3. If this is not a bug, an explanation of how Integral/Int works in haskell is appreciated, or a link to an explaination.

Thanks.

like image 461
awh Avatar asked Feb 26 '13 20:02

awh


1 Answers

You're simply out of range, 3^40 is too big of a number to even fit in a 64-bit int:

Prelude> 3^40 :: Int
-6289078614652622815
Prelude> 3^40 :: Integer
12157665459056928801

The Integer type on the other hand is unbounded and accepts all numbers no matter how big. In your second case (where you got a 0 result) you got a type Integer inferred.

like image 158
Nikita Volkov Avatar answered Oct 09 '22 19:10

Nikita Volkov