190

Int在 Haskell 中, an和 an 有什么区别Integer?答案记录在哪里?

4

6 回答 6

207

“整数”是一种任意精度类型:它可以容纳任何数字,无论多大,直到机器内存的限制……。这意味着你永远不会有算术溢出。另一方面,这也意味着您的算术相对较慢。Lisp 用户可能会在这里认出“bignum”类型。

“Int”是更常见的 32 位或 64 位整数。实现方式各不相同,但保证至少为 30 位。

资料来源:Haskell Wikibook。此外,您可能会发现A Gentle Introduction to Haskell的Numbers部分很有用。

于 2010-08-07T05:59:35.500 回答
27

Intis Bounded,这意味着您可以使用minBoundandmaxBound找出限制,这些限制取决于实现,但保证至少保持 [-2 29 .. 2 29 -1]。

例如:

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

但是,Integer是任意精度,而不是Bounded.

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)
于 2014-05-16T21:28:48.347 回答
22

Int是机器整数的类型,保证范围至少为 -2 29到 2 29 - 1,而Integer是任意精度整数,范围与内存一样大。

https://mail.haskell.org/pipermail/haskell-cafe/2005-May/009906.html

于 2010-08-07T05:59:11.863 回答
11

Int 是 C int,这意味着它的值范围从 -2147483647 到 2147483647,而整个Z集合的 Integer 范围,这意味着它可以任意大。

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

注意 Int 文字的值。

于 2010-08-07T06:00:01.630 回答
5

Prelude 只定义了最基本的数字类型:固定大小的整数 (Int)、任意精度的整数 (Integer)、...

...

有限精度整数类型 Int 至少覆盖范围 [ - 2^29, 2^29 - 1]。

来自 Haskell 报告:http ://www.haskell.org/onlinereport/basic.html#numbers

于 2010-08-07T06:49:00.330 回答
5

AnInteger被实现为 an,Int#直到它大于 anInt#可以存储的最大值。那时,它是一个GMP编号。

于 2016-03-29T02:38:45.443 回答