diff options
author | Rich Hickey <richhickey@gmail.com> | 2006-03-28 17:05:39 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2006-03-28 17:05:39 +0000 |
commit | 2a71c7ffd46b52d5b25ba341cd00f26d4c49fb08 (patch) | |
tree | 7c91067799021af0a2e43d653fb84c18d17b7b26 /src | |
parent | 25b705bd37d8290435b0d2b6ff6690cfa174ccac (diff) |
first-cut implementation
Diffstat (limited to 'src')
-rw-r--r-- | src/org/clojure/runtime/BigNum.java | 160 | ||||
-rw-r--r-- | src/org/clojure/runtime/FixNum.java | 179 |
2 files changed, 339 insertions, 0 deletions
diff --git a/src/org/clojure/runtime/BigNum.java b/src/org/clojure/runtime/BigNum.java new file mode 100644 index 00000000..63b45e49 --- /dev/null +++ b/src/org/clojure/runtime/BigNum.java @@ -0,0 +1,160 @@ +/** + * Copyright (c) Rich Hickey. All rights reserved. + * The use and distribution terms for this software are covered by the + * Common Public License 1.0 (http://opensource.org/licenses/cpl.php) + * which can be found in the file CPL.TXT at the root of this distribution. + * By using this software in any fashion, you are agreeing to be bound by + * the terms of this license. + * You must not remove this notice, or any other, from this software. + **/ + +/* rich Mar 28, 2006 10:08:33 AM */ + +package org.clojure.runtime; + +import java.math.BigInteger; + +public class BigNum extends IntegerNum { + public BigInteger val; + + public boolean equals(Object arg0) { + return arg0 != null + && arg0 instanceof BigNum + && ((BigNum)arg0).val == val; + } + public int hashCode() { + return val.hashCode(); + } + public String toString() { + return val.toString(); + } + public BigNum(long val){ + this.val = BigInteger.valueOf(val); + } + public BigNum(BigInteger val){ + this.val = val; + } + + public double doubleValue() { + return val.doubleValue(); + } + public float floatValue() { + return val.floatValue(); + } + public int intValue() { + return val.intValue(); + } + public long longValue() { + return val.longValue(); + } + + public boolean equiv(Num rhs) { + return rhs.equivTo(val); + } + public boolean equivTo(BigInteger x) { + return x.equals(val); + } + public boolean equivTo(int x) { + //must be outside of range of int or would be one itself + return false; + } + public boolean equivTo(RatioNum x) { + //wouldn't still be a RatioNum if it was an integer + return false; + } + + public boolean lt(Num rhs) + { + return rhs.gt(val); + } + public boolean gt(BigInteger x) + { + return x.compareTo(val) < 0; + } + public boolean gt(int x) + { + return BigInteger.valueOf(x).compareTo(val) < 0; + } + public boolean gt(RatioNum x) + { + return x.numerator.lt(x.denominator.multiply(val)); + } + + public Num add(Num rhs) { + return rhs.addTo(val); + } + public Num addTo(BigInteger x) { + return Num.from(x.add(val)); + } + public Num addTo(int x) { + return Num.from(val.add(BigInteger.valueOf(x))); + } + public Num addTo(RatioNum x) { + return x.addTo(val); + } + + public Num subtractFrom(Num x) { + return x.addTo(val.negate()); + } + + public Num multiplyBy(Num rhs) { + return rhs.multiply(val); + } + public Num multiply(BigInteger x) { + return Num.from(x.multiply(val)); + } + public Num multiply(int x) { + return Num.from(val.multiply(BigInteger.valueOf(x))); + } + public Num multiply(RatioNum x) { + return x.multiply(val); + } + + public Num divideBy(Num rhs) { + return rhs.divide(val); + } + public Num divide(BigInteger n) { + return Num.divide(n,val); + } + public Num divide(int n) { + return Num.divide(BigInteger.valueOf(n),val); + } + public Num divide(RatioNum x) { + return Num.divide(x.numerator,x.denominator.multiply(val)); + } + + public Object truncateDivide(ThreadLocalData tld, Num num) { + return num.truncateBy(tld, val); + } + + public Object truncateBy(ThreadLocalData tld, int div) { + return Num.truncateBigints(tld, val,BigInteger.valueOf(div)); + } + + public Object truncateBy(ThreadLocalData tld, BigInteger div) { + return Num.truncateBigints(tld, val,div); + } + + public Object truncateBy(ThreadLocalData tld, RatioNum div) { + Num q = (Num)Num.truncate(tld, div.denominator.multiply(val),div.numerator); + return RT.setValues(tld,q,q.multiplyBy(div).subtractFrom(this)); + } + + public Num negate(){ + return Num.from(val.negate()); + } + public boolean minusp() { + return val.signum() < 0; + } + public boolean plusp() { + return val.signum() > 0; + } + + public Num oneMinus() { + return Num.from(val.subtract(BigInteger.ONE)); + } + public Num onePlus() { + return Num.from(val.add(BigInteger.ONE)); + } +} + diff --git a/src/org/clojure/runtime/FixNum.java b/src/org/clojure/runtime/FixNum.java new file mode 100644 index 00000000..e3a109e5 --- /dev/null +++ b/src/org/clojure/runtime/FixNum.java @@ -0,0 +1,179 @@ +/** + * Copyright (c) Rich Hickey. All rights reserved. + * The use and distribution terms for this software are covered by the + * Common Public License 1.0 (http://opensource.org/licenses/cpl.php) + * which can be found in the file CPL.TXT at the root of this distribution. + * By using this software in any fashion, you are agreeing to be bound by + * the terms of this license. + * You must not remove this notice, or any other, from this software. + **/ + +/* rich Mar 28, 2006 10:09:27 AM */ + +package org.clojure.runtime; + +import java.math.BigInteger; + +public class FixNum extends IntegerNum { + public int val; + + public boolean equals(Object arg0) { + return arg0 != null + && arg0 instanceof FixNum + && ((FixNum)arg0).val == val; + } + public int hashCode() { + return val; + } + public String toString() { + return Integer.toString(val); + } + public FixNum(int val){ + this.val = val; + } + + public double doubleValue() { + return (double)val; + } + public float floatValue() { + return (float)val; + } + public int intValue() { + return val; + } + public long longValue() { + return (long)val; + } + + public boolean equiv(Num rhs) { + return rhs.equivTo(val); + } + public boolean equivTo(BigInteger x) { + //wouldn't still be a BigInteger if it fit in int + return false; + } + public boolean equivTo(int x) { + return x == val; + } + public boolean equivTo(RatioNum x) { + //wouldn't still be a RatioNum if it was an integer + return false; + } + + public boolean lt(Num rhs) + { + return rhs.gt(val); + } + public boolean gt(BigInteger x) + { + return x.compareTo(BigInteger.valueOf(val)) < 0; + } + public boolean gt(int x) + { + return x < val; + } + public boolean gt(RatioNum x) + { + return x.numerator.lt(x.denominator.multiply(val)); + } + + public Num add(Num rhs) { + return rhs.addTo(val); + } + public Num addTo(BigInteger x) { + return Num.from(x.add(BigInteger.valueOf(val))); + } + public Num addTo(int x) { + return Num.from((long)x + val); + } + public Num addTo(RatioNum x) { + return x.addTo(val); + } + + public Num subtractFrom(Num x) { + return x.addTo(-val); + } + + public Num multiplyBy(Num rhs) { + return rhs.multiply(val); + } + public Num multiply(BigInteger x) { + return Num.from(x.multiply(BigInteger.valueOf(val))); + } + public Num multiply(int x) { + return Num.from((long)x * val); + } + public Num multiply(RatioNum x) { + return x.multiply(val); + } + + public Object truncateDivide(ThreadLocalData tld, Num num) { + return num.truncateBy(tld, val); + } + + public Object truncateBy(ThreadLocalData tld, int div) { + return RT.setValues(tld,Num.from(val/div),Num.from(val%div)); + } + + public Object truncateBy(ThreadLocalData tld, BigInteger div) { + return Num.truncateBigints(tld, BigInteger.valueOf(val),div); + } + + public Object truncateBy(ThreadLocalData tld, RatioNum div) { + Num q = (Num)Num.truncate(tld, div.denominator.multiply(val),div.numerator); + return RT.setValues(tld,q,q.multiplyBy(div).subtractFrom(this)); + } + + public Num divideBy(Num rhs) { + return rhs.divide(val); + } + public Num divide(BigInteger n) { + return Num.divide(n,BigInteger.valueOf(val)); + } + + static int gcd(int u, int v) { + while (v != 0) { + int r = u % v; + u = v; + v = r; + } + return u; + } + + public Num divide(int n) { + int gcd = gcd(n,val); + if(gcd == 0) + return Num.ZERO; + + n = n/gcd; + int d = val/gcd; + if(d == 1) + return Num.from(n); + if(d < 0){ + n = -n; + d = -d; + } + return new RatioNum((IntegerNum)Num.from(n),(IntegerNum)Num.from(d)); + } + public Num divide(RatioNum x) { + return Num.divide(x.numerator,x.denominator.multiply(val)); + } + + public Num negate(){ + return Num.from(-val); + } + public boolean minusp() { + return val < 0; + } + public boolean plusp() { + return val > 0; + } + + public Num oneMinus() { + return Num.from(val - 1); + } + public Num onePlus() { + return Num.from(val + 1); + } + +} |