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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
|
#include <fenv.h>
#include <gc.h>
#include <gmp.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "bigint.h"
#include "datatypes.h"
#include "floats.h"
#include "reals.h"
#include "text.h"
#include "types.h"
// Check if num is boxed pointer
public
CONSTFUNC bool Real$is_boxed(Real_t n) {
return (n.bits & QNAN_MASK) == 0;
}
public
CONSTFUNC uint64_t Real$tag(Real_t n) {
return n.bits & TAG_MASK;
}
static inline Real_t R(double d) {
union {
volatile double d;
volatile uint64_t bits;
} input = {.d = d};
return (Real_t){.bits = input.bits ^ QNAN_MASK};
}
static inline Real_t mpq_to_real(__mpq_struct mpq) {
volatile rational_t *r = GC_MALLOC(sizeof(rational_t));
r->value = mpq;
return (Real_t){.rational = (void *)r + REAL_TAG_RATIONAL};
}
static inline Real_t _sym_to_real(symbolic_t sym) {
volatile symbolic_t *s = GC_MALLOC(sizeof(symbolic_t));
*s = sym;
return (Real_t){.symbolic = (void *)s + REAL_TAG_SYMBOLIC};
}
#define sym_to_real(...) _sym_to_real((symbolic_t){__VA_ARGS__})
public
Real_t Real$from_int64(int64_t i) {
double d = (double)i;
if ((int64_t)d == i) return R(d);
volatile Int_t *b = GC_MALLOC(sizeof(Int_t));
*b = I(i);
return (Real_t){.bigint = (void *)b + REAL_TAG_BIGINT};
}
public
Real_t Real$from_rational(int64_t num, int64_t den) {
__mpq_struct ret;
mpq_init(&ret);
if (den == INT64_MIN) fail("Domain error");
if (den < 0) {
if (num == INT64_MIN) fail("Domain error");
num = -num;
den = -den;
}
if (den == 0) fail("Division by zero");
mpq_set_si(&ret, num, (unsigned long)den);
mpq_canonicalize(&ret);
return mpq_to_real(ret);
}
public
bool Real$get_rational(Real_t x, int64_t *num, int64_t *den) {
if (!Real$is_boxed(x) || Real$tag(x) != REAL_TAG_RATIONAL) {
return false;
}
rational_t *r = REAL_RATIONAL(x);
// Check if both numerator and denominator fit in int64_t
if (!mpz_fits_slong_p(mpq_numref(&r->value)) || !mpz_fits_slong_p(mpq_denref(&r->value))) {
return false;
}
if (num) *num = mpz_get_si(mpq_numref(&r->value));
if (den) *den = mpz_get_si(mpq_denref(&r->value));
return true;
}
// Promote double to exact type if needed
static inline Real_t Real$as_rational(double d) {
if (!isfinite(d)) {
return R(d);
}
__mpq_struct ret;
mpq_init(&ret);
mpq_set_d(&ret, d);
return mpq_to_real(ret);
}
public
CONSTFUNC Real_t Real$from_float64(double n) {
return R(n);
}
public
Real_t Real$from_int(Int_t i) {
double d = Float64$from_int(i, true);
if (Int$equal_value(i, Int$from_float64(d, true))) return R(d);
volatile Int_t *b = GC_MALLOC(sizeof(Int_t));
*b = i;
return (Real_t){.bigint = (void *)b + REAL_TAG_BIGINT};
}
public
double Real$as_float64(Real_t n, bool truncate) {
if (!Real$is_boxed(n)) {
return REAL_DOUBLE(n);
}
switch (Real$tag(n)) {
case REAL_TAG_BIGINT: {
return Float64$from_int(*REAL_BIGINT(n), truncate);
}
case REAL_TAG_RATIONAL: {
rational_t *rational = REAL_RATIONAL(n);
return mpq_get_d(&rational->value);
}
case REAL_TAG_CONSTRUCTIVE: {
constructive_t *c = REAL_CONSTRUCTIVE(n);
return c->compute(c->context, 53);
}
case REAL_TAG_SYMBOLIC: {
symbolic_t *s = REAL_SYMBOLIC(n);
double left = Real$as_float64(s->left, truncate);
double right = Real$as_float64(s->right, truncate);
switch (s->op) {
case SYM_INVALID: fail("Invalid number!");
case SYM_ADD: return left + right;
case SYM_SUB: return left - right;
case SYM_MUL: return left * right;
case SYM_DIV: return left / right;
case SYM_MOD: return Float64$mod(left, right);
case SYM_SQRT: return sqrt(left);
case SYM_POW: return pow(left, right);
case SYM_SIN: return sin(left);
case SYM_COS: return cos(left);
case SYM_TAN: return tan(left);
case SYM_ASIN: return asin(left);
case SYM_ACOS: return acos(left);
case SYM_ATAN: return atan(left);
case SYM_ATAN2: return atan2(left, right);
case SYM_EXP: return exp(left);
case SYM_LOG: return log(left);
case SYM_LOG10: return log10(left);
case SYM_ABS: return fabs(left);
case SYM_FLOOR: return floor(left);
case SYM_CEIL: return ceil(left);
case SYM_PI: return M_PI;
case SYM_TAU: return 2. * M_PI;
case SYM_E: return M_E;
default: return NAN;
}
}
default: return NAN;
}
}
symbolic_t pi_symbol = {.op = SYM_PI};
public
Real_t Real$pi = {.symbolic = (void *)&pi_symbol + REAL_TAG_SYMBOLIC};
symbolic_t tau_symbol = {.op = SYM_TAU};
public
Real_t Real$tau = {.symbolic = (void *)&tau_symbol + REAL_TAG_SYMBOLIC};
symbolic_t e_symbol = {.op = SYM_E};
public
Real_t Real$e = {.symbolic = (void *)&e_symbol + REAL_TAG_SYMBOLIC};
public
Real_t Real$plus(Real_t a, Real_t b) {
if (Real$is_zero(a)) return b;
else if (Real$is_zero(b)) return a;
if (!Real$is_boxed(a) && !Real$is_boxed(b)) {
feclearexcept(FE_INEXACT);
volatile double result = REAL_DOUBLE(a) + REAL_DOUBLE(b);
if (!fetestexcept(FE_INEXACT) && isfinite(result)) {
return R(result);
}
}
if (!Real$is_boxed(a)) a = Real$as_rational(REAL_DOUBLE(a));
if (!Real$is_boxed(b)) b = Real$as_rational(REAL_DOUBLE(b));
// Handle exact rational arithmetic
if (Real$tag(a) == REAL_TAG_RATIONAL && Real$tag(b) == REAL_TAG_RATIONAL) {
__mpq_struct ret;
mpq_init(&ret);
mpq_add(&ret, &REAL_RATIONAL(a)->value, &REAL_RATIONAL(b)->value);
return mpq_to_real(ret);
}
// Fallback: create symbolic expression
return sym_to_real(.op = SYM_ADD, .left = a, .right = b);
}
public
Real_t Real$minus(Real_t a, Real_t b) {
if (Real$is_zero(b)) return a;
else if (Real$is_zero(a)) return Real$negative(b);
if (!Real$is_boxed(a) && !Real$is_boxed(b)) {
feclearexcept(FE_INEXACT);
volatile double result = REAL_DOUBLE(a) - REAL_DOUBLE(b);
if (!fetestexcept(FE_INEXACT) && isfinite(result)) {
return R(result);
}
}
if (!Real$is_boxed(a)) a = Real$as_rational(REAL_DOUBLE(a));
if (!Real$is_boxed(b)) b = Real$as_rational(REAL_DOUBLE(b));
if (Real$tag(a) == REAL_TAG_RATIONAL && Real$tag(b) == REAL_TAG_RATIONAL) {
__mpq_struct ret;
mpq_init(&ret);
mpq_sub(&ret, &REAL_RATIONAL(a)->value, &REAL_RATIONAL(b)->value);
return mpq_to_real(ret);
}
return sym_to_real(.op = SYM_SUB, .left = a, .right = b);
}
public
Real_t Real$times(Real_t a, Real_t b) {
if (!Real$is_boxed(a) && REAL_DOUBLE(a) == 1.0) return b;
if (!Real$is_boxed(b) && REAL_DOUBLE(b) == 1.0) return a;
if (!Real$is_boxed(a) && !Real$is_boxed(b)) {
feclearexcept(FE_INEXACT);
volatile double result = REAL_DOUBLE(a) * REAL_DOUBLE(b);
if (!fetestexcept(FE_INEXACT) && isfinite(result)) {
return R(result);
}
}
if (!Real$is_boxed(a)) a = Real$as_rational(REAL_DOUBLE(a));
if (!Real$is_boxed(b)) b = Real$as_rational(REAL_DOUBLE(b));
if (Real$tag(a) == REAL_TAG_RATIONAL && Real$tag(b) == REAL_TAG_RATIONAL) {
rational_t *ra = REAL_RATIONAL(a);
rational_t *rb = REAL_RATIONAL(b);
__mpq_struct ret;
mpq_init(&ret);
mpq_mul(&ret, &ra->value, &rb->value);
return mpq_to_real(ret);
}
// Check for sqrt(x) * sqrt(x) = x
if (Real$tag(a) == REAL_TAG_SYMBOLIC && Real$tag(b) == REAL_TAG_SYMBOLIC) {
symbolic_t *sa = REAL_SYMBOLIC(a);
symbolic_t *sb = REAL_SYMBOLIC(b);
if (sa->op == SYM_SQRT && sb->op == SYM_SQRT) {
// Check if arguments are equal
if (Real$equal_values(sa->left, sb->left)) {
return sa->left;
}
}
}
return sym_to_real(.op = SYM_MUL, .left = a, .right = b);
}
public
Real_t Real$divided_by(Real_t a, Real_t b) {
if (REAL_DOUBLE(b) == 1.0) return a;
if (!Real$is_boxed(a) && !Real$is_boxed(b)) {
feclearexcept(FE_INEXACT);
volatile double result = REAL_DOUBLE(a) / REAL_DOUBLE(b);
if (!fetestexcept(FE_INEXACT) && isfinite(result)) {
return R(result);
}
}
if (!Real$is_boxed(a)) a = Real$as_rational(REAL_DOUBLE(a));
if (!Real$is_boxed(b)) b = Real$as_rational(REAL_DOUBLE(b));
if (Real$tag(a) == REAL_TAG_RATIONAL && Real$tag(b) == REAL_TAG_RATIONAL) {
rational_t *ra = REAL_RATIONAL(a);
rational_t *rb = REAL_RATIONAL(b);
__mpq_struct ret;
mpq_init(&ret);
mpq_div(&ret, &ra->value, &rb->value);
return mpq_to_real(ret);
}
// volatile Real_t ret = sym_to_real(.op = SYM_DIV, .left = a, .right = b);
volatile Real_t ret = {};
volatile symbolic_t *s = GC_MALLOC(sizeof(symbolic_t));
s->op = SYM_DIV;
s->left = a;
s->right = b;
ret.symbolic = (void *)s + REAL_TAG_SYMBOLIC;
assert(REAL_SYMBOLIC(ret)->op == SYM_DIV);
// return (Real_t){.symbolic = (void *)s + REAL_TAG_SYMBOLIC};
print("sym: ", ret);
return ret;
// return sym_to_real(.op = SYM_DIV, .left = a, .right = b);
}
public
Real_t Real$mod(Real_t n, Real_t modulus) {
// Euclidean division, see:
// https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf
// For fast path with doubles
if (!Real$is_boxed(n) && !Real$is_boxed(modulus)) {
double r = remainder(REAL_DOUBLE(n), REAL_DOUBLE(modulus));
r -= (r < 0.0) * (2.0 * (REAL_DOUBLE(modulus) < 0.0) - 1.0) * REAL_DOUBLE(modulus);
return R(r);
}
// For rationals, compute exactly
if (Real$tag(n) == REAL_TAG_RATIONAL && Real$tag(modulus) == REAL_TAG_RATIONAL) {
rational_t *rn = REAL_RATIONAL(n);
rational_t *rm = REAL_RATIONAL(modulus);
// Compute n - floor(n/modulus) * modulus
mpq_t quotient, floored, result;
mpq_init(quotient);
mpq_init(floored);
mpq_init(result);
mpq_div(quotient, &rn->value, &rm->value);
// Floor the quotient
mpz_t floor_z;
mpz_init(floor_z);
mpz_fdiv_q(floor_z, mpq_numref(quotient), mpq_denref(quotient));
mpq_set_z(floored, floor_z);
// result = n - floor * modulus
mpq_mul(result, floored, &rm->value);
mpq_sub(result, &rn->value, result);
// Apply Euclidean correction
if (mpq_sgn(result) < 0) {
if (mpq_sgn(&rm->value) < 0) {
mpq_sub(result, result, &rm->value);
} else {
mpq_add(result, result, &rm->value);
}
}
__mpq_struct ret;
mpq_init(&ret);
mpq_set(&ret, result);
mpq_clear(quotient);
mpq_clear(floored);
mpq_clear(result);
mpz_clear(floor_z);
return mpq_to_real(ret);
}
// Fallback to symbolic
return sym_to_real(.op = SYM_MOD, .left = n, .right = modulus);
}
public
Real_t Real$mod1(Real_t n, Real_t modulus) {
Real_t one = R(1.0);
return Real$plus(one, Real$mod(Real$minus(n, one), modulus));
}
public
Real_t Real$mix(Real_t amount, Real_t x, Real_t y) {
// mix = (1 - amount) * x + amount * y
Real_t one = R(1.0);
Real_t one_minus_amount = Real$minus(one, amount);
Real_t left_term = Real$times(one_minus_amount, x);
Real_t right_term = Real$times(amount, y);
return Real$plus(left_term, right_term);
}
public
bool Real$is_between(Real_t x, Real_t low, Real_t high) {
return Real$compare_values(low, x) <= 0 && Real$compare_values(x, high) <= 0;
}
public
Real_t Real$clamped(Real_t x, Real_t low, Real_t high) {
if (Real$compare_values(x, low) <= 0) return low;
if (Real$compare_values(x, high) >= 0) return high;
return x;
}
public
Real_t Real$sqrt(Real_t a) {
if (!Real$is_boxed(a)) {
feclearexcept(FE_INEXACT);
volatile double d = sqrt(REAL_DOUBLE(a));
volatile double check = d * d;
if (!fetestexcept(FE_INEXACT) && check == REAL_DOUBLE(a)) {
return R(d);
}
}
// Check for perfect square in rationals
if (Real$tag(a) == REAL_TAG_RATIONAL) {
rational_t *r = REAL_RATIONAL(a);
mpz_t num_sqrt, den_sqrt;
mpz_init(num_sqrt);
mpz_init(den_sqrt);
if (mpz_perfect_square_p(mpq_numref(&r->value)) && mpz_perfect_square_p(mpq_denref(&r->value))) {
mpz_sqrt(num_sqrt, mpq_numref(&r->value));
mpz_sqrt(den_sqrt, mpq_denref(&r->value));
__mpq_struct ret;
mpq_init(&ret);
mpq_set_num(&ret, num_sqrt);
mpq_set_den(&ret, den_sqrt);
mpq_canonicalize(&ret);
mpz_clear(num_sqrt);
mpz_clear(den_sqrt);
return mpq_to_real(ret);
}
mpz_clear(num_sqrt);
mpz_clear(den_sqrt);
}
return sym_to_real(.op = SYM_SQRT, .left = a, .right = R(0));
}
// Because the libm implementation of pow() is often inexact for common
// cases like `10^2` due to its implementation details, this wrapper will
// try to find cases where it's not too hard to get exact results and do
// that instead, falling back to inexact pow() when necessary.
static CONSTFUNC double less_inexact_pow(double base, double exponent) {
if (exponent == 0.0) return 1.0;
if (base == 0.0) return exponent > 0.0 ? 0.0 : HUGE_VAL;
if (exponent == 1.0) return base;
if (exponent == 0.5) return sqrt(base);
// Integer or half-integer path (x^i or x^(i+.5), where i is an integer)
double int_part = trunc(exponent);
double frac_part = exponent - int_part;
if ((frac_part == 0.0 || frac_part == 0.5) && fabs(int_part) <= INT64_MAX) {
int64_t n = (int64_t)fabs(int_part);
double result = 1.0, b = base;
while (n) {
if (n & 1) result *= b;
b *= b;
n >>= 1;
}
if (frac_part == 0.5) result *= sqrt(base);
return int_part < 0.0 ? 1.0 / result : result;
}
return pow(base, exponent);
}
public
Real_t Real$power(Real_t base, Real_t exp) {
if (!Real$is_boxed(base) && !Real$is_boxed(exp)) {
feclearexcept(FE_INEXACT);
volatile double result = less_inexact_pow(REAL_DOUBLE(base), REAL_DOUBLE(exp));
if (!fetestexcept(FE_INEXACT) && isfinite(result)) {
return R(result);
}
}
return sym_to_real(.op = SYM_POW, .left = base, .right = exp);
}
// Helper function for binary functions
static Text_t format_binary_func(Text_t left, Text_t right, const char *func_name, bool colorize) {
const char *operator_color = colorize ? "\033[33m" : "";
const char *paren_color = colorize ? "\033[37m" : "";
const char *reset = colorize ? "\033[m" : "";
return colorize ? Texts(operator_color, func_name, paren_color, "(", reset, left, operator_color, ", ", reset,
right, paren_color, ")", reset)
: Texts(func_name, "(", left, ", ", right, ")");
}
// Helper function for constants
static Text_t format_constant(const char *symbol, bool colorize) {
const char *number_color = colorize ? "\033[35m" : "";
const char *reset = colorize ? "\033[m" : "";
return colorize ? Texts(number_color, symbol, reset) : Text$from_str(symbol);
}
public
Text_t Real$as_text(const void *n, bool colorize, const TypeInfo_t *type) {
(void)type;
if (n == NULL) return Text("Real");
Real_t num = *(Real_t *)n;
// ANSI color codes
const char *number_color = colorize ? "\033[35m" : ""; // magenta for numbers
const char *operator_color = colorize ? "\033[33m" : ""; // yellow for operators
const char *reset = colorize ? "\033[m" : "";
if (!Real$is_boxed(num)) {
char buf[64];
snprintf(buf, sizeof(buf), "%.17g", REAL_DOUBLE(num));
return colorize ? Texts(number_color, buf, reset) : Text$from_str(buf);
}
switch (Real$tag(num)) {
case REAL_TAG_BIGINT: {
Int_t *b = REAL_BIGINT(num);
Text_t int_text = Int$value_as_text(*b);
return colorize ? Texts(number_color, int_text, reset) : int_text;
}
case REAL_TAG_RATIONAL: {
rational_t *r = REAL_RATIONAL(num);
// Check if denominator is 1 (integer)
if (mpz_cmp_ui(mpq_denref(&r->value), 1) == 0) {
char *str = mpz_get_str(NULL, 10, mpq_numref(&r->value));
Text_t result = colorize ? Texts(number_color, str, reset) : Text$from_str(str);
return result;
}
// Check if denominator is 2^a * 5^b (terminates in decimal)
mpz_t den_copy;
mpz_init_set(den_copy, mpq_denref(&r->value));
while (mpz_divisible_ui_p(den_copy, 2)) {
mpz_divexact_ui(den_copy, den_copy, 2);
}
while (mpz_divisible_ui_p(den_copy, 5)) {
mpz_divexact_ui(den_copy, den_copy, 5);
}
bool is_terminating_decimal = (mpz_cmp_ui(den_copy, 1) == 0);
mpz_clear(den_copy);
if (is_terminating_decimal) {
// Compute exact decimal representation
mpz_t num_scaled, den_temp;
mpz_init_set(num_scaled, mpq_numref(&r->value));
mpz_init_set(den_temp, mpq_denref(&r->value));
size_t decimal_places = 0;
while (mpz_cmp_ui(den_temp, 1) != 0) {
mpz_mul_ui(num_scaled, num_scaled, 10);
if (mpz_divisible_ui_p(den_temp, 2)) mpz_divexact_ui(den_temp, den_temp, 2);
else if (mpz_divisible_ui_p(den_temp, 5)) mpz_divexact_ui(den_temp, den_temp, 5);
else break;
decimal_places++;
}
mpz_divexact(num_scaled, num_scaled, mpq_denref(&r->value));
char *num_str = mpz_get_str(NULL, 10, num_scaled);
size_t len = strlen(num_str);
bool is_negative = (num_str[0] == '-');
size_t start = is_negative ? 1 : 0;
char buf[256];
if (len - start <= decimal_places) {
int leading_zeros = decimal_places - (len - start);
if (leading_zeros > 0) {
snprintf(buf, sizeof(buf), "%s0.%0*d%s", is_negative ? "-" : "", leading_zeros, 0, num_str + start);
} else {
snprintf(buf, sizeof(buf), "%s0.%s", is_negative ? "-" : "", num_str + start);
}
} else {
int int_len = len - start - decimal_places;
snprintf(buf, sizeof(buf), "%.*s.%s", (int)start + int_len, num_str, num_str + start + int_len);
}
// Strip trailing zeros after decimal point
size_t buflen = strlen(buf);
if (strchr(buf, '.')) {
while (buflen > 0 && buf[buflen - 1] == '0') {
buf[--buflen] = '\0';
}
if (buflen > 0 && buf[buflen - 1] == '.') {
buf[--buflen] = '\0';
}
}
mpz_clear(num_scaled);
mpz_clear(den_temp);
return colorize ? Texts(number_color, buf, reset) : Text$from_str(buf);
}
// Not a decimal, show as fraction
char *num_str = mpz_get_str(NULL, 10, mpq_numref(&r->value));
char *den_str = mpz_get_str(NULL, 10, mpq_denref(&r->value));
Text_t result = colorize ? Texts(number_color, num_str, operator_color, "/", number_color, den_str, reset)
: Texts(num_str, "/", den_str);
return result;
}
case REAL_TAG_CONSTRUCTIVE: {
constructive_t *c = REAL_CONSTRUCTIVE(num);
double approx = c->compute(c->context, 53);
char buf[64];
snprintf(buf, sizeof(buf), "~%.17g", approx);
return colorize ? Texts(operator_color, "~", number_color, buf + 1, reset) : Text$from_str(buf);
}
case REAL_TAG_SYMBOLIC: {
symbolic_t *s = REAL_SYMBOLIC(num);
const char *func = NULL;
const char *binop = NULL;
switch (s->op) {
case SYM_INVALID: return Text("INVALID REAL NUMBER");
case SYM_ADD: binop = " + "; break;
case SYM_SUB: binop = " - "; break;
case SYM_MUL: binop = " * "; break;
case SYM_DIV: binop = " / "; break;
case SYM_MOD: binop = " mod "; break;
case SYM_SQRT: func = "sqrt"; break;
case SYM_POW: binop = "^"; break;
case SYM_SIN: func = "sin"; break;
case SYM_COS: func = "cos"; break;
case SYM_TAN: func = "tan"; break;
case SYM_ASIN: func = "asin"; break;
case SYM_ACOS: func = "acos"; break;
case SYM_ATAN: func = "atan"; break;
case SYM_ATAN2: {
Text_t left = Real$as_text(&s->left, colorize, type);
Text_t right = Real$as_text(&s->right, colorize, type);
return format_binary_func(left, right, "atan2", colorize);
}
case SYM_EXP: func = "exp"; break;
case SYM_LOG: func = "log"; break;
case SYM_LOG10: func = "log10"; break;
case SYM_ABS: func = "abs"; break;
case SYM_FLOOR: func = "floor"; break;
case SYM_CEIL: func = "ceil"; break;
case SYM_PI: return format_constant("π", colorize);
case SYM_TAU: return format_constant("τ", colorize);
case SYM_E: return format_constant("e", colorize);
default: {
Text_t left = Real$as_text(&s->left, colorize, type);
Text_t right = Real$as_text(&s->right, colorize, type);
return format_binary_func(left, right, "???", colorize);
}
}
const char *paren_color = colorize ? "\033[37m" : "";
if (func) {
Text_t arg = Real$as_text(&s->left, colorize, type);
return colorize ? Texts(operator_color, func, paren_color, "(", reset, arg, paren_color, ")", reset)
: Texts(func, "(", arg, ")");
} else {
Text_t left = Real$as_text(&s->left, colorize, type);
Text_t right = Real$as_text(&s->right, colorize, type);
return colorize ? Texts(paren_color, "(", reset, left, operator_color, binop, reset, right, paren_color,
")", reset)
: Texts("(", left, binop, right, ")");
}
}
default: return colorize ? Texts(operator_color, "NaN", reset) : Text("NaN");
}
}
public
Text_t Real$value_as_text(Real_t n) {
return Real$as_text(&n, false, &Real$info);
}
public
OptionalReal_t Real$parse(Text_t text, Text_t *remainder) {
OptionalInt_t int_part = Int$parse(text, I(10), &text);
if (int_part.small == 0) {
if (Text$starts_with(text, Text("."), NULL)) {
int_part = I(0);
} else {
if (remainder) *remainder = text;
return NONE_REAL;
}
}
Real_t ret = Real$from_int(int_part);
Text_t after_decimal;
if (Text$starts_with(text, Text("."), &after_decimal)) {
text = after_decimal;
// Count zeroes:
TextIter_t state = NEW_TEXT_ITER_STATE(text);
int64_t i = 0, digits = 0;
for (; i < text.length; i++) {
int32_t g = Text$get_grapheme_fast(&state, i);
if ('0' <= g && g <= '9') digits += 1;
else if (g == '_') continue;
else break;
}
if (digits > 0) {
// n = int_part + fractional_part / 10^digits
OptionalInt_t fractional_part = Int$parse(text, I(10), &after_decimal);
if (fractional_part.small != 0 && !Int$is_zero(fractional_part)) {
Real_t frac = Real$from_int(fractional_part);
Real_t pow10 = Real$power(R(10.), R((double)digits));
Real_t excess = Real$divided_by(frac, pow10);
ret = Int$is_negative(int_part) ? Real$minus(ret, excess) : Real$plus(ret, excess);
}
}
text = after_decimal;
}
Text_t after_exp;
if (Text$starts_with(text, Text("e"), &after_exp) || Text$starts_with(text, Text("E"), &after_exp)) {
OptionalInt_t exponent = Int$parse(after_exp, I(10), &after_exp);
if (exponent.small != 0) {
// n *= 10^exp
if (!Int$is_zero(exponent)) {
if (Int$is_negative(exponent)) {
ret = Real$divided_by(ret, Real$power(R(10.), Real$from_int(Int$negative(exponent))));
} else {
ret = Real$times(ret, Real$power(R(10.), Real$from_int(exponent)));
}
}
text = after_exp;
}
}
if (remainder) *remainder = text;
else if (text.length > 0) {
return NONE_REAL;
}
return ret;
}
public
PUREFUNC bool Real$is_none(const void *vn, const TypeInfo_t *type) {
(void)type;
Real_t n = *(Real_t *)vn;
return n.bits == REAL_TAG_NONE;
}
// Equality check (may be undecidable for some symbolics)
public
bool Real$equal_values(Real_t a, Real_t b) {
if (a.bits == b.bits) return true;
if (!Real$is_boxed(a) && !Real$is_boxed(b)) return REAL_DOUBLE(a) == REAL_DOUBLE(b);
if (!Real$is_boxed(a)) a = Real$as_rational(REAL_DOUBLE(a));
if (!Real$is_boxed(b)) b = Real$as_rational(REAL_DOUBLE(b));
if (Real$tag(a) == REAL_TAG_RATIONAL && Real$tag(b) == REAL_TAG_RATIONAL) {
rational_t *ra = REAL_RATIONAL(a);
rational_t *rb = REAL_RATIONAL(b);
return mpq_equal(&ra->value, &rb->value);
}
// Refine symbolics until separation or timeout
double da = Real$as_float64(a, true);
double db = Real$as_float64(b, true);
return fabs(da - db) < 1e-15;
}
public
bool Real$equal(const void *va, const void *vb, const TypeInfo_t *t) {
(void)t;
Real_t a = *(Real_t *)va;
Real_t b = *(Real_t *)vb;
return Real$equal_values(a, b);
}
public
uint64_t Real$hash(const void *vr, const TypeInfo_t *type) {
(void)type;
Text_t text = Real$value_as_text(*(Real_t *)vr);
return Text$hash(&text, &Text$info);
}
// Comparison: -1 (less), 0 (equal), 1 (greater)
public
int32_t Real$compare_values(Real_t a, Real_t b) {
if (!Real$is_boxed(a) && !Real$is_boxed(b)) {
return (REAL_DOUBLE(a) > REAL_DOUBLE(b)) - (REAL_DOUBLE(a) < REAL_DOUBLE(b));
}
if (Real$tag(a) == REAL_TAG_RATIONAL && Real$tag(b) == REAL_TAG_RATIONAL) {
rational_t *ra = REAL_RATIONAL(a);
rational_t *rb = REAL_RATIONAL(b);
return mpq_cmp(&ra->value, &rb->value);
}
double da = Real$as_float64(a, true);
double db = Real$as_float64(b, true);
return (da > db) - (da < db);
}
public
int32_t Real$compare(const void *va, const void *vb, const TypeInfo_t *t) {
(void)t;
Real_t a = *(Real_t *)va;
Real_t b = *(Real_t *)vb;
return Real$compare_values(a, b);
}
// Unary negation
public
Real_t Real$negative(Real_t a) {
if (!Real$is_boxed(a)) return R(-REAL_DOUBLE(a));
if (Real$tag(a) == REAL_TAG_RATIONAL) {
rational_t *rat = REAL_RATIONAL(a);
__mpq_struct ret;
mpq_init(&ret);
mpq_neg(&ret, &rat->value);
return mpq_to_real(ret);
}
return Real$times(R(-1.0), a);
}
public
Real_t Real$rounded_to(Real_t x, Real_t round_to) {
// Convert to rationals for exact computation
__mpq_struct rx;
mpq_init(&rx);
// Convert x to rational
if (!Real$is_boxed(x)) {
mpq_set_d(&rx, REAL_DOUBLE(x));
} else if (Real$tag(x) == REAL_TAG_RATIONAL) {
mpq_set(&rx, &REAL_RATIONAL(x)->value);
} else {
mpq_set_d(&rx, Real$as_float64(x, true));
}
__mpq_struct rr;
mpq_init(&rr);
// Convert round_to to rational
if (!Real$is_boxed(round_to)) {
mpq_set_d(&rr, REAL_DOUBLE(round_to));
} else if (Real$tag(round_to) == REAL_TAG_RATIONAL) {
mpq_set(&rr, &REAL_RATIONAL(round_to)->value);
} else {
mpq_set_d(&rr, Real$as_float64(round_to, true));
}
// Compute x / round_to
mpq_t quotient;
mpq_init(quotient);
mpq_div(quotient, &rx, &rr);
// Round to nearest integer using mpz_fdiv_qr for exact rounding
mpz_t rounded, remainder;
mpz_init(rounded);
mpz_init(remainder);
// Get 2 * numerator and denominator for rounding
mpz_t doubled_num;
mpz_init(doubled_num);
mpz_mul_ui(doubled_num, mpq_numref(quotient), 2);
// rounded = (2*num + den) / (2*den) using integer division
mpz_add(doubled_num, doubled_num, mpq_denref(quotient));
mpz_mul_ui(remainder, mpq_denref(quotient), 2);
mpz_fdiv_q(rounded, doubled_num, remainder);
// Multiply back: rounded * round_to
__mpq_struct ret;
mpq_init(&ret);
mpq_set_z(&ret, rounded);
mpq_mul(&ret, &ret, &rr);
mpq_clear(&rx);
mpq_clear(&rr);
mpq_clear(quotient);
mpz_clear(rounded);
mpz_clear(remainder);
mpz_clear(doubled_num);
return mpq_to_real(ret);
}
// Trigonometric functions - return symbolic expressions
public
Real_t Real$sin(Real_t x) {
if (!Real$is_boxed(x)) {
double result = sin(REAL_DOUBLE(x));
// Check if result is exact (e.g., sin(0) = 0)
if (result == 0.0 || result == 1.0 || result == -1.0) {
return R(result);
}
}
return sym_to_real(.op = SYM_SIN, .left = x, .right = R(0));
}
public
Real_t Real$cos(Real_t x) {
if (!Real$is_boxed(x)) {
double result = cos(REAL_DOUBLE(x));
if (result == 0.0 || result == 1.0 || result == -1.0) {
return R(result);
}
}
return sym_to_real(.op = SYM_COS, .left = x, .right = R(0));
}
public
Real_t Real$tan(Real_t x) {
if (!Real$is_boxed(x)) {
double result = tan(REAL_DOUBLE(x));
if (result == 0.0) return R(0.0);
}
return sym_to_real(.op = SYM_TAN, .left = x, .right = R(0));
}
public
Real_t Real$asin(Real_t x) {
if (!Real$is_boxed(x)) {
double result = asin(REAL_DOUBLE(x));
if (result == 0.0) return R(0.0);
}
return sym_to_real(.op = SYM_ASIN, .left = x, .right = R(0));
}
public
Real_t Real$acos(Real_t x) {
if (!Real$is_boxed(x)) {
double result = acos(REAL_DOUBLE(x));
if (result == 0.0) return R(0.0);
}
return sym_to_real(.op = SYM_ACOS, .left = x, .right = R(0));
}
public
Real_t Real$atan(Real_t x) {
if (!Real$is_boxed(x)) {
double result = atan(REAL_DOUBLE(x));
if (result == 0.0) return R(0.0);
}
return sym_to_real(.op = SYM_ATAN, .left = x, .right = R(0));
}
public
Real_t Real$atan2(Real_t y, Real_t x) {
if (!Real$is_boxed(y) && !Real$is_boxed(x)) {
double result = atan2(REAL_DOUBLE(y), REAL_DOUBLE(x));
if (result == 0.0) return R(0.0);
}
return sym_to_real(.op = SYM_ATAN2, .left = y, .right = x);
}
public
Real_t Real$exp(Real_t x) {
if (!Real$is_boxed(x)) {
feclearexcept(FE_INEXACT);
volatile double result = exp(REAL_DOUBLE(x));
if (!fetestexcept(FE_INEXACT) && isfinite(result)) {
return R(result);
}
}
return sym_to_real(.op = SYM_EXP, .left = x, .right = R(0));
}
public
Real_t Real$log(Real_t x) {
if (!Real$is_boxed(x)) {
feclearexcept(FE_INEXACT);
volatile double result = log(REAL_DOUBLE(x));
if (!fetestexcept(FE_INEXACT) && isfinite(result)) {
return R(result);
}
}
return sym_to_real(.op = SYM_LOG, .left = x, .right = R(0));
}
public
Real_t Real$log10(Real_t x) {
if (!Real$is_boxed(x)) {
feclearexcept(FE_INEXACT);
volatile double result = log10(REAL_DOUBLE(x));
if (!fetestexcept(FE_INEXACT) && isfinite(result)) {
return R(result);
}
}
return sym_to_real(.op = SYM_LOG10, .left = x, .right = R(0));
}
public
Real_t Real$abs(Real_t x) {
if (!Real$is_boxed(x)) {
return R(fabs(REAL_DOUBLE(x)));
}
if (Real$tag(x) == REAL_TAG_RATIONAL) {
rational_t *r = REAL_RATIONAL(x);
__mpq_struct ret;
mpq_init(&ret);
mpq_abs(&ret, &r->value);
return mpq_to_real(ret);
}
return sym_to_real(.op = SYM_ABS, .left = x, .right = R(0));
}
public
Real_t Real$floor(Real_t x) {
if (!Real$is_boxed(x)) {
// TODO: this may be inexact in some rare cases
return R(floor(REAL_DOUBLE(x)));
}
if (Real$tag(x) == REAL_TAG_RATIONAL) {
rational_t *r = REAL_RATIONAL(x);
mpz_t quotient;
mpz_init(quotient);
mpz_fdiv_q(quotient, mpq_numref(&r->value), mpq_denref(&r->value));
__mpq_struct ret;
mpq_init(&ret);
mpq_set_z(&ret, quotient);
mpz_clear(quotient);
return mpq_to_real(ret);
}
return sym_to_real(.op = SYM_FLOOR, .left = x, .right = R(0));
}
public
Real_t Real$ceil(Real_t x) {
if (!Real$is_boxed(x)) {
return R(ceil(REAL_DOUBLE(x)));
}
if (Real$tag(x) == REAL_TAG_RATIONAL) {
rational_t *r = REAL_RATIONAL(x);
mpz_t quotient;
mpz_init(quotient);
mpz_cdiv_q(quotient, mpq_numref(&r->value), mpq_denref(&r->value));
__mpq_struct ret;
mpq_init(&ret);
mpq_set_z(&ret, quotient);
mpz_clear(quotient);
return mpq_to_real(ret);
}
return sym_to_real(.op = SYM_CEIL, .left = x, .right = R(0));
}
public
int Real$test() {
GC_INIT();
printf("=== Exact Number System Tests ===\n\n");
// Test 1: Simple double arithmetic (fast path)
printf("Test 1: 2.0 + 3.0\n");
Real_t a = R(2.0);
Real_t b = R(3.0);
Real_t result = Real$plus(a, b);
Text_t s = Real$value_as_text(result);
printf("Result: %s\n", Text$as_c_string(s));
printf("Is boxed: %s\n\n", Real$is_boxed(result) ? "yes" : "no");
// Test 2: Exact rational arithmetic
printf("Test 2: 1/3 + 1/6\n");
Real_t third = Real$from_rational(1, 3);
Real_t sixth = Real$from_rational(1, 6);
result = Real$plus(third, sixth);
s = Real$value_as_text(result);
printf("Result: %s\n", Text$as_c_string(s));
printf("Expected: 1/2\n");
printf("Is boxed: %s\n\n", Real$is_boxed(result) ? "yes" : "no");
// Test 3: (1/3) * 3 should give exactly 1
printf("Test 3: (1/3) * 3\n");
Real_t three = Real$from_rational(3, 1);
result = Real$times(third, three);
s = Real$value_as_text(result);
printf("Result: %s\n", Text$as_c_string(s));
printf("Expected: 1\n");
printf("Is boxed: %s\n\n", Real$is_boxed(result) ? "yes" : "no");
// Test 4: sqrt(4) is exact
printf("Test 4: sqrt(4)\n");
Real_t four = Real$from_rational(4, 1);
result = Real$sqrt(four);
s = Real$value_as_text(result);
printf("Result: %s\n", Text$as_c_string(s));
printf("Expected: 2\n");
printf("Is boxed: %s\n\n", Real$is_boxed(result) ? "yes" : "no");
// Test 5: sqrt(2) * sqrt(2) stays symbolic
printf("Test 5: sqrt(2) * sqrt(2)\n");
Real_t two = Real$from_rational(2, 1);
Real_t sqrt2 = Real$sqrt(two);
result = Real$times(sqrt2, sqrt2);
s = Real$value_as_text(result);
printf("Result (symbolic): %s\n", Text$as_c_string(s));
printf("Approximate value: %.17g\n", Real$as_float64(result, true));
printf("Is boxed: %s\n\n", Real$is_boxed(result) ? "yes" : "no");
// Test 6: Complex symbolic expression
printf("Test 6: (sqrt(2) + 1) * (sqrt(2) - 1)\n");
Real_t one = R(1.0);
Real_t sqrt2_plus_1 = Real$plus(sqrt2, one);
Real_t sqrt2_minus_1 = Real$minus(sqrt2, one);
result = Real$times(sqrt2_plus_1, sqrt2_minus_1);
s = Real$value_as_text(result);
printf("Result (symbolic): %s\n", Text$as_c_string(s));
printf("Approximate value: %.17g\n", Real$as_float64(result, true));
printf("Expected: 1 (but stays symbolic)\n");
printf("Is boxed: %s\n\n", Real$is_boxed(result) ? "yes" : "no");
// Test 7: Division creating rational
printf("Test 7: 5 / 7\n");
Real_t five = Real$from_int64(5);
Real_t seven = Real$from_int64(7);
result = Real$divided_by(five, seven);
s = Real$value_as_text(result);
printf("Result: %s\n", Text$as_c_string(s));
printf("Expected: 5/7\n");
printf("Is boxed: %s\n\n", Real$is_boxed(result) ? "yes" : "no");
// Test 8: Power that stays exact
printf("Test 8: 2^3\n");
result = Real$power(R(2.0), R(3.0));
s = Real$value_as_text(result);
printf("Result: %s\n", Text$as_c_string(s));
printf("Is boxed: %s\n\n", Real$is_boxed(result) ? "yes" : "no");
// Test 9: Decimal arithmetic
printf("Test 8: 2^3\n");
result = Real$power(R(2.0), R(3.0));
s = Real$value_as_text(result);
printf("Result: %s\n", Text$as_c_string(s));
printf("Is boxed: %s\n\n", Real$is_boxed(result) ? "yes" : "no");
// Test 9: Currency calculation that fails with doubles
printf("Test 9: 0.1 + 0.2 (classic floating point error)\n");
Real_t dime = Real$from_rational(1, 10);
Real_t two_dime = Real$from_rational(2, 10);
result = Real$plus(dime, two_dime);
s = Real$value_as_text(result);
printf("Result: %s\n", Text$as_c_string(s));
printf("Double arithmetic: %.17g\n", 0.1 + 0.2);
printf("Expected: 0.3\n");
printf("Is boxed: %s\n\n", Real$is_boxed(result) ? "yes" : "no");
// Test 10: Rounding
printf("Test 10: round(sqrt(2), 0.00001)\n");
result = Real$rounded_to(sqrt2, Real$from_rational(1, 100000));
s = Real$value_as_text(result);
printf("Result: %s\n", Text$as_c_string(s));
printf("Expected: 1.41421\n");
printf("Is boxed: %s\n\n", Real$is_boxed(result) ? "yes" : "no");
printf("=== Tests Complete ===\n");
return 0;
}
public
const TypeInfo_t Real$info = {
.size = sizeof(Real_t),
.align = __alignof__(Real_t),
.metamethods =
{
.compare = Real$compare,
.equal = Real$equal,
.hash = Real$hash,
.as_text = Real$as_text,
.is_none = Real$is_none,
// .serialize = Real$serialize,
// .deserialize = Real$deserialize,
},
};
|