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?

3
  • I got that exception, are you sure..? Commented Dec 29, 2011 at 5:45
  • @KugathasanAbimaran I copy-pasted all that, so I am pretty sure... :O Commented Dec 29, 2011 at 6:03
  • @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. Commented Dec 29, 2011 at 6:19

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.