More Clojure Math Functions

Here are some more Clojure math functions.

ted.core=> (def long-max-minus-1 (- Long/MAX_VALUE 1) )
#'ted.core/long-max-minus-1
ted.core=> long-max-minus-1
9223372036854775806
ted.core=>  (+ long-max-minus-1 2)

ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow (Numbers.java:1501)
ted.core=>  (+' long-max-minus-1 2)
9223372036854775808N
ted.core=>  (unchecked-add long-max-minus-1 2)

ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow (Numbers.java:1501)
ted.core=>  (unchecked-add long-max-minus-1 1)
9223372036854775807
ted.core=>  (+ long-max-minus-1 1)
9223372036854775807
;; so unchecked-add is just like +

ted.core=> (unchecked-inc 5)
6
ted.core=> (unchecked-inc long-max-minus-1)
9223372036854775807
ted.core=> (unchecked-inc (unchecked-inc long-max-minus-1))

ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow (Numbers.java:1501)
ted.core=> (inc (inc long-max-minus-1))

ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow (Numbers.java:1501)
ted.core=> (inc' (inc long-max-minus-1))
9223372036854775808N

;; I am guessing unchecked-dec is just like regular dec
ted.core=> (def long-min-plus-1 (+ Long/MIN_VALUE 1))
#'ted.core/long-min-plus-1
ted.core=> long-min-plus-1
-9223372036854775807
ted.core=> (dec long-min-plus-1)
-9223372036854775808
ted.core=> (dec (dec long-min-plus-1))

ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow (Numbers.java:1501)
ted.core=> (unchecked-dec long-min-plus-1)
-9223372036854775808
ted.core=> (unchecked-dec (unchecked-dec long-min-plus-1))

ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow (Numbers.java:1501)
ted.core=> (dec' (unchecked-dec long-min-plus-1))
-9223372036854775809N

ted.core=> long-min-plus-1
-9223372036854775807
ted.core=> Long/MIN_VALUE
-9223372036854775808

ted.core=> (-' long-min-plus-1 1)
-9223372036854775808
ted.core=> (- long-min-plus-1 1)
-9223372036854775808
ted.core=> (unchecked-subtract long-min-plus-1 1)
-9223372036854775808
ted.core=> (unchecked-subtract (unchecked-subtract long-min-plus-1 1))
CompilerException clojure.lang.ArityException: Wrong number of args (1) passed to: core/unchecked-subtract--inliner--4555, compiling:(/tmp/form-init7761027828321026718.clj:1:1) 

ted.core=> (-' (unchecked-subtract long-min-plus-1 1))
9223372036854775808N

ted.core=> (* 3037000500 3037000500)

ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow (Numbers.java:1501)
ted.core=> (unchecked-multiply 3037000500 3037000500)
-9223372036709301616

ted.core=> (set! *unchecked-math* true)
true
ted.core=> (* 3037000500 3037000500)
-9223372036709301616

 

You’re welcome.

There are 250 words in this article.