unchecked-add-int

added
1.0

ns
clojure.core

type
function

(unchecked-add-int x y)

Returns the sum of x and y, both int.
Note - uses a primitive operator subject to overflow.

                ;; Adding two int-range Longs works
(unchecked-add-int 1 1)
;;=> 2

;; Adding two int-range BigInts works
(unchecked-add-int 1N 1N)
;;=> 2

;; Doubles are truncated
(unchecked-add-int 1 1.9)
;;=> 2

;; BigDecimals are truncated
(unchecked-add-int 1 1.9M)
;;=> 2

;; Uncaught integer overflow
(unchecked-add-int Integer/MAX_VALUE 1)
;;=> -2147483648

;; Uncaught integer underflow
(unchecked-add-int Integer/MIN_VALUE -1)
;;=> 2147483647

;; Fails for Longs outside of int range
(unchecked-add-int 2147483648 0)
;;=> IllegalArgumentException Value out of range for int: 2147483648  clojure.lang.RT.intCast (RT.java:1205)

;; Fails for BigInts outside of int range
(unchecked-add-int 2147483648N 0)
;;=> IllegalArgumentException Value out of range for int: 2147483648  clojure.lang.RT.intCast (RT.java:1205)

;; Fails for Doubles outside of int range
(unchecked-add-int 2147483648.0 0)
;;=> IllegalArgumentException Value out of range for int: 2147483648  clojure.lang.RT.intCast (RT.java:1205)

;; Fails for BigDecimals outside of int range
(unchecked-add-int 2147483648.0M 0)
;;=> IllegalArgumentException Value out of range for int: 2147483648  clojure.lang.RT.intCast (RT.java:1205)