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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/**
* 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 */
using System;
using java.math;
namespace org.clojure.runtime
{
public abstract class Num : IComparable , IConvertible
{
public static Num ZERO = from(0);
public 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 <= Int32.MaxValue && val >= Int32.MinValue)
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);
}
internal static BigInteger BIGTEN = BigInteger.valueOf(10);
static public Num from(Object x)
{
if(x is Num)
return (Num) x;
else
{
IConvertible c = x as IConvertible;
if(c != null)
{
switch(c.GetTypeCode())
{
case TypeCode.Int32:
return Num.from((Int32) x);
case TypeCode.Double:
case TypeCode.Single:
return Num.from(Convert.ToDouble(x));
case TypeCode.Int64:
return Num.from((Int64) x);
//TODO: Decimal w/o string conversion
case TypeCode.Decimal:
BigDecimal d = new BigDecimal(x.ToString());
return Num.divide(d.movePointRight(d.scale()).toBigInteger(),
BIGTEN.pow(d.scale()));
default:
return Num.from(Convert.ToInt32(x));
}
}
else if(x is BigInteger)
return Num.from((BigInteger) x);
else
throw new ArgumentException("Cannot convert argument: " + x + " to Num");
}
}
virtual public byte byteValue()
{
return checked((byte)intValue());
}
virtual public short shortValue()
{
return checked((short)intValue());
}
abstract public double doubleValue();
abstract public float floatValue();
abstract public int intValue();
abstract public long longValue();
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(ThreadLocalData tld, Object num, Object div)
{
return Num.from(div).truncateDivide(tld, Num.from(num));
}
abstract public Object truncateDivide(ThreadLocalData tld, Num rhs);
abstract public Object truncateBy(ThreadLocalData tld, int x);
abstract public Object truncateBy(ThreadLocalData tld, BigInteger x);
abstract public Object truncateBy(ThreadLocalData tld, RatioNum x);
static public Object truncateBigints(ThreadLocalData tld, BigInteger n, BigInteger d)
{
BigInteger[] result = n.divideAndRemainder(d);
return RT.setValues(tld, Num.from(result[0]), Num.from(result[1]));
}
internal static BigInteger BIG_ONE = BigInteger.valueOf(1);
internal static BigInteger BIG_ZERO = BigInteger.valueOf(0);
static public Num divide(BigInteger n, BigInteger d)
{
BigInteger gcd = n.gcd(d);
if(gcd.Equals(BIG_ZERO))
return Num.ZERO;
n = n.divide(gcd);
d = d.divide(gcd);
if(d.Equals(BIG_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 obj)
{
Num other = Num.from(obj);
if(this.
|