liveBook · Manning

Numerical overflow

As explained in Clojure: The Essential Reference

Integer overflow is the condition in which an integer computation exceeds the representable range and wraps or otherwise produces a bounded-width result.

Numerical overflow matters when integer arithmetic crosses the range its type can represent. In Clojure, `+`, `-`, and `*` throw an `ArithmeticException` when a result with long operands exceeds the long range. Choose a different operator when a calculation must instead grow beyond that range or deliberately wrap around it. section 5.5 section 5.1

Three operator choices give different behavior:

  • Use ordinary `+`, `-`, and `*` to detect long overflow with an exception. section 5.5
  • Use primed operators such as `*'` when the result should promote to arbitrary precision rather than overflow. section 5.1 section 5.5
  • Use `unchecked-add`, `unchecked-subtract`, `unchecked-multiply`, or their increment, decrement, and negate counterparts when overflow checks should be omitted. Addition, subtraction, and multiplication take exactly two arguments; the other three take one. section 5.13

Crossing the long boundary

`(unchecked-inc Long/MAX_VALUE)` yields `-9223372036854775808`, or `Long/MIN_VALUE`; `(unchecked-dec Long/MIN_VALUE)` yields `9223372036854775807`. The signed 64-bit value rolls from one end of its range to the other. section 5.13 section 5.1

(take 7 (iterate #(* % %) 2))                                 ; #1
;; ArithmeticException integer overflow

(take 7 (iterate #(*' % %) 2))
;; (2 4 16 256 65536 4294967296 18446744073709551616N)        ; #2

Repeated squaring makes the choice visible: ordinary `*` raises an overflow exception, while `*'` produces a larger integer. section 5.5

Wraparound can be intentional. A string-hashing example combines `unchecked-multiply` and `unchecked-add` to keep accumulating a bounded-width hash; a negative result is possible. Division is different: `/` can produce a ratio, and division by zero raises its own exception. section 5.13 section 5.5 section 5.1

How Clojure: The Essential Reference covers it

How deep it goes
Explains long and int boundary rollover, unchecked-operator arities, and checked exceptions, with bit layouts rather than deeper runtime internals.
Examples
Uses boundary increments, repeated squaring, and a string hash built with unchecked multiplication and addition.
What it assumes
Assumes basic Clojure arithmetic and introduces signed integer boundaries and unchecked-operator contracts through examples.
What you can do after
Apply it via Clojure arithmetic operators to select an exception, a promoted integer, or intentional wraparound.
How it connects
Connects numerical overflow to two’s complement, arbitrary-precision arithmetic, type promotion, ratios, and hashing.

Also explained in

Compare how 7 books explain numerical overflow →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.