[armedbear-cvs] r12922 - trunk/abcl/src/org/armedbear/lisp

Erik Huelsmann ehuelsmann at common-lisp.net
Sat Sep 25 10:10:24 UTC 2010


Author: ehuelsmann
Date: Sat Sep 25 06:10:22 2010
New Revision: 12922

Log:
Factor out common code and add documentation to indicate
which part of the CLHS it's implementing.

Modified:
   trunk/abcl/src/org/armedbear/lisp/MathFunctions.java

Modified: trunk/abcl/src/org/armedbear/lisp/MathFunctions.java
==============================================================================
--- trunk/abcl/src/org/armedbear/lisp/MathFunctions.java	(original)
+++ trunk/abcl/src/org/armedbear/lisp/MathFunctions.java	Sat Sep 25 06:10:22 2010
@@ -37,6 +37,24 @@
 
 public final class MathFunctions
 {
+
+    // Implementation of section 12.1.5.3, which says:
+    // "If the result of any computation would be a complex number whose
+    //  real part is of type rational and whose imaginary part is zero,
+    //  the result is converted to the rational which is the real part."
+    private static final LispObject complexToRealFixup(LispObject result,
+                                                       LispObject arg)
+    {
+        if (result instanceof Complex
+            && ! (arg instanceof Complex)) {
+            Complex c = (Complex)result;
+            LispObject im = c.getImaginaryPart();
+            if (im.zerop())
+                return c.getRealPart();
+        }
+        return result;
+    }
+
     // ### sin
     private static final Primitive SIN = new Primitive("sin", "radians")
     {
@@ -135,14 +153,8 @@
         result = log(result);
         result = result.multiplyBy(Complex.getInstance(Fixnum.ZERO,
                                                        Fixnum.MINUS_ONE));
-        if (result instanceof Complex) {
-            if (arg instanceof Complex)
-                return result;
-            LispObject im = ((Complex)result).getImaginaryPart();
-            if (im.zerop())
-                return ((Complex)result).getRealPart();
-        }
-        return result;
+
+        return complexToRealFixup(result, arg);
     }
 
     // ### acos
@@ -177,14 +189,8 @@
                 result = new SingleFloat((float)((DoubleFloat)result).value);
         }
         result = result.subtract(asin(arg));
-        if (result instanceof Complex) {
-            if (arg instanceof Complex)
-                return result;
-            LispObject im = ((Complex)result).getImaginaryPart();
-            if (im.zerop())
-                return ((Complex)result).getRealPart();
-        }
-        return result;
+
+        return complexToRealFixup(result, arg);
     }
 
     // ### atan
@@ -277,14 +283,8 @@
         LispObject result = exp(arg);
         result = result.subtract(exp(arg.multiplyBy(Fixnum.MINUS_ONE)));
         result = result.divideBy(Fixnum.TWO);
-        if (result instanceof Complex) {
-            if (arg instanceof Complex)
-                return result;
-            LispObject im = ((Complex)result).getImaginaryPart();
-            if (im.zerop())
-                return ((Complex)result).getRealPart();
-        }
-        return result;
+
+        return complexToRealFixup(result, arg);
     }
 
     // ### cosh
@@ -315,14 +315,8 @@
         LispObject result = exp(arg);
         result = result.add(exp(arg.multiplyBy(Fixnum.MINUS_ONE)));
         result = result.divideBy(Fixnum.TWO);
-        if (result instanceof Complex) {
-            if (arg instanceof Complex)
-                return result;
-            LispObject im = ((Complex)result).getImaginaryPart();
-            if (im.zerop())
-                return ((Complex)result).getRealPart();
-        }
-        return result;
+
+        return complexToRealFixup(result, arg);
     }
 
     // ### tanh
@@ -365,14 +359,8 @@
         result = sqrt(result);
         result = result.add(arg);
         result = log(result);
-        if (result instanceof Complex) {
-            if (arg instanceof Complex)
-                return result;
-            LispObject im = ((Complex)result).getImaginaryPart();
-            if (im.zerop())
-                return ((Complex)result).getRealPart();
-        }
-        return result;
+
+        return complexToRealFixup(result, arg);
     }
 
     // ### acosh
@@ -402,14 +390,8 @@
         LispObject result = n1.add(n2);
         result = log(result);
         result = result.multiplyBy(Fixnum.TWO);
-        if (result instanceof Complex) {
-            if (arg instanceof Complex)
-                return result;
-            LispObject im = ((Complex)result).getImaginaryPart();
-            if (im.zerop())
-                return ((Complex)result).getRealPart();
-        }
-        return result;
+
+        return complexToRealFixup(result, arg);
     }
 
     // ### atanh
@@ -434,14 +416,8 @@
         LispObject n2 = log(Fixnum.ONE.subtract(arg));
         LispObject result = n1.subtract(n2);
         result = result.divideBy(Fixnum.TWO);
-        if (result instanceof Complex) {
-            if (arg instanceof Complex)
-                return result;
-            LispObject im = ((Complex)result).getImaginaryPart();
-            if (im.zerop())
-                return ((Complex)result).getRealPart();
-        }
-        return result;
+
+        return complexToRealFixup(result, arg);
     }
 
     // ### cis




More information about the armedbear-cvs mailing list