ClojureDocs

Nav

Namespaces

unchecked-subtract-int

clojure.core

Available since 1.0 (source)
  • (unchecked-subtract-int x y)
Returns the difference of x and y, both int.
Note - uses a primitive operator subject to overflow.
1 Example
;; Subtracting two int-range Longs works
(unchecked-subtract-int 1 1)
;;=> 0

;; Subtracting two int-range BigInts works
(unchecked-subtract-int 1N 1N)
;;=> 0

;; Doubles are truncated
(unchecked-subtract-int 1 1.9)
;;=> 0

;; BigDecimals are truncated
(unchecked-subtract-int 1 1.9M)
;;=> 0

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

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

;; Fails for Longs outside of int range
(unchecked-subtract-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-subtract-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-subtract-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-subtract-int 2147483648.0M 0)
;;=> IllegalArgumentException Value out of range for int: 2147483648  clojure.lang.RT.intCast (RT.java:1205)
See Also

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

Added by svenschoenung
0 Notes
No notes for unchecked-subtract-int