I can't answer whether Clojure should do that.

It is doing it because unchecked-multiply falls back to normal multiply,
i.e. *, unless both of its arguments are primitive long values.  Even boxed
Long values cause it to fall back to the behavior of *.  The same goes for
unchecked-add and unchecked-subtract.


user=> (unchecked-multiply 2432902008176640000 21)
-4249290049419214848
user=> (unchecked-multiply 2432902008176640000 (Long. 21))

ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow
(Numbers.java:1501)

Andy


On Fri, Oct 23, 2015 at 1:54 PM, Garrett Rowe <gmrowe1...@gmail.com> wrote:

> Should `unchecked-multiply` throw an exception in this case? (see also:
> http://stackoverflow.com/questions/33306984/why-does-my-hash-function-fail-with-arithmeticexception-integer-overflow-even/33308956#33308956
> )
>
> user> (reduce unchecked-multiply (range 1 22))
> ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow
> (Numbers.java:1388)
>
> The loop-recur version also fails, unless you use a type hint:
>
> user> (loop [n 1
>              r (range 1 22)]
>         (if-let [a (first r)]
>           (recur (unchecked-multiply n a) (next r))
>           n))
> ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow
> (Numbers.java:1388)
>
> user> (loop [n 1
>              r (range 1 22)]
>         (if-let [^long a (first r)]
>           (recur (unchecked-multiply n a) (next r))
>           n))
> -4249290049419214848
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to