Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb653b7

Browse files
jafingerhutstuarthalloway
authored andcommitted
Make bigdec work on clojure.lang.BigInt args
1 parent 82fb40a commit fb653b7

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/clj/clojure/core.clj

+2-1
Original file line numberDiff line numberDiff line change
@@ -3247,7 +3247,8 @@
32473247
[x] (cond
32483248
(decimal? x) x
32493249
(float? x) (. BigDecimal valueOf (double x))
3250-
(ratio? x) (/ (BigDecimal. (.numerator x)) (.denominator x))
3250+
(ratio? x) (/ (BigDecimal. (.numerator ^clojure.lang.Ratio x)) (.denominator ^clojure.lang.Ratio x))
3251+
(instance? clojure.lang.BigInt x) (.toBigDecimal ^clojure.lang.BigInt x)
32513252
(instance? BigInteger x) (BigDecimal. ^BigInteger x)
32523253
(number? x) (BigDecimal/valueOf (long x))
32533254
:else (BigDecimal. x)))

src/jvm/clojure/lang/BigInt.java

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package clojure.lang;
1414

1515
import java.math.BigInteger;
16+
import java.math.BigDecimal;
1617

1718
public final class BigInt extends Number{
1819

@@ -66,6 +67,13 @@ public BigInteger toBigInteger(){
6667
return bipart;
6768
}
6869

70+
public BigDecimal toBigDecimal(){
71+
if(bipart == null)
72+
return BigDecimal.valueOf(lpart);
73+
else
74+
return new BigDecimal(bipart);
75+
}
76+
6977
///// java.lang.Number:
7078

7179
public int intValue(){

test/clojure/test_clojure/numbers.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
(deftest Coerced-BigDecimal
28-
(let [v (bigdec 3)]
28+
(doseq [v [(bigdec 3) (bigdec (inc (bigint Long/MAX_VALUE)))]]
2929
(are [x] (true? x)
3030
(instance? BigDecimal v)
3131
(number? v)

0 commit comments

Comments
 (0)