Changeset 12922


Ignore:
Timestamp:
09/25/10 10:10:22 (13 years ago)
Author:
ehuelsmann
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/src/org/armedbear/lisp/MathFunctions.java

    r12513 r12922  
    3838public final class MathFunctions
    3939{
     40
     41    // Implementation of section 12.1.5.3, which says:
     42    // "If the result of any computation would be a complex number whose
     43    //  real part is of type rational and whose imaginary part is zero,
     44    //  the result is converted to the rational which is the real part."
     45    private static final LispObject complexToRealFixup(LispObject result,
     46                                                       LispObject arg)
     47    {
     48        if (result instanceof Complex
     49            && ! (arg instanceof Complex)) {
     50            Complex c = (Complex)result;
     51            LispObject im = c.getImaginaryPart();
     52            if (im.zerop())
     53                return c.getRealPart();
     54        }
     55        return result;
     56    }
     57
    4058    // ### sin
    4159    private static final Primitive SIN = new Primitive("sin", "radians")
     
    136154        result = result.multiplyBy(Complex.getInstance(Fixnum.ZERO,
    137155                                                       Fixnum.MINUS_ONE));
    138         if (result instanceof Complex) {
    139             if (arg instanceof Complex)
    140                 return result;
    141             LispObject im = ((Complex)result).getImaginaryPart();
    142             if (im.zerop())
    143                 return ((Complex)result).getRealPart();
    144         }
    145         return result;
     156
     157        return complexToRealFixup(result, arg);
    146158    }
    147159
     
    178190        }
    179191        result = result.subtract(asin(arg));
    180         if (result instanceof Complex) {
    181             if (arg instanceof Complex)
    182                 return result;
    183             LispObject im = ((Complex)result).getImaginaryPart();
    184             if (im.zerop())
    185                 return ((Complex)result).getRealPart();
    186         }
    187         return result;
     192
     193        return complexToRealFixup(result, arg);
    188194    }
    189195
     
    278284        result = result.subtract(exp(arg.multiplyBy(Fixnum.MINUS_ONE)));
    279285        result = result.divideBy(Fixnum.TWO);
    280         if (result instanceof Complex) {
    281             if (arg instanceof Complex)
    282                 return result;
    283             LispObject im = ((Complex)result).getImaginaryPart();
    284             if (im.zerop())
    285                 return ((Complex)result).getRealPart();
    286         }
    287         return result;
     286
     287        return complexToRealFixup(result, arg);
    288288    }
    289289
     
    316316        result = result.add(exp(arg.multiplyBy(Fixnum.MINUS_ONE)));
    317317        result = result.divideBy(Fixnum.TWO);
    318         if (result instanceof Complex) {
    319             if (arg instanceof Complex)
    320                 return result;
    321             LispObject im = ((Complex)result).getImaginaryPart();
    322             if (im.zerop())
    323                 return ((Complex)result).getRealPart();
    324         }
    325         return result;
     318
     319        return complexToRealFixup(result, arg);
    326320    }
    327321
     
    366360        result = result.add(arg);
    367361        result = log(result);
    368         if (result instanceof Complex) {
    369             if (arg instanceof Complex)
    370                 return result;
    371             LispObject im = ((Complex)result).getImaginaryPart();
    372             if (im.zerop())
    373                 return ((Complex)result).getRealPart();
    374         }
    375         return result;
     362
     363        return complexToRealFixup(result, arg);
    376364    }
    377365
     
    403391        result = log(result);
    404392        result = result.multiplyBy(Fixnum.TWO);
    405         if (result instanceof Complex) {
    406             if (arg instanceof Complex)
    407                 return result;
    408             LispObject im = ((Complex)result).getImaginaryPart();
    409             if (im.zerop())
    410                 return ((Complex)result).getRealPart();
    411         }
    412         return result;
     393
     394        return complexToRealFixup(result, arg);
    413395    }
    414396
     
    435417        LispObject result = n1.subtract(n2);
    436418        result = result.divideBy(Fixnum.TWO);
    437         if (result instanceof Complex) {
    438             if (arg instanceof Complex)
    439                 return result;
    440             LispObject im = ((Complex)result).getImaginaryPart();
    441             if (im.zerop())
    442                 return ((Complex)result).getRealPart();
    443         }
    444         return result;
     419
     420        return complexToRealFixup(result, arg);
    445421    }
    446422
Note: See TracChangeset for help on using the changeset viewer.