1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
* which can be found in the file CPL.TXT at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
/* rich Mar 28, 2006 10:07:33 AM */
package org.clojure.runtime;
import java.math.BigInteger;
public abstract class Num extends Number implements Comparable{
public final static Num ZERO = from(0);
public final static Num ONE = from(1);
static public Num from(int val)
{
//todo - cache a bunch of small fixnums
return new FixNum(val);
}
static public Num from(double val)
{
return new DoubleNum(val);
}
static public Num from(long val)
{
if(val <= Integer.MAX_VALUE && val >= Integer.MIN_VALUE)
return from((int) val);
else
return new BigNum(val);
}
static public Num from(BigInteger val)
{
if(val.bitLength() < 32)
return from(val.intValue());
else
return new BigNum(val);
}
static public Num from(Object x)
{
if(x instanceof Num)
return (Num) x;
else
{
Class c = x.getClass();
if(c == Integer.class)
return Num.from(((Integer) x).intValue());
else if(c == Double.class || c == Float.class)
return Num.from(((Number) x).doubleValue());
else if(c == Long.class)
return Num.from(((Long) x).longValue());
else if(c == BigInteger.class)
return Num.from((BigInteger) x);
else
return Num.from(((Number) x).intValue());
}
}
static public Num add(Object x, Object y)
{
//if(x instanceof Num && y instanceof Num)
//return ((Num)x).add((Num) y);
return Num.from(x).add(Num.from(y));
}
abstract public Num add(Num rhs);
abstract public Num addTo(int x);
abstract public Num addTo(BigInteger x);
abstract public Num addTo(RatioNum x);
static public Num subtract(Object x, Object y)
{
return Num.from(y).subtractFrom(Num.from(x));
}
//this double-dispatches to addTo(-self)
abstract public Num subtractFrom(Num rhs);
static public Num multiply(Object x, Object y)
{
return Num.from(x).multiplyBy(Num.from(y));
}
abstract public Num multiplyBy(Num rhs);
abstract public Num multiply(int x);
abstract public Num multiply(BigInteger x);
abstract public Num multiply(RatioNum x);
static public Num divide(Object x, Object y)
{
return Num.from(x).divideBy(Num.from(y));
}
abstract public Num divideBy(Num rhs);
abstract public Num divide(int x);
abstract public Num divide(BigInteger x);
abstract public Num divide(RatioNum x);
static public Object truncate(Object num, Object div)
{
return Num.from(div).truncateDivide( Num.from(num));
}
abstract public Object truncateDivide(Num rhs);
abstract public Object truncateBy(int x);
abstract public Object truncateBy(BigInteger x);
abstract public Object truncateBy(RatioNum x);
static public Object truncateBigints(BigInteger n, BigInteger d)
{
BigInteger[] result = n.divideAndRemainder(d);
return RT.setValues(Num.from(result[0]), Num.from(result[1]));
}
static public Num divide(BigInteger n, BigInteger d)
{
BigInteger gcd = n.gcd(d);
if(gcd.equals(BigInteger.ZERO))
return Num.ZERO;
n = n.divide(gcd);
d = d.divide(gcd);
if(d.equals(BigInteger.ONE))
return Num.from(n);
return new RatioNum((IntegerNum) Num.from(d.signum() < 0 ? n.negate() : n),
(IntegerNum) Num.from(d.signum() < 0 ? d.negate() : d));
}
static public boolean equiv(Object x, Object y)
{
return Num.from(x).equiv(Num.from(y));
}
abstract public boolean equiv(Num rhs);
abstract public boolean equivTo(int x);
abstract public boolean equivTo(BigInteger x);
abstract public boolean equivTo(RatioNum x);
static public boolean lt(Object x, Object y)
{
return Num.from(x).lt(Num.from(y));
}
static public boolean lte(Object x, Object y)
{
Num lx = Num.from(x);
Num ly = Num.from(y);
return lx.lt(ly) || lx.equiv(ly);
}
static public boolean gt(Object x, Object y)
{
return Num.from(y).lt(Num.from(x));
}
static public boolean gte(Object x, Object y)
{
Num lx = Num.from(x);
Num ly = Num.from(y);
return ly.lt(lx) || lx.equiv(ly);
}
abstract public boolean lt(Num rhs);
abstract public boolean gt(int x);
abstract public boolean gt(BigInteger x);
abstract public boolean gt(RatioNum x);
static public Num negate(Object x)
{
return Num.from(x).negate();
}
abstract public Num negate();
abstract public boolean minusp();
abstract public boolean plusp();
abstract public Num oneMinus();
abstract public Num onePlus();
public int compareTo(Object object)
{
Num other = Num.from(object);
if(this.equiv(other))
return 0;
else if(this.lt(other))
return -1;
else
return 1;
}
}
|