liveBook · Manning

Unchecked-subtract-int

As explained in Clojure: The Essential Reference

unchecked-subtract-int is a Clojure function that subtracts one numeric value from another as primitive 32-bit signed integers without overflow checking.

unchecked-subtract-int subtracts its second numeric argument from its first after converting both to primitive 32-bit signed integers. It returns an int and deliberately allows overflow, making it useful when fixed-width integer arithmetic matters more than Clojure’s default long-based behavior. section 5.14 section 5.4

Use the function with these expectations:

  • Pass two numeric arguments within the signed 32-bit integer range, from Integer/MIN_VALUE through Integer/MAX_VALUE. section 5.14
  • Receive a primitive int result rather than a promoted long or higher-precision numeric value. section 5.14
  • Expect rollover when subtraction reaches the limits of signed 32-bit precision; the operation performs no overflow checking. section 5.14

Integer arithmetic in line generation

A line-generation routine uses unchecked-subtract-int for coordinate differences: `(unchecked-subtract-int y1 y2)` compares vertical distance with horizontal distance, while `(unchecked-subtract-int x2 x1)` computes `dx`. The resulting integer values drive an iterative point-producing loop, which can then paint a line into a two-dimensional integer image array.

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, primitive-int conversion, return type, and overflow behavior, then demonstrates use inside an integer line algorithm.
Examples
Uses coordinate differences in line generation and paints the generated points into a two-dimensional integer image.
What it assumes
Requires numeric arguments within signed 32-bit integer precision and awareness that unchecked operations can overflow.
What you can do after
Apply it in Clojure code that requires primitive 32-bit subtraction with deliberate rollover instead of overflow checking.
How it connects
Connects primitive integers with integer overflow, hashing algorithms, coordinate geometry, and multi-dimensional arrays.

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.