liveBook · Manning

Number type

As explained in Julia as a Second Language

A number is a mathematical value representing a quantity or position in a numerical system.

A quantity may arrive as text, become a coordinate, or take part in arithmetic. In Julia, its number type determines which values it can represent. Integer types include signed Int8, Int16, Int32, Int64, and Int128, plus unsigned counterparts such as UInt8. The suffix gives the bit length; floating-point types are another option. section 2.3 section 11.2.2

A few REPL checks make the choice concrete:

  • Use typeof(1) to inspect a literal; the shown result is Int64. Use Int8(42) to request a signed 8-bit value. section 2.3
  • Use typemin(Int8) and typemax(Int8) to find its limits: -128 and 127. A conversion such as UInt8(256) produces an error because the value does not fit. section 2.3
  • Do not expect arithmetic on two Int8 values to widen the result automatically: the result remains Int8 and can overflow. BigInt handles integers beyond Int128, at a memory and performance cost. section 2.3

Number is also the root of Julia’s number-type hierarchy. From Int64, following supertypes leads through Signed, Integer, and Real to Number. Going downward, Number has Complex and Real subtypes. Use supertype and subtypes to explore the hierarchy rather than treating every numeric type as an unrelated choice. section 7.2

Turn input into the intended number type

Input from a keyboard or file commonly arrives as a string. parse(Int16, "42") produces an Int16; parse(Float64, "0.42") produces a floating-point value. The optional base changes how digits are read: parse(Int, "101", base=2) gives 5, while the default base 10 gives 101. Conversion back to text works too: string(5, base=2) gives "101". section 11.2.2

Number becomes especially useful when defining reusable types. A vector whose two coordinate fields share a type parameter, T, rejects mixed Int64 and Float64 arguments when Julia must infer T. Writing T<:Number also excludes nonnumeric coordinates, such as characters. If conversion is intended, specifying the type parameter explicitly can let both fields take that chosen type. section 18.3

Listing 18.7 Point and Vec2D defined so the type parameter must be a number type
import Base: +, -
 
struct Point{T<:Number}
    x::T
    y::T
end
 
struct Vec2D{T<:Number}
    Δx::T
    Δy::T
end
 
"Adding vector `v` to point `p` creates a new point"
function +(p::Point{T}, v::Vec2D{T}) where T
    Point(p.x + v.Δx, p.y + v.Δy)
end
 
"Subtracting two points gives a vector"
function -(p::Point{T}, q::Point{T}) where T
    Vec2D(p.x - q.x, p.y - q.y)
end

The Point and Vec2D definitions show T<:Number on both coordinate types, then use matching parameters for point-and-vector operations. section 18.3

How Julia as a Second Language covers it

How deep it goes
Works through type inspection, integer bounds, overflow, parsing, hierarchy traversal, and parametric constraints; defers background on bit lengths and overflow.
Examples
Shows REPL checks for Int8 bounds, base-2 parsing, and Point and Vec2D definitions that reject nonnumeric coordinates.
What it assumes
Assumes basic Julia expressions and directs readers unfamiliar with bit lengths or integer overflow to separate background.
What you can do after
Operate/configure Julia number types by checking limits, parsing strings into chosen types, and constraining numeric fields.
How it connects
Connects number types to type hierarchies, string parsing, integer overflow, and parametric types.

Also explained in

Compare how 6 books explain number type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.