liveBook · Manning

Integer

As explained in Julia as a Second Language

An integer is a whole number with no fractional component, including positive, negative, and zero values.

An integer is a whole number: positive, negative, or zero, with no fractional part. In Julia, integers are useful for calculator-style arithmetic, but their types also determine which values can be stored. section 2.0 section 2.3

Choose a type only when its range matters:

  • Signed types include Int8, Int16, Int32, Int64, and Int128; unsigned versions start with U, such as UInt8. The suffix gives the bit length. section 2.3
  • Use typemin and typemax to check limits. Int8 runs from -128 to 127, and typeof(1) returns Int64. section 2.3
  • A constructor such as Int8(42) creates a specified type; converting a value outside its range, such as UInt8(256), raises an error. section 2.3

Keep the quotient and remainder

In the Julia REPL, 5/2 gives 2.5, while div(5, 2) gives the integer quotient 2 and rem(5, 2) gives the remainder 1. You can also write the remainder as 5%2. section 2.4.1

Arithmetic does not automatically widen an Int8 result when it overflows. Start with default integer sizes and prefer signed integers unless your problem or performance needs call for another type. For values beyond Int128, BigInt offers arbitrary size at a memory and performance cost. section 2.3 section 2.6

A whole-number result is not always an integer-typed result: floor(3.6) gives 3.0. Use round(Int64, 3.5) to obtain an integer-typed 4. The same value can also be written in different formats: Int(0b1101) and Int(0x0d) both give 13. section 2.5.4 section 2.3.1

How Julia as a Second Language covers it

How deep it goes
Explains type ranges, conversion, overflow, division, rounding, and formats; defers broader number-type and overflow background.
Examples
Demonstrates typeof(1), Int8(42), UInt8(256), div(5, 2), rem(5, 2), and integer rounding in the REPL.
What it assumes
Assumes basic arithmetic, introduces Julia’s number types through calculator use, and points unfamiliar readers to background on number types and overflow.
What you can do after
Apply it via Julia’s type constructors, range-checking functions, div, rem, and rounding functions to choose suitable integer results.
How it connects
Connects integers to floating-point numbers, signed and unsigned types, integer overflow, division, and rounding.

Also explained in

Compare how 15 books explain integer →

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.