简体   繁体   English

整数和整数有什么区别?

[英]What is the difference between Int and Integer?

In Haskell, what is the difference between an Int and an Integer ?在 Haskell 中, IntInteger什么区别? Where is the answer documented?答案记录在哪里?

"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. Lisp 用户可能会在这里认出“bignum”类型。

"Int" is the more common 32 or 64 bit integer. “Int”是更常见的 32 位或 64 位整数。 Implementations vary, although it is guaranteed to be at least 30 bits.实现方式各不相同,但保证至少为 30 位。

Source: The Haskell Wikibook .资料来源: Haskell 维基教科书 Also, you may find the Numbers section of A Gentle Introduction to Haskell useful.此外,您可能会发现A Gentle Introduction to HaskellNumbers部分很有用。

Int is Bounded , which means that you can use minBound and maxBound to find out the limits, which are implementation-dependent but guaranteed to hold at least [-2 29 .. 2 29 -1]. Int is Bounded ,这意味着您可以使用minBoundmaxBound来找出限制,这些限制取决于实现,但保证至少保持 [-2 29 .. 2 29 -1]。

For example:例如:

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

However, Integer is arbitrary precision, and not Bounded .但是, 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)

Int is the type of machine integers, with guaranteed range at least -2 29 to 2 29 - 1, while Integer is arbitrary precision integers, with range as large as you have memory for. Int是机器整数的类型,保证范围至少为 -2 29到 2 29 - 1,而Integer是任意精度整数,范围与您的内存一样大。

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

Int is the C int, which means its values range from -2147483647 to 2147483647, while an Integer range from the whole Z set, that means, it can be arbitrarily large. Int是C的int,表示它的取值范围从-2147483647到2147483647,而Integer范围是整个Z集,表示可以任意大。

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

Notice the value of the Int literal.注意 Int 文字的值。

The Prelude defines only the most basic numeric types: fixed sized integers (Int), arbitrary precision integers (Integer), ... Prelude 只定义了最基本的数字类型:固定大小的整数 (Int)、任意精度整数 (Integer)、...

... ...

The finite-precision integer type Int covers at least the range [ - 2^29, 2^29 - 1].有限精度整数类型 Int 至少覆盖范围 [ - 2^29, 2^29 - 1]。

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

An Integer is implemented as an Int# until it gets larger than the maximum value an Int# can store. Integer作为Int#实现,直到它大于Int#可以存储的最大值。 At that point, it's a GMP number.在这一点上,它是一个GMP编号。

Integer allows for more aggressive optimizations because it is not as constrained by undefined behavior as a result of overflows. 整数允许进行更积极的优化,因为它不受溢出所导致的不确定行为的约束。

ie, the compiler must assume the expression as written won't ever experience undefined behavior, and that any potential optimizations the compiler introduces won't also introduce new undefined behavior. 也就是说,编译器必须假定所编写的表达式永远不会经历未定义的行为,并且编译器引入的任何潜在优化也不会引入新的未定义的行为。

or another way 或另一种方式

the expression a - (b - c) is algebraically equivalent to (a + c) - b but the compiler can not do that rearrangement because it is possible that the intermediate value a + c will overflow with inputs which would not cause an overflow in the original. 表达式a - (b - c)在代数上等同于(a + c) - b但是编译器无法进行这种重新排列,因为中间值a + c可能会因输入溢出而不会引起in的溢出。原本的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM