# What is the difference between Int and Integer?

In Haskell, what is the difference between an `Int` and an `Integer`? 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.

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

Source: The Haskell Wikibook. Also, you may find the Numbers section of A Gentle Introduction to Haskell useful.

• According to this answer, using `Integer` is often faster than is – Maarten Jan 4 '15 at 17:06
• @Maarten, that's only because `Int64` is implemented rather badly on 32-bit systems. On 64-bit systems, it's great. – dfeuer Sep 21 '16 at 5:09

`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 [-229 .. 229-1].

For example:

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

However, `Integer` is arbitrary precision, and not `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 -229 to 229 - 1, while Integer is arbitrary precision integers, with range as large as you have memory for.

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.

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

Notice the value of the Int literal.

• GHCi, version 7.10.3 gives warning : Literal 12345678901234567890 is out of the Int range -9223372036854775808..9223372036854775807 – Adam Aug 6 '17 at 17:55

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].

An `Integer` is implemented as an `Int#` until it gets larger than the maximum value an `Int#` can store. At that point, it's a GMP number.

• This sounds implementation specific. Is there a reference saying that Integer needs to be implemented this way? – yoniLavi Apr 8 '16 at 19:38
• No, you're right, this is GHC specific. That said, 1. GHC is what most people use, 2. This is the most intelligent way I can think of to implement such a data type. – Nate Symer Apr 9 '16 at 16:48
• Does this mean that (in GHC) there's no performance tradeoff for using `Integer`, and therefore `Integer` is always the better option? – Kirk Broadhurst Jun 19 at 17:51

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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.84575367,"math_prob":0.89431864,"size":3821,"snap":"2019-35-2019-39","text_gpt3_token_len":985,"char_repetition_ratio":0.11186796,"word_repetition_ratio":0.0049423394,"special_character_ratio":0.29651925,"punctuation_ratio":0.16598639,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9829025,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-09-22T11:22:46Z\",\"WARC-Record-ID\":\"<urn:uuid:ad23c51b-94c3-49e9-b716-c3f8f2db7c0e>\",\"Content-Length\":\"183776\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9216530e-2e1d-4248-a127-88439d3d2e45>\",\"WARC-Concurrent-To\":\"<urn:uuid:88eaaeda-bb48-4be2-92a6-0ec37fd0664e>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/3429291/what-is-the-difference-between-int-and-integer/3429322\",\"WARC-Payload-Digest\":\"sha1:O5AYCD4O5ZC74YPSNITKHHMZRXHSE2IW\",\"WARC-Block-Digest\":\"sha1:KKI6CIOJ3654EZGVLOKN4SUUVH2L3PHR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-39/CC-MAIN-2019-39_segments_1568514575484.57_warc_CC-MAIN-20190922094320-20190922120320-00488.warc.gz\"}"}