Changeset 4586


Ignore:
Timestamp:
10/30/03 21:40:10 (18 years ago)
Author:
asimon
Message:

catch ArithmeticException? in truncate

Location:
trunk/j/src/org/armedbear/lisp
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/j/src/org/armedbear/lisp/Bignum.java

    r3929 r4586  
    33 *
    44 * Copyright (C) 2003 Peter Graves
    5  * $Id: Bignum.java,v 1.46 2003-09-20 17:02:03 piso Exp $
     5 * $Id: Bignum.java,v 1.47 2003-10-30 21:40:10 asimon Exp $
    66 *
    77 * This program is free software; you can redistribute it and/or
     
    372372        final LispThread thread = LispThread.currentThread();
    373373        LispObject[] values = new LispObject[2];
    374         if (obj instanceof Ratio) {
     374  try {
     375    if (obj instanceof Ratio) {
    375376            Ratio divisor = (Ratio) obj;
    376377            LispObject quotient =
    377                 multiplyBy(divisor.DENOMINATOR()).truncate(divisor.NUMERATOR());
     378        multiplyBy(divisor.DENOMINATOR()).truncate(divisor.NUMERATOR());
    378379            LispObject remainder =
    379                 subtract(quotient.multiplyBy(divisor));
     380        subtract(quotient.multiplyBy(divisor));
    380381            values[0] = quotient;
    381382            values[1] = remainder;
    382         } else if (obj instanceof Fixnum) {
     383    } else if (obj instanceof Fixnum) {
    383384            BigInteger divisor = ((Fixnum)obj).getBigInteger();
    384385            BigInteger[] results = value.divideAndRemainder(divisor);
     
    387388            values[0] = number(quotient);
    388389            values[1] = (remainder.signum() == 0) ? Fixnum.ZERO : number(remainder);
    389         } else if (obj instanceof Bignum) {
     390    } else if (obj instanceof Bignum) {
    390391            BigInteger divisor = ((Bignum)obj).getValue();
    391392            BigInteger[] results = value.divideAndRemainder(divisor);
     
    394395            values[0] = number(quotient);
    395396            values[1] = (remainder.signum() == 0) ? Fixnum.ZERO : number(remainder);
    396         } else if (obj instanceof Ratio) {
     397    } else if (obj instanceof Ratio) {
    397398            Ratio divisor = (Ratio) obj;
    398399            LispObject quotient =
    399                 multiplyBy(divisor.DENOMINATOR()).truncate(divisor.NUMERATOR());
     400        multiplyBy(divisor.DENOMINATOR()).truncate(divisor.NUMERATOR());
    400401            LispObject remainder =
    401                 subtract(quotient.multiplyBy(divisor));
     402        subtract(quotient.multiplyBy(divisor));
    402403            values[0] = quotient;
    403404            values[1] = remainder;
    404         } else
     405    } else
    405406            throw new ConditionThrowable(new LispError("Bignum.truncate(): not implemented: " + obj.typeOf()));
     407        }
     408        catch (ArithmeticException e) {
     409            if (obj.zerop())
     410                throw new ConditionThrowable(new DivisionByZero());
     411            throw new ConditionThrowable(new ArithmeticError(e.getMessage()));
     412        }
    406413        thread.setValues(values);
    407414        return values[0];
  • trunk/j/src/org/armedbear/lisp/Fixnum.java

    r4575 r4586  
    33 *
    44 * Copyright (C) 2002-2003 Peter Graves
    5  * $Id: Fixnum.java,v 1.76 2003-10-30 08:17:14 asimon Exp $
     5 * $Id: Fixnum.java,v 1.77 2003-10-30 21:40:10 asimon Exp $
    66 *
    77 * This program is free software; you can redistribute it and/or
     
    450450        final LispThread thread = LispThread.currentThread();
    451451        LispObject[] values = new LispObject[2];
    452         if (obj instanceof Fixnum) {
     452  try {
     453    if (obj instanceof Fixnum) {
    453454            long divisor = ((Fixnum)obj).value;
    454455            long quotient = value / divisor;
     
    456457            values[0] = number(quotient);
    457458            values[1] = remainder == 0 ? Fixnum.ZERO : number(remainder);
    458         } else if (obj instanceof Bignum) {
     459    } else if (obj instanceof Bignum) {
    459460            BigInteger value = getBigInteger();
    460461            BigInteger divisor = ((Bignum)obj).getValue();
     
    464465            values[0] = number(quotient);
    465466            values[1] = (remainder.signum() == 0) ? Fixnum.ZERO : number(remainder);
    466         } else if (obj instanceof Ratio) {
     467    } else if (obj instanceof Ratio) {
    467468            Ratio divisor = (Ratio) obj;
    468469            LispObject quotient =
    469                 multiplyBy(divisor.DENOMINATOR()).truncate(divisor.NUMERATOR());
     470        multiplyBy(divisor.DENOMINATOR()).truncate(divisor.NUMERATOR());
    470471            LispObject remainder =
    471                 subtract(quotient.multiplyBy(divisor));
     472        subtract(quotient.multiplyBy(divisor));
    472473            values[0] = quotient;
    473474            values[1] = remainder;
    474         } else
     475    } else
    475476            throw new ConditionThrowable(new LispError("Fixnum.truncate(): not implemented: " + obj.typeOf()));
     477        }
     478        catch (ArithmeticException e) {
     479            if (obj.zerop())
     480                throw new ConditionThrowable(new DivisionByZero());
     481            throw new ConditionThrowable(new ArithmeticError(e.getMessage()));
     482        }
    476483        thread.setValues(values);
    477484        return values[0];
  • trunk/j/src/org/armedbear/lisp/Ratio.java

    r4341 r4586  
    33 *
    44 * Copyright (C) 2003 Peter Graves
    5  * $Id: Ratio.java,v 1.36 2003-10-13 11:50:32 piso Exp $
     5 * $Id: Ratio.java,v 1.37 2003-10-30 21:40:10 asimon Exp $
    66 *
    77 * This program is free software; you can redistribute it and/or
     
    378378    {
    379379        BigInteger n, d;
    380         if (obj instanceof Fixnum) {
     380  try {
     381    if (obj instanceof Fixnum) {
    381382            n = ((Fixnum)obj).getBigInteger();
    382383            d = BigInteger.ONE;
    383         } else if (obj instanceof Bignum) {
     384    } else if (obj instanceof Bignum) {
    384385            n = ((Bignum)obj).getValue();
    385386            d = BigInteger.ONE;
    386         } else if (obj instanceof Ratio) {
     387    } else if (obj instanceof Ratio) {
    387388            n = ((Ratio)obj).numerator();
    388389            d = ((Ratio)obj).denominator();
    389         } else {
     390    } else {
    390391            Thread.dumpStack();
    391392            throw new ConditionThrowable(new TypeError(obj, "number"));
    392         }
    393 
    394         // Invert and multiply.
    395         BigInteger num = numerator.multiply(d);
    396         BigInteger den = denominator.multiply(n);
    397         BigInteger quotient = num.divide(den);
    398 
    399         // Multiply quotient by divisor.
    400         LispObject product = number(quotient.multiply(n), d);
    401 
    402         // Subtract to get remainder.
    403         LispObject remainder = subtract(product);
    404 
    405         final LispThread thread = LispThread.currentThread();
    406         LispObject[] values = new LispObject[2];
    407         values[0] = number(quotient);
    408         values[1] = remainder;
    409         thread.setValues(values);
    410         return values[0];
     393    }
     394
     395    // Invert and multiply.
     396    BigInteger num = numerator.multiply(d);
     397    BigInteger den = denominator.multiply(n);
     398    BigInteger quotient = num.divide(den);
     399
     400    // Multiply quotient by divisor.
     401    LispObject product = number(quotient.multiply(n), d);
     402
     403    // Subtract to get remainder.
     404    LispObject remainder = subtract(product);
     405
     406    final LispThread thread = LispThread.currentThread();
     407    LispObject[] values = new LispObject[2];
     408    values[0] = number(quotient);
     409    values[1] = remainder;
     410    thread.setValues(values);
     411    return values[0];
     412        }
     413        catch (ArithmeticException e) {
     414            if (obj.zerop())
     415                throw new ConditionThrowable(new DivisionByZero());
     416            throw new ConditionThrowable(new ArithmeticError(e.getMessage()));
     417        }
    411418    }
    412419
Note: See TracChangeset for help on using the changeset viewer.