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
