Changeset 3361


Ignore:
Timestamp:
08/13/03 01:14:00 (18 years ago)
Author:
piso
Message:

ABS()

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

Legend:

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

    r3357 r3361  
    33 *
    44 * Copyright (C) 2003 Peter Graves
    5  * $Id: Bignum.java,v 1.20 2003-08-12 17:00:11 piso Exp $
     5 * $Id: Bignum.java,v 1.21 2003-08-13 01:14:00 piso Exp $
    66 *
    77 * This program is free software; you can redistribute it and/or
     
    101101    }
    102102
     103    public LispObject ABS()
     104    {
     105        if (value.signum() >= 0)
     106            return this;
     107        return new Bignum(value.negate());
     108    }
     109
    103110    public LispObject ZEROP()
    104111    {
  • trunk/j/src/org/armedbear/lisp/Complex.java

    r3357 r3361  
    33 *
    44 * Copyright (C) 2003 Peter Graves
    5  * $Id: Complex.java,v 1.7 2003-08-12 17:00:12 piso Exp $
     5 * $Id: Complex.java,v 1.8 2003-08-13 01:14:00 piso Exp $
    66 *
    77 * This program is free software; you can redistribute it and/or
     
    147147    }
    148148
     149    public LispObject ABS() throws TypeError
     150    {
     151        float real = LispFloat.coerceToFloat(realpart).getValue();
     152        float imag = LispFloat.coerceToFloat(imagpart).getValue();
     153        return new LispFloat((float)Math.sqrt(real * real + imag * imag));
     154    }
     155
    149156    public LispObject ZEROP() throws TypeError
    150157    {
  • trunk/j/src/org/armedbear/lisp/Fixnum.java

    r3357 r3361  
    33 *
    44 * Copyright (C) 2002-2003 Peter Graves
    5  * $Id: Fixnum.java,v 1.45 2003-08-12 17:00:12 piso Exp $
     5 * $Id: Fixnum.java,v 1.46 2003-08-13 01:14:00 piso Exp $
    66 *
    77 * This program is free software; you can redistribute it and/or
     
    112112            return false;
    113113        }
     114    }
     115
     116    public LispObject ABS()
     117    {
     118        if (value >= 0)
     119            return this;
     120        return new Fixnum(- value);
    114121    }
    115122
  • trunk/j/src/org/armedbear/lisp/LispFloat.java

    r3357 r3361  
    33 *
    44 * Copyright (C) 2003 Peter Graves
    5  * $Id: LispFloat.java,v 1.18 2003-08-12 17:00:12 piso Exp $
     5 * $Id: LispFloat.java,v 1.19 2003-08-13 01:14:00 piso Exp $
    66 *
    77 * This program is free software; you can redistribute it and/or
     
    100100            return value == ((Ratio)obj).floatValue();
    101101        return false;
     102    }
     103
     104    public LispObject ABS()
     105    {
     106        if (value >= 0)
     107            return this;
     108        return new LispFloat(- value);
    102109    }
    103110
     
    291298    };
    292299
    293     public static LispObject coerceToFloat(LispObject obj) throws TypeError
    294     {
    295         if (obj instanceof LispFloat)
    296             return obj;
     300    public static LispFloat coerceToFloat(LispObject obj) throws TypeError
     301    {
     302        if (obj instanceof LispFloat)
     303            return (LispFloat) obj;
    297304        if (obj instanceof Fixnum)
    298305            return new LispFloat(((Fixnum)obj).getValue());
  • trunk/j/src/org/armedbear/lisp/LispObject.java

    r3341 r3361  
    33 *
    44 * Copyright (C) 2002-2003 Peter Graves
    5  * $Id: LispObject.java,v 1.27 2003-08-12 02:06:53 piso Exp $
     5 * $Id: LispObject.java,v 1.28 2003-08-13 01:14:00 piso Exp $
    66 *
    77 * This program is free software; you can redistribute it and/or
     
    109109    }
    110110
     111    public LispObject ABS() throws TypeError
     112    {
     113        throw new TypeError(this, "number");
     114    }
     115
    111116    public LispObject ZEROP() throws TypeError
    112117    {
  • trunk/j/src/org/armedbear/lisp/Ratio.java

    r3357 r3361  
    33 *
    44 * Copyright (C) 2003 Peter Graves
    5  * $Id: Ratio.java,v 1.16 2003-08-12 17:00:13 piso Exp $
     5 * $Id: Ratio.java,v 1.17 2003-08-13 01:14:00 piso Exp $
    66 *
    77 * This program is free software; you can redistribute it and/or
     
    9999        }
    100100        return false;
     101    }
     102
     103    public LispObject ABS()
     104    {
     105        if (numerator.signum() > 0 && denominator.signum() > 0)
     106            return this;
     107        if (numerator.signum() < 0 && denominator.signum() < 0)
     108            return this;
     109        return new Ratio(numerator.negate(), denominator);
    101110    }
    102111
Note: See TracChangeset for help on using the changeset viewer.