Not getting integer overflow in Clojure?
Asked Answered
N

2

7

I am running Clojure 1.3.0 with La Clojure in IntelliJ IDEA while reading The Joy Of Clojure, and on section 4.1.3 (page 64), the authors demonstrate integer overflow with the following code:

(+ Integer/MAX_VALUE Integer/MAX_VALUE)
;=> java.lang.ArithmeticException: integer overflow

However, when I try it out on the REPL, I get instead

user=> (+ Integer/MAX_VALUE Integer/MAX_VALUE)
4294967294

user=> Integer/MAX_VALUE
2147483647

What is happening here? Why are my integers being added correctly instead of overflowing?

Novelia answered 29/12, 2011 at 4:44 Comment(3)
I got that exception, are you sure..?Chemulpo
@KugathasanAbimaran I copy-pasted all that, so I am pretty sure... :ONovelia
@wrongusernaem : I'm using 1.2.0 and get that exception. Seems like book [that edition] uses 1.2.0. And gertalot have the solution.Chemulpo
A
8

(edited) Clojure (at least 1.3.0) automatically converts the integer to a long if necessary. For more details about automatic boxing, promotion and primitive numeric type support in Clojure 1.3.0 check the Documentation for Clojure 1.3.0 Numerics.

The reason you don't get an overflow is because Clojure automatically converts the integer to a long, so (+ Integer/MAX_VALUE Integer/MAX_VALUE) is adding two longs:

user> (type Integer/MAX_VALUE)
java.lang.Long
Adduct answered 29/12, 2011 at 5:51 Comment(0)
S
8

In Clojure all primative integers are primative longs and the documentation on the numerics page is refering to that. in 1.3 you just need bigger numbers to get your overflow.

user=> (+ Long/MAX_VALUE Long/MAX_VALUE)
ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow (Numbers.java:1374)

This important part is that older versions of clojure would have promoted to a big int automatically and it was decided that the cost of this was not worth the extremely rare cases where it is desired. if you really want promoting math use +'

user=> (+' Long/MAX_VALUE Long/MAX_VALUE)
18446744073709551614N
Smaragdite answered 30/12, 2011 at 2:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.