Skip to content

Commit

Permalink
incomplete work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
richhickey committed Jun 13, 2010
1 parent c5d0985 commit 8fbafa9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/jvm/clojure/lang/Compiler.java
Expand Up @@ -5392,7 +5392,7 @@ public void emit(C context, ObjExpr objx, GeneratorAdapter gen){
if(!(arg instanceof MaybePrimitiveExpr && arg.hasJavaClass() && arg.getJavaClass() == primc))
throw new IllegalArgumentException("recur arg for primitive local: " +
lb.name + " must be matching primitive, had: " +
arg.getJavaClass().getName() +
(arg.hasJavaClass() ? arg.getJavaClass().getName():"Object") +
", needed: " +
primc.getName());
}
Expand Down
49 changes: 19 additions & 30 deletions src/jvm/clojure/lang/Numbers.java
Expand Up @@ -137,37 +137,37 @@ static public Number quotient(Number x, Number y){
Ops yops = ops(y);
if(yops.isZero(y))
throw new ArithmeticException("Divide by zero");
return reduce(ops(x).combine(yops).quotient(x, y));
return ops(x).combine(yops).quotient(x, y);
}

static public Number remainder(Number x, Number y){
Ops yops = ops(y);
if(yops.isZero(y))
throw new ArithmeticException("Divide by zero");
return reduce(ops(x).combine(yops).remainder(x, y));
return ops(x).combine(yops).remainder(x, y);
}

static Number quotient(double n, double d){
double q = n / d;
if(q <= Integer.MAX_VALUE && q >= Integer.MIN_VALUE)
if(q <= Long.MAX_VALUE && q >= Long.MIN_VALUE)
{
return (int) q;
return (long) q;
}
else
{ //bigint quotient
return reduceBigInteger(new BigDecimal(q).toBigInteger());
return new BigDecimal(q).toBigInteger();
}
}

static Number remainder(double n, double d){
double q = n / d;
if(q <= Integer.MAX_VALUE && q >= Integer.MIN_VALUE)
if(q <= Long.MAX_VALUE && q >= Long.MIN_VALUE)
{
return (n - ((int) q) * d);
}
else
{ //bigint quotient
Number bq = reduceBigInteger(new BigDecimal(q).toBigInteger());
Number bq = new BigDecimal(q).toBigInteger();
return (n - bq.doubleValue() * d);
}
}
Expand Down Expand Up @@ -253,14 +253,6 @@ else if(x instanceof BigDecimal)
return x;
}

static public Number reduce(Number val){
if(val instanceof Long)
return reduce(val.longValue());
else if (val instanceof BigInteger)
return reduceBigInteger((BigInteger) val);
return val;
}

static public Number reduceBigInteger(BigInteger val){
int bitLength = val.bitLength();
if(bitLength < 32)
Expand All @@ -272,13 +264,6 @@ static public Number reduceBigInteger(BigInteger val){
return val;
}

static public Number reduce(long val){
// if(val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE)
// return (int) val;
// else
return val;
}

static public Number divide(BigInteger n, BigInteger d){
if(d.equals(BigInteger.ZERO))
throw new ArithmeticException("Divide by zero");
Expand All @@ -288,9 +273,9 @@ static public Number divide(BigInteger n, BigInteger d){
n = n.divide(gcd);
d = d.divide(gcd);
if(d.equals(BigInteger.ONE))
return reduceBigInteger(n);
return n;
else if(d.equals(BigInteger.ONE.negate()))
return reduceBigInteger(n.negate());
return n.negate();
return new Ratio((d.signum() < 0 ? n.negate() : n),
(d.signum() < 0 ? d.negate() : d));
}
Expand Down Expand Up @@ -348,6 +333,10 @@ static public int shiftLeft(int x, int n){
return x << n;
}

static public long shiftLeft(long x, int n){
return x << n;
}

static public Number shiftRight(Object x, Object n){
return bitOps(x).shiftRight((Number)x, ((Number)n).intValue());
}
Expand Down Expand Up @@ -609,7 +598,7 @@ public Number quotient(Number x, Number y){
Ratio ry = toRatio(y);
BigInteger q = rx.numerator.multiply(ry.denominator).divide(
rx.denominator.multiply(ry.numerator));
return reduceBigInteger(q);
return q;
}

public Number remainder(Number x, Number y){
Expand Down Expand Up @@ -690,11 +679,11 @@ public boolean isNeg(Number x){
}

final public Number add(Number x, Number y){
return reduceBigInteger(toBigInteger(x).add(toBigInteger(y)));
return toBigInteger(x).add(toBigInteger(y));
}

final public Number multiply(Number x, Number y){
return reduceBigInteger(toBigInteger(x).multiply(toBigInteger(y)));
return toBigInteger(x).multiply(toBigInteger(y));
}

public Number divide(Number x, Number y){
Expand Down Expand Up @@ -724,12 +713,12 @@ final public Number negate(Number x){

public Number inc(Number x){
BigInteger bx = toBigInteger(x);
return reduceBigInteger(bx.add(BigInteger.ONE));
return bx.add(BigInteger.ONE);
}

public Number dec(Number x){
BigInteger bx = toBigInteger(x);
return reduceBigInteger(bx.subtract(BigInteger.ONE));
return bx.subtract(BigInteger.ONE);
}
}

Expand Down Expand Up @@ -907,7 +896,7 @@ public boolean testBit(Number x, int n){
public Number shiftLeft(Number x, int n){
if(n < 0)
return shiftRight(x, -n);
return reduceBigInteger(toBigInteger(x).shiftLeft(n));
return Numbers.shiftLeft(x.longValue(),n);
}

public Number shiftRight(Number x, int n){
Expand Down

0 comments on commit 8fbafa9

Please sign in to comment.