/
Numbers.cs
680 lines (614 loc) · 16.5 KB
/
Numbers.cs
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#region License
/* Copyright (c) 2007-2016 Llewellyn Pritchard
* All rights reserved.
* This source code is subject to terms and conditions of the BSD License.
* See docs/license.txt. */
#endregion
using System;
using System.Diagnostics;
using System.Globalization;
using IronScheme.Compiler.Numbers;
using IronScheme.Runtime.R6RS;
using Microsoft.Scripting;
using Microsoft.Scripting.Math;
using BigInteger = Oyster.Math.IntX;
namespace IronScheme.Runtime
{
public partial class Builtins
{
[Builtin("decompose-flonum")]
public static object DecomposeFlonum(object flonum)
{
double d = (double)flonum;
BigInteger mantissa;
BigInteger exponent;
bool res = d.GetMantissaAndExponent(out mantissa, out exponent);
if (res)
{
return Values(flonum, mantissa, exponent);
}
else
{
return FALSE;
}
}
[Builtin("string->number", AllowConstantFold = true)]
public static object StringToNumber(object obj)
{
string str = RequiresNotNull<string>(obj);
if (str.Length == 0)
{
return FALSE;
}
Parser number_parser = new Parser();
Scanner number_scanner = new Scanner();
number_parser.scanner = number_scanner;
number_scanner.SetSource(str, 0);
number_parser.result = null;
number_scanner.yy_push_state(3);
try
{
CallTarget0 disp = delegate
{
if (number_parser.Parse())
{
Debug.Assert(number_parser.result != null);
return number_parser.result;
}
else
{
return FALSE;
}
};
CallTarget1 handler = delegate(object o)
{
throw new Continuation();
};
return Runtime.R6RS.Exceptions.WithExceptionHandler(
Closure.Create(handler),
Closure.Create(disp));
}
catch
{
return FALSE;
}
}
[Builtin("string->number", AllowConstantFold = true)]
public static object StringToNumber(object obj, object radix)
{
string str = RequiresNotNull<string>(obj);
int r = RequiresNotNull<int>(radix);
if (str.Length == 0)
{
return FALSE;
}
string prefix = "";
if (str.Length > 1)
{
prefix = str.Substring(0, 2);
}
switch (prefix)
{
case "#b":
case "#o":
case "#x":
return StringToNumber(str);
default:
switch (r)
{
case 2:
return StringToNumber("#b" + str);
case 8:
return StringToNumber("#o" + str);
case 10:
return StringToNumber(str);
case 16:
return StringToNumber("#x" + str);
default:
return FALSE;
}
}
}
[Builtin("inexact=?", AllowConstantFold = true)]
public static object InexactEqual(object a, object b)
{
return GetBool(ConvertToComplex(a) == ConvertToComplex(b));
}
public static int ExactCompare(object a, object b)
{
NumberClass f = GetNumberClass(a);
NumberClass s = GetNumberClass(b);
NumberClass effective = f & s;
switch (effective)
{
case NumberClass.Integer:
return ((int)a).CompareTo((int)b);
case NumberClass.BigInteger:
return ConvertToBigInteger(a).CompareTo(ConvertToBigInteger(b));
case NumberClass.Rational:
return ConvertToRational(a).CompareTo(ConvertToRational(b));
default:
AssertionViolation("exact-compare", "not exact", a, b);
// never reached
return 0;
}
}
public static int InexactCompare(object a, object b)
{
NumberClass f = GetNumberClass(a);
NumberClass s = GetNumberClass(b);
NumberClass effective = f & s;
switch (effective)
{
case NumberClass.Real:
return ((double)a).CompareTo(b);
default:
AssertionViolation("inexact-compare", "not a real", a, b);
// never reached
return 0;
}
}
static bool IsExact(object obj)
{
return obj is int ||
obj is BigInteger ||
obj is Fraction ||
obj is ComplexFraction;
}
#region math
[Builtin("fx+internal", AllowConstantFold = true)]
public static object FxPlusInternal(int a, int b)
{
long r = (long)a + b;
int rr = (int)r;
if (r != rr)
{
return FALSE;
}
return RuntimeHelpers.Int32ToObject(rr);
}
[Builtin("fx-internal", AllowConstantFold = true)]
public static object FxMinusInternal(int a, int b)
{
long r = (long)a - b;
int rr = (int)r;
if (r != rr)
{
return FALSE;
}
return RuntimeHelpers.Int32ToObject(rr);
}
[Builtin("fx*internal", AllowConstantFold = true)]
public static object FxMultiplyInternal(int a, int b)
{
long r = (long)a * b;
int rr = (int)r;
if (r != rr)
{
return FALSE;
}
return RuntimeHelpers.Int32ToObject(rr);
}
[Builtin("fxarithmetic-shift-left-internal", AllowConstantFold = true)]
public static object FxShiftLeftInternal(int a, int b)
{
long r = (long)a << b;
int rr = (int)r;
if (r != rr)
{
return FALSE;
}
return RuntimeHelpers.Int32ToObject(rr);
}
enum NumberClass
{
Complex = 1,
Real = 2 | Complex,
Rational = 4 | Real,
BigInteger = 8 | Rational,
Integer = 16 | BigInteger,
NotANumber = 0
}
static NumberClass GetNumberClass(object obj)
{
if (obj is int)
{
return NumberClass.Integer;
}
else if (obj is double)
{
return NumberClass.Real;
}
else if (obj is BigInteger)
{
return NumberClass.BigInteger;
}
else if (obj is Fraction)
{
return NumberClass.Rational;
}
else if (obj is Complex64 || obj is ComplexFraction)
{
return NumberClass.Complex;
}
else
{
RaiseNumberTypeNotSupported();
return NumberClass.NotANumber;
}
}
static int ConvertToInteger(object o)
{
if (o is int)
{
return (int)o;
}
RaiseNumberTypeNotSupported();
return default(int);
}
private static void RaiseNumberTypeNotSupported()
{
throw new NotSupportedException("number type not supported");
}
protected internal static BigInteger ConvertToBigInteger(object o)
{
if (o is int)
{
return (int)o;
}
if (o is BigInteger)
{
return (BigInteger)o;
}
RaiseNumberTypeNotSupported();
return default(int);
}
static Fraction ConvertToRational(object o)
{
if (o is int)
{
return (int)o;
}
if (o is Fraction)
{
return (Fraction)o;
}
if (o is BigInteger)
{
return new Fraction((BigInteger)o, 1);
}
if (o is double)
{
return (Fraction)(double)o;
}
RaiseNumberTypeNotSupported();
return default(int);
}
static double ConvertToReal(object o)
{
return SafeConvert(o);
}
static Complex64 ConvertToComplex(object o)
{
if (o is Complex64)
{
return (Complex64)o;
}
else if (o is ComplexFraction)
{
return (ComplexFraction)o;
}
else
{
return Complex64.MakeReal(ConvertToReal(o));
}
}
static ComplexFraction ConvertToComplexFraction(object o)
{
if (o is ComplexFraction)
{
return (ComplexFraction)o;
}
else if (o is int)
{
return new ComplexFraction((int)o);
}
else if (o is BigInteger)
{
return new ComplexFraction((BigInteger)o);
}
else
{
return new ComplexFraction((Fraction)o);
}
}
[Builtin("generic+", AllowConstantFold = true)]
public static object Add(object first, object second)
{
NumberClass f = GetNumberClass(first);
NumberClass s = GetNumberClass(second);
NumberClass effective = f & s;
switch (effective)
{
case NumberClass.Integer:
{
long result = (long)ConvertToInteger(first) + ConvertToInteger(second);
int iresult = (int)result;
if (result != iresult)
{
return (BigInteger)result;
}
else
{
return RuntimeHelpers.Int32ToObject(iresult);
}
}
case NumberClass.BigInteger:
return ToIntegerIfPossible(ConvertToBigInteger(first) + ConvertToBigInteger(second));
case NumberClass.Rational:
return IntegerIfPossible(ConvertToRational(first) + ConvertToRational(second));
case NumberClass.Real:
return ConvertToReal(first) + ConvertToReal(second);
case NumberClass.Complex:
if (IsExact(first) && IsExact(second))
{
return IntegerIfPossible(ConvertToComplexFraction(first) + ConvertToComplexFraction(second));
}
return DoubleIfPossible(ConvertToComplex(first) + ConvertToComplex(second));
}
RaiseNumberTypeNotSupported();
return null;
}
[Builtin("generic-", AllowConstantFold = true)]
public static object Subtract(object first, object second)
{
NumberClass f = GetNumberClass(first);
NumberClass s = GetNumberClass(second);
NumberClass effective = f & s;
switch (effective)
{
case NumberClass.Integer:
{
long result = (long)ConvertToInteger(first) - ConvertToInteger(second);
int iresult = (int)result;
if (result != iresult)
{
return (BigInteger)result;
}
else
{
return RuntimeHelpers.Int32ToObject(iresult);
}
}
case NumberClass.BigInteger:
return ToIntegerIfPossible(ConvertToBigInteger(first) - ConvertToBigInteger(second));
case NumberClass.Rational:
return IntegerIfPossible(ConvertToRational(first) - ConvertToRational(second));
case NumberClass.Real:
return ConvertToReal(first) - ConvertToReal(second);
case NumberClass.Complex:
if (IsExact(first) && IsExact(second))
{
return IntegerIfPossible(ConvertToComplexFraction(first) - ConvertToComplexFraction(second));
}
return DoubleIfPossible(ConvertToComplex(first) - ConvertToComplex(second));
}
RaiseNumberTypeNotSupported();
return null;
}
[Builtin("generic*", AllowConstantFold = true)]
public static object Multiply(object first, object second)
{
NumberClass f = GetNumberClass(first);
NumberClass s = GetNumberClass(second);
NumberClass effective = f & s;
switch (effective)
{
case NumberClass.Integer:
{
long result = (long)ConvertToInteger(first) * ConvertToInteger(second);
int iresult = (int)result;
if (result != iresult)
{
return (BigInteger)result;
}
else
{
return RuntimeHelpers.Int32ToObject(iresult);
}
}
case NumberClass.BigInteger:
return ToIntegerIfPossible(ConvertToBigInteger(first) * ConvertToBigInteger(second));
case NumberClass.Rational:
return IntegerIfPossible(ConvertToRational(first) * ConvertToRational(second));
case NumberClass.Real:
return ConvertToReal(first) * ConvertToReal(second);
case NumberClass.Complex:
if (IsExact(first) && IsExact(second))
{
return IntegerIfPossible(ConvertToComplexFraction(first) * ConvertToComplexFraction(second));
}
return DoubleIfPossible(ConvertToComplex(first) * ConvertToComplex(second));
}
RaiseNumberTypeNotSupported();
return null;
}
protected internal static object ToIntegerIfPossible(BigInteger i)
{
if (i.IsInt32)
{
return RuntimeHelpers.Int32ToObject((int)i);
}
else
{
return i;
}
}
[Builtin("generic/", AllowConstantFold = true)]
public static object Divide(object first, object second)
{
NumberClass f = GetNumberClass(first);
NumberClass s = GetNumberClass(second);
NumberClass effective = f & s;
try
{
switch (effective)
{
case NumberClass.Integer:
case NumberClass.BigInteger:
return IntegerIfPossible(new Fraction(ConvertToBigInteger(first), ConvertToBigInteger(second)));
case NumberClass.Rational:
return IntegerIfPossible(ConvertToRational(first)/ConvertToRational(second));
case NumberClass.Real:
return ConvertToReal(first)/ConvertToReal(second);
case NumberClass.Complex:
if (IsExact(first) && IsExact(second))
{
return IntegerIfPossible(ConvertToComplexFraction(first)/ConvertToComplexFraction(second));
}
return DoubleIfPossible(ConvertToComplex(first)/ConvertToComplex(second));
}
}
catch (DivideByZeroException)
{
return AssertionViolation("/", "divide by zero", first);
}
RaiseNumberTypeNotSupported();
return null;
}
#endregion
static double SafeConvert(object obj)
{
try
{
if (obj is int)
{
return (int)obj;
}
if (obj is double)
{
return (double)obj;
}
if (obj is BigInteger)
{
return ((BigInteger)obj).ToFloat64();
}
if (obj is Fraction)
{
return ((Fraction)obj).ToDouble(null);
}
if (obj is Complex64)
{
Complex64 c = (Complex64)obj;
if (c.Imag == 0.0)
{
return c.Real;
}
else
{
return (double)AssertionViolation(GetCaller(), "no conversion to real possible", obj);
}
}
//System.Diagnostics.Trace.Assert(!(obj is string));
return Convert.ToDouble(obj, CultureInfo.InvariantCulture);
}
catch (OverflowException)
{
var i = (BigInteger)obj;
return i < 0 ? double.NegativeInfinity : double.PositiveInfinity;
}
}
// todo: this can probably be easily ported to Scheme
//based on lsqrt()
[Builtin("bignum-sqrt", AllowConstantFold = true)]
public static object SqrtBigInteger(object num)
{
BigInteger x = (BigInteger)num;
BigInteger v0, q0, x1;
if (x <= 1)
{
return x;
}
v0 = x;
x = x / 2;
while (true)
{
q0 = v0 / x;
x1 = (x + q0) / 2;
if (q0 >= x)
break;
x = x1;
}
if (x1 * x1 != v0)
{
return Math.Sqrt(v0.ToFloat64());
}
return x1;
}
// todo: this can probably be easily ported to Scheme
[Builtin("bignum-sqrt-exact", AllowConstantFold = true)]
public static object ExactSqrtBigInteger(object num)
{
BigInteger x = (BigInteger)num;
BigInteger v0, q0, x1;
if (x <= 1)
{
return x;
}
v0 = x;
x = x / 2;
while (true)
{
q0 = v0 / x;
x1 = (x + q0) / 2;
if (q0 >= x)
break;
x = x1;
}
q0 = x1 * x1;
if (q0 > v0)
{
x1 = x1 - 1;
q0 = x1 * x1;
}
return Values(ToIntegerIfPossible(x1), ToIntegerIfPossible(v0 - q0));
}
static object DoubleIfPossible(object res)
{
if (res is Complex64)
{
var c = (Complex64)res;
if (c.Imag == 0)
{
return c.Real;
}
}
return res;
}
// improve actually
static object IntegerIfPossible(object res)
{
if (res is BigInteger)
{
return ToIntegerIfPossible((BigInteger)res);
}
else if (res is Fraction)
{
Fraction f = (Fraction)res;
if (f.Denominator == 1)
{
return ToIntegerIfPossible(f.Numerator);
}
}
else if (res is ComplexFraction)
{
ComplexFraction cf = (ComplexFraction)res;
if (cf.Imag == 0)
{
return IntegerIfPossible(cf.Real);
}
}
return res;
}
}
}