Changeset 3579
 Timestamp:
 09/04/03 14:42:53 (19 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/j/src/org/armedbear/lisp/Fixnum.java
r3570 r3579 3 3 * 4 4 * Copyright (C) 20022003 Peter Graves 5 * $Id: Fixnum.java,v 1.6 0 20030904 04:28:01piso Exp $5 * $Id: Fixnum.java,v 1.61 20030904 14:42:53 piso Exp $ 6 6 * 7 7 * This program is free software; you can redistribute it and/or … … 281 281 public LispObject divideBy(LispObject obj) throws LispError 282 282 { 283 if (obj instanceof Fixnum) { 284 final int divisor = ((Fixnum)obj).value; 285 if (value % divisor == 0) 286 return new Fixnum(value / divisor); 287 return number(BigInteger.valueOf(value), 288 BigInteger.valueOf(divisor)); 289 } 290 if (obj instanceof Bignum) 291 return number(getBigInteger(), ((Bignum)obj).getValue()); 292 if (obj instanceof Ratio) { 293 BigInteger numerator = ((Ratio)obj).numerator(); 294 BigInteger denominator = ((Ratio)obj).denominator(); 295 return number(getBigInteger().multiply(denominator), 296 numerator); 297 } 298 if (obj instanceof LispFloat) 299 return new LispFloat(value / LispFloat.getValue(obj)); 300 if (obj instanceof Complex) { 301 Complex c = (Complex) obj; 302 LispObject realPart = c.getRealPart(); 303 LispObject imagPart = c.getImaginaryPart(); 304 LispObject denominator = 305 realPart.multiplyBy(realPart).add(imagPart.multiplyBy(imagPart)); 306 return Complex.getInstance(multiplyBy(realPart).divideBy(denominator), 307 Fixnum.ZERO.subtract(multiplyBy(imagPart).divideBy(denominator))); 308 } 309 throw new TypeError(obj, "number"); 283 try { 284 if (obj instanceof Fixnum) { 285 final int divisor = ((Fixnum)obj).value; 286 if (value % divisor == 0) 287 return new Fixnum(value / divisor); 288 return number(BigInteger.valueOf(value), 289 BigInteger.valueOf(divisor)); 290 } 291 if (obj instanceof Bignum) 292 return number(getBigInteger(), ((Bignum)obj).getValue()); 293 if (obj instanceof Ratio) { 294 BigInteger numerator = ((Ratio)obj).numerator(); 295 BigInteger denominator = ((Ratio)obj).denominator(); 296 return number(getBigInteger().multiply(denominator), 297 numerator); 298 } 299 if (obj instanceof LispFloat) 300 return new LispFloat(value / LispFloat.getValue(obj)); 301 if (obj instanceof Complex) { 302 Complex c = (Complex) obj; 303 LispObject realPart = c.getRealPart(); 304 LispObject imagPart = c.getImaginaryPart(); 305 LispObject denominator = 306 realPart.multiplyBy(realPart).add(imagPart.multiplyBy(imagPart)); 307 return Complex.getInstance(multiplyBy(realPart).divideBy(denominator), 308 Fixnum.ZERO.subtract(multiplyBy(imagPart).divideBy(denominator))); 309 } 310 throw new TypeError(obj, "number"); 311 } 312 catch (ArithmeticException e) { 313 if (obj.zerop()) 314 throw new DivisionByZero(); 315 throw new ArithmeticError(e.getMessage()); 316 } 310 317 } 311 318
Note: See TracChangeset
for help on using the changeset viewer.