On Sat, Apr 4, 2015 at 8:52 AM, Paul Roush <pro...@gmail.com> wrote:

> I have 2 questions related to bigints.
>
> 1) I've read things suggesting Clojure auto-converts between int, long,
> and bigint to prevent overflow.  That isn't the behavior I've experienced.
> Did something change from an older release of Clojure to more recent ones?
>

Clojure 1.2 and earlier auto-promoted, if I recall correctly, but that has
not been the case since Clojure 1.3, released in Sep 2011.  You may have
found something published before then.

Clojure 1.3 and later do give a bigint result for integer arithmetic if any
of the arguments are bigints, e.g. (+ 1N 1) => 2N


>
> 2) I found myself wanting to write a function that would use bigint math
> vs not depending on whether I passed a bigint in as arg.  Internally I
> wanted to "seed" a reduce call with either 1 or 1N as appropriate.  So I
> wanted to write something like this (except I'd like it to work):
>
> (defn foo [n]
>   (cast (type n) 1))
>
> ; (foo 3) => 1
> ; (foo 42N) => 1N
>
> The following works, but I'd like to understand if I can avoid the
> conditional:
>
> (defn foo [n]
>   (if (= clojure.lang.BigInt (type n)) 1N 1))
>

More commonly I have seen (instance? clojure.lang.BigInt n) rather than (=
clojure.lang.BigInt (type n)), but I do not know any way to avoid the
conditional here.

I would ask that you consider whether you really do need this check,
though, since if your argument is BigInt and is combined with any
arithmetic operation with a local long value in the function, the result
will be BigInt.

Andy

-- 
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