KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > floor


1 /*
2  * floor.java
3  *
4  * Copyright (C) 2004 Peter Graves
5  * $Id: floor.java,v 1.1 2004/03/09 11:49:37 piso Exp $
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
22 package org.armedbear.lisp;
23
24 // ### floor number &optional divisor
25
public final class floor extends Primitive
26 {
27     private floor()
28     {
29         super("floor", "number &optional divisor");
30     }
31
32     public LispObject execute(LispObject number)
33         throws ConditionThrowable
34     {
35         LispObject quotient = number.truncate(Fixnum.ONE);
36         final LispThread thread = LispThread.currentThread();
37         LispObject remainder = thread._values[1];
38         if (!remainder.zerop()) {
39             if (number.minusp()) {
40                 quotient = quotient.decr();
41                 remainder = remainder.incr();
42                 thread._values[0] = quotient;
43                 thread._values[1] = remainder;
44             }
45         }
46         return quotient;
47     }
48
49     public LispObject execute(LispObject number, LispObject divisor)
50         throws ConditionThrowable
51     {
52         LispObject quotient = number.truncate(divisor);
53         final LispThread thread = LispThread.currentThread();
54         LispObject remainder = thread._values[1];
55         boolean adjust = false;
56         if (!remainder.zerop()) {
57             if (divisor.minusp()) {
58                 if (number.plusp())
59                     adjust = true;
60             } else {
61                 if (number.minusp())
62                     adjust = true;
63             }
64         }
65         if (adjust) {
66             quotient = quotient.decr();
67             remainder = remainder.add(divisor);
68             thread._values[0] = quotient;
69             thread._values[1] = remainder;
70         }
71         return quotient;
72     }
73
74     private static final Primitive FLOOR = new floor();
75 }
76
Popular Tags