liveBook · Manning

Unchecked-divide-int

As explained in Clojure: The Essential Reference

unchecked-divide-int is a Clojure function that divides one numeric value by another as primitive 32-bit signed integers without overflow checking.

unchecked-divide-int divides two numeric values as primitive 32-bit signed integers and returns an int. It belongs to Clojure’s unchecked-*-int family, which is useful when ordinary long-based arithmetic is unsuitable, such as integer algorithms and hashing. section 5.14

Its contract is:

  • Takes two numeric arguments within the int range, from Integer/MIN_VALUE through Integer/MAX_VALUE. section 5.14
  • Casts arguments to primitive int values and returns the primitive int result of the division. section 5.14
  • Uses unchecked 32-bit arithmetic, so results overflow when they reach the limits of signed int precision. section 5.14
  • Differs from unchecked-divide by retaining int precision rather than promoting the result to accommodate greater precision. section 5.14 section 5.4

Integer line drawing

A line-drawing routine uses `(unchecked-divide-int dx 2)` to initialize its integer error term, alongside unchecked subtraction and increment operations. This keeps coordinate and error calculations in the int-based representation used by a multidimensional Java image array. section 5.14

Listing 5.11. → Integer math, Overflowing , More Use Cases
(unchecked-add-int [x y])
(unchecked-subtract-int [x y])
(unchecked-multiply-int [x y])
(unchecked-divide-int [x y])
(unchecked-inc-int [x])
(unchecked-dec-int [x])
(unchecked-negate-int [x])
(unchecked-remainder-int [x y])

Browse the compact listing of the eight unchecked int operators, including unchecked-divide-int. section 5.14

How Clojure: The Essential Reference covers it

How deep it goes
Defines arity, numeric range, primitive int conversion, return type, and overflow behavior, then demonstrates algorithmic use.
Examples
Uses an integer line-drawing routine whose error term begins with unchecked division of a coordinate difference by two.
What it assumes
Assumes numeric arguments and teaches the int-range, primitive-result, and unchecked-overflow contract.
What you can do after
Apply it in fixed-width integer algorithms that require primitive int results and explicitly tolerate unchecked overflow.
How it connects
Connects primitive int arithmetic with unchecked operators, hashing algorithms, and integer-based image processing.

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.