Skip to content

Commit

Permalink
Make bigdec work on clojure.lang.BigInt args
Browse files Browse the repository at this point in the history
  • Loading branch information
jafingerhut authored and stuarthalloway committed May 18, 2012
1 parent 82fb40a commit fb653b7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/clj/clojure/core.clj
Expand Up @@ -3247,7 +3247,8 @@
[x] (cond
(decimal? x) x
(float? x) (. BigDecimal valueOf (double x))
(ratio? x) (/ (BigDecimal. (.numerator x)) (.denominator x))
(ratio? x) (/ (BigDecimal. (.numerator ^clojure.lang.Ratio x)) (.denominator ^clojure.lang.Ratio x))
(instance? clojure.lang.BigInt x) (.toBigDecimal ^clojure.lang.BigInt x)
(instance? BigInteger x) (BigDecimal. ^BigInteger x)
(number? x) (BigDecimal/valueOf (long x))
:else (BigDecimal. x)))
Expand Down
8 changes: 8 additions & 0 deletions src/jvm/clojure/lang/BigInt.java
Expand Up @@ -13,6 +13,7 @@
package clojure.lang;

import java.math.BigInteger;
import java.math.BigDecimal;

public final class BigInt extends Number{

Expand Down Expand Up @@ -66,6 +67,13 @@ public BigInteger toBigInteger(){
return bipart;
}

public BigDecimal toBigDecimal(){
if(bipart == null)
return BigDecimal.valueOf(lpart);
else
return new BigDecimal(bipart);
}

///// java.lang.Number:

public int intValue(){
Expand Down
2 changes: 1 addition & 1 deletion test/clojure/test_clojure/numbers.clj
Expand Up @@ -25,7 +25,7 @@


(deftest Coerced-BigDecimal
(let [v (bigdec 3)]
(doseq [v [(bigdec 3) (bigdec (inc (bigint Long/MAX_VALUE)))]]
(are [x] (true? x)
(instance? BigDecimal v)
(number? v)
Expand Down

0 comments on commit fb653b7

Please sign in to comment.