关于haskell:Int和Integer有什么区别?

What is the difference between Int and Integer?

在haskell中,IntInteger有什么区别?答案记录在哪里?


"Integer" is an arbitrary precision
type: it will hold any number no
matter how big, up to the limit of
your machine's memory…. This means you never have
arithmetic overflows. On the other
hand it also means your arithmetic is
relatively slow. Lisp users may
recognise the"bignum" type here.

"Int" is the more common 32 or 64 bit
integer. Implementations vary,
although it is guaranteed to be at
least 30 bits.

源:wikibook Haskell。所以,你可以找到一段温柔的自然数的Haskell的选择。


Int is the type of machine integers,
with guaranteed range at least
-229 to 229 - 1, while Integer is arbitrary precision integers, with
range as large as you have memory for.

http:/ / / / mail.haskell.org pipermail Haskell的咖啡馆/可能/ 009906.html 2005


IntBounded,这意味着你可以使用minBoundmaxBound找出限制,这是依赖于实现的保证,但抱着至少[ 229。229)。

例如:

1
2
Prelude> (minBound, maxBound) :: (Int, Int)
(-9223372036854775808,9223372036854775807)

然而,Integer是任意的和不Bounded精度。

1
2
3
4
5
6
7
8
9
Prelude> (minBound, maxBound) :: (Integer, Integer)

<interactive>:3:2:
    No instance for (Bounded Integer) arising from a use of `minBound'
    Possible fix: add an instance declaration for (Bounded Integer)
    In the expression: minBound
    In the expression: (minBound, maxBound) :: (Integer, Integer)
    In an equation for `it':
        it = (minBound, maxBound) :: (Integer, Integer)

int int是C,这意味着它的值的范围从- 2147483647 to 2147483647,而从整个范围的整数集Z,这是arbitrarily均值,它可以是大的。

1
2
3
$ ghci
Prelude> (12345678901234567890 :: Integer, 12345678901234567890 :: Int)
(12345678901234567890,-350287150)

通知书的int值的文本。


The Prelude defines only the most
basic numeric types: fixed sized
integers (Int), arbitrary precision
integers (Integer), ...

……

The finite-precision integer type Int
covers at least the range [ - 2^29,
2^29 - 1].

从Haskell报告:http:////basic.html www.haskell.org onlinereport #号码


在一个实现,为IntegerInt#直到它得到最大的价值,在装修Int#CAN商店。在那点上,它是一个的GMP。