Skip to content

Commit

Permalink
CLJ-1193: Make bigint and biginteger work on all floats and doubles
Browse files Browse the repository at this point in the history
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information
jafingerhut authored and stuarthalloway committed Aug 14, 2013
1 parent 98437ff commit 93e4911
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/clj/clojure/core.clj
Expand Up @@ -3278,6 +3278,7 @@
(instance? clojure.lang.BigInt x) x
(instance? BigInteger x) (clojure.lang.BigInt/fromBigInteger x)
(decimal? x) (bigint (.toBigInteger ^BigDecimal x))
(float? x) (bigint (. BigDecimal valueOf (double x)))
(ratio? x) (bigint (.bigIntegerValue ^clojure.lang.Ratio x))
(number? x) (clojure.lang.BigInt/valueOf (long x))
:else (bigint (BigInteger. x))))
Expand All @@ -3291,6 +3292,7 @@
(instance? BigInteger x) x
(instance? clojure.lang.BigInt x) (.toBigInteger ^clojure.lang.BigInt x)
(decimal? x) (.toBigInteger ^BigDecimal x)
(float? x) (.toBigInteger (. BigDecimal valueOf (double x)))
(ratio? x) (.bigIntegerValue ^clojure.lang.Ratio x)
(number? x) (BigInteger/valueOf (long x))
:else (BigInteger. x)))
Expand Down
18 changes: 14 additions & 4 deletions test/clojure/test_clojure/numbers.clj
Expand Up @@ -35,10 +35,20 @@
(not (float? v)))))

(deftest BigInteger-conversions
(are [x] (biginteger x)
Long/MAX_VALUE
13178456923875639284562345789M
13178456923875639284562345789N))
(doseq [coerce-fn [bigint biginteger]]
(doseq [v (map coerce-fn [ Long/MAX_VALUE
13178456923875639284562345789M
13178456923875639284562345789N
Float/MAX_VALUE
(- Float/MAX_VALUE)
Double/MAX_VALUE
(- Double/MAX_VALUE)
(* 2 (bigdec Double/MAX_VALUE)) ])]
(are [x] (true? x)
(integer? v)
(number? v)
(not (decimal? v))
(not (float? v))))))

(deftest unchecked-cast-num-obj
(do-template [prim-array cast]
Expand Down

0 comments on commit 93e4911

Please sign in to comment.