liveBook · Manning

Unchecked-subtract

As explained in Clojure: The Essential Reference

unchecked-subtract is a Clojure function that subtracts one numeric argument from another without checking for integer overflow.

unchecked-subtract subtracts one numeric argument from another without checking for integer overflow. It accepts exactly two numeric arguments and returns the subtraction result, with the result type determined by numeric type contagion. section 5.13

Use unchecked-subtract when two-argument subtraction should preserve unchecked integer arithmetic:

  • (unchecked-subtract 2 38) ;; -36 section 5.13
  • Unchecked arithmetic can wrap at integer boundaries rather than signaling overflow; this behavior is useful in hashing and related integer-overflow algorithms. section 5.13

Unchecked arithmetic in a hash function

A string hash can combine multiplication and subtraction-like unchecked arithmetic while allowing fixed-width integer results to wrap. The related example uses unchecked-add and unchecked-multiply to produce long-valued hashes, including negative results after overflow. section 5.13

(unchecked-add 1 2)
;; 3

(unchecked-subtract 2 38)
;; -36

(unchecked-multiply 10 3)
;; 30

(unchecked-inc 100)
;; 101

(unchecked-dec 12)
;; 11

(unchecked-negate 1)
;; -1

Browse concise evaluations of unchecked arithmetic, including unchecked-subtract. section 5.13

How Clojure: The Essential Reference covers it

How deep it goes
Defines arity, numeric inputs, result typing, and overflow behavior, then demonstrates ordinary and hashing-oriented use.
Examples
Shows a simple subtraction and a related long-valued string-hashing function built from unchecked arithmetic.
What it assumes
Assumes familiarity with Clojure numeric operators and introduces restricted arity, overflow removal, and type contagion.
What you can do after
Apply via Clojure with exactly two numeric arguments and anticipate unchecked integer overflow behavior.
How it connects
Connects unchecked-subtract with unchecked-add, unchecked-multiply, integer overflow, hashing, and numeric type promotion.

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.