Permalink
Browse files

incorporate BigInt

  • Loading branch information...
1 parent 845c63e commit 07f05862c2362f6b51e7c92ccd9476c45c9dff6e @richhickey richhickey committed Jun 25, 2010
@@ -3008,6 +3008,7 @@
[n]
(or (instance? Integer n)
(instance? Long n)
+ (instance? clojure.lang.BigInt n)
(instance? BigInteger n)
(instance? Short n)
(instance? Byte n)))
@@ -3065,6 +3066,19 @@
(or (integer? n) (ratio? n) (decimal? n)))
(defn bigint
+ "Coerce to BigInt"
+ {:tag clojure.lang.BigInt
+ :static true
+ :added "1.3"}
+ [x] (cond
+ (instance? clojure.lang.BigInt x) x
+ (instance? BigInteger x) (clojure.lang.BigInt/fromBigInteger x)
+ (decimal? x) (bigint (.toBigInteger ^BigDecimal x))
+ (ratio? x) (bigint (.bigIntegerValue ^clojure.lang.Ratio x))
+ (number? x) (clojure.lang.BigInt/valueOf (long x))
+ :else (bigint (BigInteger. x))))
+
+(defn biginteger
"Coerce to BigInteger"
{:tag BigInteger
:added "1.0"
@@ -242,6 +242,7 @@
(defmethod print-dup java.lang.Double [o w] (print-method o w))
(defmethod print-dup clojure.lang.Ratio [o w] (print-method o w))
(defmethod print-dup java.math.BigDecimal [o w] (print-method o w))
+(defmethod print-dup clojure.lang.BigInt [o w] (print-method o w))
(defmethod print-dup java.math.BigInteger [o w] (print-method o w))
(defmethod print-dup clojure.lang.PersistentHashMap [o w] (print-method o w))
(defmethod print-dup clojure.lang.PersistentHashSet [o w] (print-method o w))
@@ -279,10 +280,14 @@
(.write w (str b))
(.write w "M"))
-(defmethod print-method java.math.BigInteger [b, ^Writer w]
+(defmethod print-method clojure.lang.BigInt [b, ^Writer w]
(.write w (str b))
(.write w "N"))
+(defmethod print-method java.math.BigInteger [b, ^Writer w]
+ (.write w (str b))
+ (.write w "BIGINT"))
+
(defmethod print-method java.util.regex.Pattern [p ^Writer w]
(.write w "#\"")
(loop [[^Character c & r :as s] (seq (.pattern ^java.util.regex.Pattern p))
@@ -247,7 +247,7 @@ http://www.lispworks.com/documentation/HyperSpec/Body/22_c.htm
for improved performance"
[base val]
(let [format-str (get java-base-formats base)]
- (if (and format-str (integer? val))
+ (if (and format-str (integer? val) (not (instance? clojure.lang.BigInt val)))
(clojure.core/format format-str val)
(base-str base val))))
@@ -19,6 +19,10 @@
final public long lpart;
final public BigInteger bipart;
+final public static BigInt ZERO = new BigInt(0,null);
+final public static BigInt ONE = new BigInt(1,null);
+
+
//must follow Long
public int hashCode(){
if(bipart == null)
@@ -106,4 +110,18 @@ public short shortValue(){
return bipart.shortValue();
}
+public static BigInt valueOf(long val){
+ return new BigInt(val, null);
+}
+
+public String toString(){
+ if(bipart == null)
+ return String.valueOf(lpart);
+ return bipart.toString();
+}
+
+public int bitLength(){
+ return toBigInteger().bitLength();
+}
+
}
@@ -329,7 +329,7 @@ private static Object matchNumber(String s){
if(m.group(2) != null)
{
if(m.group(8) != null)
- return BigInteger.ZERO;
+ return BigInt.ZERO;
return Numbers.num(0);
}
boolean negate = (m.group(1).equals("-"));
@@ -346,9 +346,13 @@ else if((n = m.group(7)) != null)
if(n == null)
return null;
BigInteger bn = new BigInteger(n, radix);
+ if(negate)
+ bn = bn.negate();
if(m.group(8) != null)
- return negate ? bn.negate() : bn;
- return Numbers.reduceBigInteger(negate ? bn.negate() : bn);
+ return BigInt.fromBigInteger(bn);
+ return bn.bitLength() < 64 ?
+ Numbers.num(bn.longValue())
+ : BigInt.fromBigInteger(bn);
}
m = floatPat.matcher(s);
if(m.matches())
@@ -360,8 +364,8 @@ else if((n = m.group(7)) != null)
m = ratioPat.matcher(s);
if(m.matches())
{
- return Numbers.divide(Numbers.reduceBigInteger(new BigInteger(m.group(1))),
- Numbers.reduceBigInteger((new BigInteger(m.group(2)))));
+ return Numbers.divide(Numbers.reduceBigInt(BigInt.fromBigInteger(new BigInteger(m.group(1)))),
+ Numbers.reduceBigInt(BigInt.fromBigInteger(new BigInteger(m.group(2)))));
}
return null;
}
Oops, something went wrong. Retry.

0 comments on commit 07f0586

Please sign in to comment.