Int, Integer, Double and defaulting
Int, the machine integer
Int is a whole number held in one machine word: 64 bits on the machine this page was built on, so it holds -2^63 to 2^63 - 1. Arithmetic on it is the arithmetic of the processor, and it wraps around at the ends instead of failing. The largest value is a constant, not a guess:
main :: IO ()
main = print (maxBound :: Int)
prints 9223372036854775807. Every Int
on the page is annotated with its type for the reason the
previous lesson
explained: a bare literal has no type of its own, and
-Wall reports the default the compiler had to choose.
The annotation is also what makes maxBound legal, since
that function needs a type that has a largest value.
Use Int for a counter, an index, a length: values
that fit in a machine word and are used in a tight loop. Its operations are
fast because the hardware has them.
Integer, with no limit
Integer is arbitrary precision. It grows as needed, the way a number in a school exercise grows, and the only cost is memory and time. Two to the hundredth is 31 digits, a value no machine word holds:
main :: IO ()
main = print (2 ^ (100 :: Int) :: Integer)
1267650600228229401496703205376
Nothing was truncated, and nothing overflowed. The result is exact, and the
same expression in Int would have wrapped around
without a word of complaint, because overflow is not an error in a machine
integer; it is what the machine does.
Use Integer for a value that can be large and whose
exact value matters: a factorial, a count of combinations, a sum of money in
the smallest unit. Use Int for the loop that computes
it.
Double
Double is a 64-bit IEEE 754 floating-point number, and it cannot represent every decimal fraction. One tenth is the standard example, and adding two of them shows the error:
main :: IO ()
main = print (0.1 + 0.2 :: Double)
0.30000000000000004
The number is not wrong; the binary representation of 0.1 and 0.2 is
approximate, and the sum of the approximations is the nearest
Double to 0.3, which prints as that long value. A
comparison 0.1 + 0.2 == 0.3 is therefore
False, and the fix is to compare with a tolerance or
to work in integers, never to add more decimals.
GHC also has Float, a 32-bit version with about seven
significant digits. Use it when a library demands it; otherwise
Double is the default and the better choice.
div and mod against quot and rem
Four functions do integer division, and they differ only for negative numbers. div rounds toward negative infinity and mod takes the sign of the divisor; quot rounds toward zero and rem takes the sign of the dividend:
main :: IO ()
main = do
print (7 `div` 2 :: Int, 7 `mod` 2 :: Int)
print (7 `div` (-2) :: Int, 7 `mod` (-2) :: Int)
print (7 `quot` (-2) :: Int, 7 `rem` (-2) :: Int)
(3,1)
(-4,-1)
(-3,1)
All three lines divide 7 by something, and the first agrees whichever pair
you use. On the negative divisor they disagree:
div gives -4, because
-4 * -2 = 8 is the multiple of the divisor below 7,
and mod gives -1, the
distance back to 7. quot gives
-3, because it drops the fraction toward zero, and
rem gives 1.
The identity that always holds is
div x y * y + mod x y == x, and the same with
quot and rem. Pick the pair
that matches the problem: div for a clock or a
bucket index, where the remainder must be non-negative;
quot when you mean truncation, as C does. With
positive numbers, which is most of the time, the two pairs agree.
fromIntegral and rounding
Haskell will not add an Int to a Double by itself: there is no silent conversion between numeric types, because each one has a different set of values. fromIntegral converts an integer of any type into any numeric type:
main :: IO ()
main = print (fromIntegral (3 :: Int) / 2 :: Double)
1.5
Without it, 3 / 2 would be a type error: the integer
literals have no Fractional instance, so
/ cannot be applied. The annotation on the result
says which type the conversion targets.
Going the other way is a choice, and the standard library makes you name it.
floor goes down, ceiling up,
truncate toward zero, and
round to the nearest value, with halves going to the
even neighbour:
main :: IO ()
main = print (round (2.5 :: Double) :: Int, floor (2.5 :: Double) :: Int)
(2,2)
round 2.5 is 2, not 3, because 2 is even. That is banker's rounding, and it is the default in GHC: over many values it does not drift upward the way "round half up" does. If a report needs the other rule, write it out with floor (x + 0.5) and say so in a comment.
Type defaulting
A numeric literal with nothing to fix its type is left as Num a => a, and a program has to print something concrete. GHC resolves the ambiguity by a set of defaulting rules and warns that it did, which is a warning worth reading once:
module Main (main) where
main :: IO ()
main = print (2 + 3)
ghc -Wall -O1 -o def Default.hs
./def
[1 of 2] Compiling Main ( Default.hs, Default.o )
Default.hs:4:8: warning: [GHC-18042] [-Wtype-defaults]
• Defaulting the type variable ‘a0’ to type ‘Integer’ in the following constraints
(Show a0) arising from a use of ‘print’ at Default.hs:4:8-12
(Num a0) arising from the literal ‘2’ at Default.hs:4:15
• In the expression: print (2 + 3)
In an equation for ‘main’: main = print (2 + 3)
|
4 | main = print (2 + 3)
| ^^^^^
[2 of 2] Linking def
5
The program works, and the value is an Integer because that is the first type the defaulting rules try. On a large number that is slower than an Int, and on a value that should fit in a machine word it can hide the difference. The fix is one annotation, and the warning goes away:
main :: IO ()
main = print (2 + 3 :: Int)
There is no output to show for the fixed file beyond the same 5: what changed is the type of the value, and the warning. This is the reason every program in this course carries annotations on its literals.
One program, every rule
module Main (main) where
main :: IO ()
main = do
print (7 `div` 2 :: Int, 7 `mod` 2 :: Int)
print (7 `div` (-2) :: Int, 7 `mod` (-2) :: Int)
print (7 `quot` (-2) :: Int, 7 `rem` (-2) :: Int)
print (2 ^ (100 :: Int) :: Integer)
print (maxBound :: Int)
print (0.1 + 0.2 :: Double)
print (fromIntegral (3 :: Int) / 2 :: Double)
print (round (2.5 :: Double) :: Int, floor (2.5 :: Double) :: Int)
ghc -Wall -O1 -o numbers Numbers.hs
./numbers
[1 of 2] Compiling Main ( Numbers.hs, Numbers.o )
[2 of 2] Linking numbers
(3,1)
(-4,-1)
(-3,1)
1267650600228229401496703205376
9223372036854775807
0.30000000000000004
1.5
(2,2)
Eight lines, no warnings, and every rule of the page in one place. The build was clean under -Wall and hlint said No hints, which is the state a file should reach before it is committed.
Exercises
- Print maxBound :: Int + 1. What is printed, and why is there no error?
- Compute 20! as an Integer and then as an Int. Compare the two answers and explain the difference.
- Print div (-7) 2, mod (-7) 2, quot (-7) 2 and rem (-7) 2. Check each against the identity div x y * y + mod x y == x.
- Print round (3.5 :: Double) :: Int and round (2.5 :: Double) :: Int. Both are halves; which one went up, and which went down?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which type holds a whole number of any size?
Answer
Integer
Integer has arbitrary precision, while Int is one machine word and wraps around.
-
What does
divdo with a negative divisor thatquotdoes not?Answer
It rounds toward negative infinity
div rounds down, so 7
div(-2) is -4, while quot truncates toward zero and gives -3. -
Why is
0.1 + 0.2not exactly 0.3 in a Double?Answer
Because 0.1 and 0.2 have no exact binary representation
A Double stores a binary fraction, and one tenth is not a finite binary fraction, so both values are approximations.
-
Why is
fromIntegralneeded before dividing an Int by a Double?Answer
Because Haskell does not convert between numeric types silently
Each numeric type has its own set of values, so the conversion is explicit; fromIntegral does not round, it converts exactly.
-
What does
round (2.5 :: Double) :: Intgive?Answer
2
round sends a half to the even neighbour, and 2 is even, so the result is 2.
-
What does this program print? ``
module Main (main) where main :: IO () main = print (7mod2 :: Int)``Answer
1
Seven divided by two is three with a remainder of one, and mod returns the remainder.
-
What does this program print? ``
module Main (main) where main :: IO () main = print (7div(-2) :: Int)``Answer
-4
div rounds toward negative infinity, and -4 is the multiple of -2 below 7.
-
What does this program print?
module Main (main) where main :: IO () main = print (2 ^ (10 :: Int) :: Integer)Answer
1024
Two to the tenth is 1024, and Integer holds it exactly.
-
A file has
main = print (2 + 3)and is built with -Wall. Which warning appears?Answer
warning: [GHC-18042] [-Wtype-defaults]
The literals have no fixed type, so the compiler defaults the variable to Integer and reports it as GHC-18042.
-
What does
maxBound :: Intprint on a 64-bit machine?Answer
9223372036854775807
An Int is one 64-bit word here, so the largest value is 2^63 - 1, which is that 19-digit number.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev