liveBook · Manning

Unchecked-negate-int

As explained in Clojure: The Essential Reference

unchecked-negate-int is a Clojure function that negates a numeric value as a primitive 32-bit signed integer without overflow checking.

unchecked-negate-int negates a numeric value as a primitive signed 32-bit integer. It casts the input to int, returns an int, and allows arithmetic to overflow instead of checking the result. section 5.14 section 5.4

Use the function with these expectations:

  • Accepts one numeric argument in the int precision range, from Integer/MIN_VALUE through Integer/MAX_VALUE. section 5.14
  • Returns the primitive int result of negation rather than promoting the result to a wider numeric type. section 5.14
  • Overflows when the negated result reaches the limits of signed 32-bit int precision. section 5.14
  • Fits integer-focused code such as hashing and arithmetic that deliberately uses int values. section 5.14

Negating an int value

(unchecked-negate-int 7) ;; -7 (unchecked-negate-int -7) ;; 7 (type (unchecked-negate-int 7)) ;; java.lang.Integer The function belongs beside unchecked-inc-int, unchecked-dec-int, and the other unchecked int operators when an integer algorithm must retain int arithmetic. 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 complete unchecked-int operator family, including unchecked-negate-int and its neighboring arithmetic operations. section 5.14

How Clojure: The Essential Reference covers it

How deep it goes
Defines arity, numeric range, primitive return type, and overflow behavior, but does not develop negation internals separately.
Examples
Uses an unchecked-int operator listing and an integer-based image-processing example to situate fixed-width arithmetic.
What you can do after
Apply it via Clojure when an integer calculation must return int values and accept unchecked overflow.
How it connects
Connects primitive types, integer arithmetic, overflow, hashing algorithms, and unchecked arithmetic operators.

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.