source: trunk/abcl/src/org/armedbear/lisp/ftruncate.java

Last change on this file was 12254, checked in by ehuelsmann, 14 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1/*
2 * ftruncate.java
3 *
4 * Copyright (C) 2004-2005 Peter Graves
5 * $Id: ftruncate.java 12254 2009-11-06 20:07:54Z ehuelsmann $
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *
21 * As a special exception, the copyright holders of this library give you
22 * permission to link this library with independent modules to produce an
23 * executable, regardless of the license terms of these independent
24 * modules, and to copy and distribute the resulting executable under
25 * terms of your choice, provided that you also meet, for each linked
26 * independent module, the terms and conditions of the license of that
27 * module.  An independent module is a module which is not derived from
28 * or based on this library.  If you modify this library, you may extend
29 * this exception to your version of the library, but you are not
30 * obligated to do so.  If you do not wish to do so, delete this
31 * exception statement from your version.
32 */
33
34package org.armedbear.lisp;
35
36// ### ftruncate number &optional divisor => quotient, remainder
37// (defun ftruncate (number &optional (divisor 1))
38//  (multiple-value-bind (tru rem) (truncate number divisor)
39//   (values (float tru) rem)))
40
41// "FFLOOR, FCEILING, FTRUNCATE, and FROUND handle arguments of different types
42// in the following way: If number is a float, and divisor is not a float of
43// longer format, then the first result is a float of the same type as number.
44// Otherwise, the first result is of the type determined by contagion rules."
45public final class ftruncate extends Primitive
46{
47    private ftruncate()
48    {
49        super("ftruncate", "number &optional divisor");
50    }
51
52    @Override
53    public LispObject execute(LispObject arg)
54    {
55        final LispThread thread = LispThread.currentThread();
56        if (arg.zerop()) {
57            LispObject q = arg;
58            LispObject r;
59            if (arg instanceof DoubleFloat)
60                r = DoubleFloat.ZERO;
61            else
62                r = SingleFloat.ZERO;
63            return thread.setValues(q, r);
64        }
65        if (arg instanceof DoubleFloat) {
66            double d = ((DoubleFloat)arg).value;
67            if (Double.isInfinite(d) || Double.isNaN(d))
68                return thread.setValues(arg, new DoubleFloat(Double.NaN));
69        } else if (arg instanceof SingleFloat) {
70            float f = ((SingleFloat)arg).value;
71            if (Float.isInfinite(f) || Float.isNaN(f))
72                return thread.setValues(arg, new SingleFloat(Float.NaN));
73        }
74        LispObject q = arg.truncate(Fixnum.ONE); // an integer
75        if (arg instanceof DoubleFloat) {
76            if (q.zerop()) {
77                if (arg.minusp())
78                    q = new DoubleFloat(-0.0);
79                else
80                    q = new DoubleFloat(0.0);
81            } else if (q instanceof Fixnum)
82                q = new DoubleFloat(((Fixnum)q).value);
83            else
84                q = new DoubleFloat(((Bignum)q).doubleValue());
85        } else {
86            if (q.zerop()) {
87                if (arg.minusp())
88                    q = new SingleFloat(-0.0f);
89                else
90                    q = new SingleFloat(0.0f);
91            } else if (q instanceof Fixnum)
92                q = new SingleFloat(((Fixnum)q).value);
93            else
94                q = new SingleFloat(((Bignum)q).floatValue());
95        }
96        thread._values[0] = q;
97        return q;
98    }
99
100    @Override
101    public LispObject execute(LispObject first, LispObject second)
102
103    {
104        final LispThread thread = LispThread.currentThread();
105        if (first.zerop()) {
106            LispObject q = first;
107            LispObject r;
108            if (first instanceof DoubleFloat)
109                r = DoubleFloat.ZERO;
110            else
111                r = SingleFloat.ZERO;
112            return thread.setValues(q, r);
113        }
114        if (first instanceof DoubleFloat) {
115            double d1 = ((DoubleFloat)first).value;
116            if (Double.isInfinite(d1) || Double.isNaN(d1))
117                return thread.setValues(first, new DoubleFloat(Double.NaN));
118        } else if (first instanceof SingleFloat) {
119            float f1 = ((SingleFloat)first).value;
120            if (Float.isInfinite(f1) || Float.isNaN(f1))
121                return thread.setValues(first, new SingleFloat(Float.NaN));
122        }
123        LispObject q = first.truncate(second); // an integer
124        if (first instanceof DoubleFloat || second instanceof DoubleFloat) {
125            if (q.zerop()) {
126                if (first.minusp()) {
127                    if (second.minusp())
128                        q = new DoubleFloat(0.0);
129                    else
130                        q = new DoubleFloat(-0.0);
131                } else if (second.minusp())
132                    q = new DoubleFloat(-0.0);
133                else
134                    q = new DoubleFloat(0.0);
135            } else if (q instanceof Fixnum)
136                q = new DoubleFloat(((Fixnum)q).value);
137            else
138                q = new DoubleFloat(((Bignum)q).doubleValue());
139        } else {
140            if (q.zerop()) {
141                if (first.minusp()) {
142                    if (second.minusp())
143                        q = new SingleFloat(0.0f);
144                    else
145                        q = new SingleFloat(-0.0f);
146                } else if (second.minusp())
147                    q = new SingleFloat(-0.0f);
148                else
149                    q = new SingleFloat(0.0f);
150            } else if (q instanceof Fixnum)
151                q = new SingleFloat(((Fixnum)q).value);
152            else
153                q = new SingleFloat(((Bignum)q).floatValue());
154        }
155        thread._values[0] = q;
156        return q;
157    }
158
159    private static final Primitive FTRUNCATE = new ftruncate();
160}
Note: See TracBrowser for help on using the repository browser.