Changeset 4544
 Timestamp:
 10/26/03 00:37:26 (18 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/j/src/org/armedbear/lisp/Primitives.java
r4543 r4544 3 3 * 4 4 * Copyright (C) 20022003 Peter Graves 5 * $Id: Primitives.java,v 1.48 4 20031025 21:56:29 pisoExp $5 * $Id: Primitives.java,v 1.485 20031026 00:37:26 dmcnaught Exp $ 6 6 * 7 7 * This program is free software; you can redistribute it and/or … … 4469 4469 }; 4470 4470 4471 private static final Primitive1 COS = 4472 new Primitive1("cos") { 4473 public LispObject execute(LispObject arg) throws ConditionThrowable 4474 { 4475 return cos(arg); 4476 } 4477 }; 4478 4479 private static LispObject cos(LispObject arg) throws ConditionThrowable 4480 { 4481 if (arg.realp()) { 4482 LispFloat argf = LispFloat.coerceToFloat(arg); 4483 return new LispFloat(Math.cos(argf.getValue())); 4484 } else if (arg instanceof Complex) { 4485 Complex argc = (Complex)arg; 4486 Complex iargc = (Complex)argc.multiplyBy(Complex.getInstance(new Fixnum(0), new Fixnum(1))); 4487 Complex c = (Complex)exp(iargc); 4488 c = (Complex)c.add(exp(iargc.multiplyBy(new Fixnum(1)))); 4489 c = (Complex)c.divideBy(new Fixnum(2)); 4490 return c; 4491 } 4492 4493 throw new ConditionThrowable(new TypeError(arg, "number")); 4494 } 4495 4496 private static final Primitive1 SIN = 4497 new Primitive1("sin") { 4498 public LispObject execute(LispObject arg) throws ConditionThrowable 4499 { 4500 return sin(arg); 4501 } 4502 }; 4503 4504 private static LispObject sin(LispObject arg) throws ConditionThrowable 4505 { 4506 if (arg.realp()) { // return real 4507 LispFloat argf = LispFloat.coerceToFloat(arg); 4508 return new LispFloat(Math.sin(argf.getValue())); 4509 } else if (arg instanceof Complex) { 4510 Complex argc = (Complex)arg; 4511 Complex iargc = (Complex)argc.multiplyBy(Complex.getInstance(new Fixnum(0), new Fixnum(1))); 4512 Complex c = (Complex)exp(iargc); 4513 c = (Complex)c.subtract(exp(iargc.multiplyBy(new Fixnum(1)))); 4514 c = (Complex)c.divideBy((new Fixnum(2)).multiplyBy(Complex.getInstance(new Fixnum(0), new Fixnum(1)))); 4515 return c; 4516 } 4517 4518 throw new ConditionThrowable(new TypeError(arg, "number")); 4519 } 4520 4521 private static final Primitive1 TAN = 4522 new Primitive1("tan") { 4523 public LispObject execute(LispObject arg) throws ConditionThrowable 4524 { 4525 return tan(arg); 4526 } 4527 }; 4528 4529 private static LispObject tan(LispObject arg) throws ConditionThrowable 4530 { 4531 return sin(arg).divideBy(cos(arg)); 4532 } 4533 4534 private static final Primitive1 EXP = 4535 new Primitive1("exp") { 4536 public LispObject execute(LispObject arg) throws ConditionThrowable 4537 { 4538 return exp(arg); 4539 } 4540 }; 4541 4542 private static LispObject exp(LispObject arg) throws ConditionThrowable 4543 { 4544 if (arg.realp()) { // return real 4545 LispFloat argf = LispFloat.coerceToFloat(arg); 4546 return new LispFloat(Math.exp(argf.getValue())); 4547 } else if (arg instanceof Complex) { 4548 Complex argc = (Complex)arg; 4549 double re = LispFloat.coerceToFloat(argc.getRealPart()).getValue(); 4550 double im = LispFloat.coerceToFloat(argc.getImaginaryPart()).getValue(); 4551 LispFloat resX = new LispFloat(Math.exp(re) * Math.cos(im)); 4552 LispFloat resY = new LispFloat(Math.exp(re) * Math.sin(im)); 4553 return Complex.getInstance(resX, resY); 4554 } 4555 4556 throw new ConditionThrowable(new TypeError(arg, "number")); 4557 } 4558 4471 4559 // ### sqrt 4472 4560 private static final Primitive1 SQRT = … … 4485 4573 private static final LispFloat sqrt(LispObject obj) throws ConditionThrowable 4486 4574 { 4487 if (obj instanceof Fixnum) 4488 return new LispFloat(Math.sqrt(((Fixnum)obj).getValue())); 4489 if (obj instanceof Bignum) 4490 return new LispFloat(Math.sqrt(((Bignum)obj).floatValue())); 4491 if (obj instanceof Ratio) 4492 return new LispFloat(Math.sqrt(((Ratio)obj).floatValue())); 4493 if (obj instanceof LispFloat) 4494 return new LispFloat(Math.sqrt(((LispFloat)obj).getValue())); 4575 if (obj.realp() && !obj.minusp()) { // returning real 4576 if (obj instanceof Fixnum) 4577 return new LispFloat(Math.sqrt(((Fixnum)obj).getValue())); 4578 if (obj instanceof Bignum) 4579 return new LispFloat(Math.sqrt(((Bignum)obj).floatValue())); 4580 if (obj instanceof Ratio) 4581 return new LispFloat(Math.sqrt(((Ratio)obj).floatValue())); 4582 if (obj instanceof LispFloat) 4583 return new LispFloat(Math.sqrt(((LispFloat)obj).getValue())); 4584 }// else { // returning Complex 4585 // if (obj.realp()) { 4586 4587 4495 4588 throw new ConditionThrowable(new TypeError(obj, "number")); 4496 4589 }
Note: See TracChangeset
for help on using the changeset viewer.