liveBook · Manning

Unchecked-remainder-int

As explained in Clojure: The Essential Reference

unchecked-remainder-int is a Clojure function that computes the remainder of two numeric values as primitive 32-bit signed integers without overflow checking.

unchecked-remainder-int computes the remainder of two numeric values using 32-bit signed int arithmetic. It casts inputs to int, returns an int result, and does not check for overflow or promote the calculation to long. This makes it useful for integer-heavy code such as hashing, but only when the int range is appropriate. section 5.14 section 5.7

Use the function with these constraints in mind:

  • Pass exactly two numeric arguments whose values lie between Integer/MIN_VALUE and Integer/MAX_VALUE. section 5.14
  • Expect 32-bit signed integer behavior, including overflow at int precision limits rather than automatic promotion. section 5.14
  • Prefer rem for ordinary remainder calculations; reserve unchecked-remainder-int for proven performance-sensitive or intentionally overflowing integer work. section 5.7

Comparing remainder implementations

For small int-valued inputs, a benchmark compares the ordinary and unchecked forms: (let [num (int 100) div (int 10)] (quick-bench (unchecked-remainder-int num div))) ;; Execution time mean : 8.169385 ns (let [num 100 div 10] (quick-bench (rem num div))) ;; Execution time mean : 15.733921 ns The unchecked form is faster in this measurement, but the improvement is small in absolute terms and should not justify its use without evidence that other costs do not dominate. section 5.7

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, int-range behavior, overflow, promotion, and benchmarking, but does not detail remainder edge cases beyond performance.
Examples
Uses a Criterium benchmark comparing rem and unchecked-remainder-int on int-valued inputs.
What it assumes
Assumes familiarity with Clojure numeric operators and explains how int casting, promotion, overflow, and remainder alternatives differ.
What you can do after
Apply it via Clojure's numeric API when measured, range-safe int arithmetic matters more than checked promotion.
How it connects
Connects unchecked-remainder-int with rem, mod, quot, integer arithmetic, hashing, overflow, and primitive types.

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.