liveBook · Manning

Unchecked-dec

As explained in Clojure: The Essential Reference

unchecked-dec-int is a Clojure function that decrements using Java Integer arithmetic without implicit promotion to Long or overflow checking.

`unchecked-dec-int` decrements one numeric argument using 32-bit integer arithmetic. It casts the argument to a primitive `int`, returns an `int` result, and does not check for overflow. Use it when an algorithm needs `int` values and rollover is acceptable; the stated input contract limits values to the `int` range. section 5.14

Decrement and rollover

`(unchecked-dec-int 12)` returns `11`. At the lower boundary, `(unchecked-dec-int Integer/MIN_VALUE)` wraps to `Integer/MAX_VALUE` rather than producing a value below the 32-bit range. section 5.14 section 5.6

The `-int` suffix matters: `unchecked-dec` also permits rollover, but its result type follows numeric promotion rather than staying `int`. In an integer-coordinate line-drawing loop, `unchecked-dec-int` updates the vertical coordinate when the line moves downward. Prefer an ordinary decrement when unchecked rollover is not wanted. section 5.13 section 5.14

(defn to-points [x1 y1 x2 y2]
  (let [[^int x1 ^int y1 ^int x2 ^int y2] (adjust x1 x2 y1 y2)    ; #1
        dx (unchecked-subtract-int x2 x1)
        dy (Math/abs (unchecked-subtract-int y1 y2))]
    (map (swap (steep? x1 x2 y1 y2))
         (loop [x x1                                              ; #2
                y y1
                error (unchecked-divide-int dx 2)
                points []]
           (if (> x x2)
             points
             (if (< error dy)
               (recur (unchecked-inc-int x)                       ; #3
                      (if (< y1 y2)
                        (unchecked-inc-int y)
                        (unchecked-dec-int y))
                      (unchecked-add-int error
                        (unchecked-subtract-int dx dy))
                      (conj points [x y]))
               (recur (unchecked-inc-int x)
                      y
                      (unchecked-subtract-int error dy)
                      (conj points [x y]))))))))

The point-generation loop uses `unchecked-dec-int` for a downward step, alongside other `-int` operators for coordinate arithmetic. section 5.14

How Clojure: The Essential Reference covers it

How deep it goes
Defines unary input, int output, and rollover, then shows algorithmic use without detailing implementation internals.
Examples
Shows downward coordinate adjustment in a point-generation loop and contrasts an equivalent long-based loop.
What it assumes
Assumes basic Clojure arithmetic while explaining primitive int casting and 32-bit overflow.
What you can do after
Apply it via Clojure’s standard library to decrement int coordinates when rollover is acceptable.
How it connects
Connects unchecked-dec-int to unchecked-dec, numeric promotion, integer overflow, and line drawing.

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.