TExcelTAD
Home › Julia › Julia Integers & Floats

Julia Integers & Floats

Julia has a rich set of built-in number types. The two you'll use most are Int64 (integers) and Float64 (floating-point numbers), but many more exist for when memory size or precision matters.

Integer types

Type Signed? Bits Range
Int8 / UInt8 yes / no 8 −128…127 / 0…255
Int16 / UInt16 yes / no 16 −32768…32767 / 0…65535
Int32 / UInt32 yes / no 32 about ±2.1 billion
Int64 / UInt64 yes / no 64 about ±9.2 × 10¹⁸
Int128 / UInt128 yes / no 128 very large
BigInt yes any limited only by memory
Bool — 8 false, true

Int is an alias for the native word size (Int64 on 64-bit machines).

julia · try it yourself
println(typeof(42))
println(Int === Int64)
println(typemin(Int8), " to ", typemax(Int8))
println(typemax(Int64))
println(typeof(0xff), " ", 0xff)     # hex literals are unsigned
println(0b1010, " ", 0o17)           # binary and octal
Int64
true
-128 to 127
9223372036854775807
UInt8 255
10 15

Overflow wraps around

Fixed-size integer arithmetic is fast because it maps straight to CPU instructions — and like the CPU, it silently wraps around on overflow. When you need huge numbers use BigInt (or big(...)):

julia · try it yourself
m = typemax(Int64)
println(m + 1)                  # wraps to the minimum
println(big(m) + 1)             # BigInt never overflows
println(factorial(big(25)))
println(2^64, " vs ", big(2)^64)
-9223372036854775808
9223372036854775808
15511210043330985984000000
0 vs 18446744073709551616

Floating-point types

Type Bits Approx. decimal digits
Float16 16 3
Float32 32 7
Float64 64 15–16
BigFloat any (default 256) ~77

A literal with a decimal point or exponent is a Float64. Append f0 for a Float32.

julia · try it yourself
println(typeof(1.0), " ", typeof(1e3), " ", 1e3)
println(typeof(2.5f0), " ", 2.5f0)
println(0.1 + 0.2)
println(0.1 + 0.2 == 0.3)
println(isapprox(0.1 + 0.2, 0.3))
println(eps(1.0))
Float64 Float64 1000.0
Float32 2.5
0.30000000000000004
false
true
2.220446049250313e-16

0.1 + 0.2 isn't exactly 0.3 because these decimals can't be represented exactly in binary. Compare floats with isapprox (or the ≈ operator, typed \approx) instead of ==. eps(1.0) is the gap between 1.0 and the next representable Float64.

Special floating-point values

julia · try it yourself
println(1 / 0)
println(-1 / 0)
println(0 / 0)
println(isnan(0 / 0), " ", isinf(1 / 0))
println(NaN == NaN)
println(typemax(Float64), " ", floatmax(Float64))
Inf
-Inf
NaN
true true
false
Inf 1.7976931348623157e308

Note that NaN is not equal to anything, not even itself — use isnan to test for it.

Converting between number types

julia · try it yourself
println(Float64(7))          # integer to float
println(Int(3.0))            # exact float to integer
println(round(Int, 3.7))     # rounding to an integer type
println(trunc(Int, -3.7), " ", floor(Int, -3.7), " ", ceil(Int, -3.7))
println(parse(Int, "123") + 1)
try
    Int(3.5)
catch e
    println(sprint(showerror, e))
end
7.0
3
4
-3 -4 -3
124
InexactError: Int64(3.5)

Int(3.5) throws an InexactError because the value can't be represented exactly; use round, floor, ceil or trunc to say how you want to lose the fraction.

Test yourself

What does typemax(Int64) + 1 evaluate to?

  1. An OverflowError
  2. typemin(Int64), because integers wrap around
  3. Inf
  4. A BigInt

What is the type of the literal 2.5f0?

  1. Float64
  2. Float32
  3. Float16
  4. BigFloat

What does Int(2.5) do?

  1. Returns 2
  2. Returns 3
  3. Throws an InexactError
  4. Returns 2.5