forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfunctions.cc
1768 lines (1562 loc) · 50 KB
/
functions.cc
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
// ****************************************************************************
// functions.cc DB48X project
// ****************************************************************************
//
// File Description:
//
// Standard mathematical functions
//
//
//
//
//
//
//
//
// ****************************************************************************
// (C) 2022 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
#include "functions.h"
#include "arithmetic.h"
#include "array.h"
#include "bignum.h"
#include "compare.h"
#include "conditionals.h"
#include "decimal.h"
#include "expression.h"
#include "fraction.h"
#include "integer.h"
#include "integrate.h"
#include "list.h"
#include "logical.h"
#include "polynomial.h"
#include "solve.h"
#include "tag.h"
#include "unit.h"
#include "variables.h"
RECORDER(function, 16, "Evaluation of functions");
RECORDER(function_error, 16, "Errors during evaluation of functions");
bool function::should_be_symbolic(id type)
// ----------------------------------------------------------------------------
// Check if we should treat the type symbolically
// ----------------------------------------------------------------------------
{
return is_symbolic(type);
}
algebraic_p function::symbolic(id op, algebraic_r x)
// ----------------------------------------------------------------------------
// Check if we should process this function symbolically
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
if (expression_p expr = x->as<expression>())
{
expression_g left, right;
if (expr->split_equation(left, right))
{
algebraic_g l = +left;
algebraic_g r = +right;
l = expression::make(op, l);
r = expression::make(op, r);
return expression::make(ID_TestEQ, l, r);
}
}
expression_p result = expression::make(op, x);
if (result && !unit::factoring && Settings.AutoSimplify())
result = result->simplify();
return result;
}
bool function::has_symbolic_arguments(id type)
// ----------------------------------------------------------------------------
// Check if the command has any symbolic arguments, e.g. Root or Sum
// ----------------------------------------------------------------------------
{
return (type == ID_IFTE ||
type == ID_Sum ||
type == ID_Product ||
type == ID_IFTE ||
type == ID_Subst ||
type == ID_Where ||
type == ID_Copy ||
type == ID_Integrate ||
type == ID_Root ||
type == ID_MultipleEquationsSolver ||
type == ID_Derivative ||
type == ID_Primitive);
}
bool function::is_symbolic_argument(id type, uint arg)
// ----------------------------------------------------------------------------
// Check if the given argument needs to be stored in symbolic form
// ----------------------------------------------------------------------------
{
switch(type)
{
case ID_Sum:
return Sum::can_be_symbolic(arg);
case ID_Product:
return Product::can_be_symbolic(arg);
case ID_IFTE:
return IFTE::can_be_symbolic(arg);
case ID_Subst:
return Subst::can_be_symbolic(arg);
case ID_Where:
return Where::can_be_symbolic(arg);
case ID_Copy:
return Copy::can_be_symbolic(arg);
case ID_Integrate:
return Integrate::can_be_symbolic(arg);
case ID_Root:
return Root::can_be_symbolic(arg);
case ID_Derivative:
return Derivative::can_be_symbolic(arg);
case ID_Primitive:
return Primitive::can_be_symbolic(arg);
default:
break;
}
return false;
}
object::result function::evaluate(id op, ops_t ops)
// ----------------------------------------------------------------------------
// Shared code for evaluation of all common math functions
// ----------------------------------------------------------------------------
{
algebraic_g x = algebraic_p(rt.top());
if (!x)
return ERROR;
x = evaluate(x, op, ops);
if (x && rt.top(x))
return OK;
return ERROR;
}
bool function::exact_trig(id op, algebraic_g &x)
// ----------------------------------------------------------------------------
// Optimize cases where we can do exact trigonometry (avoid rounding)
// ----------------------------------------------------------------------------
// This matters to get exact results for rectangular -> polar
{
// When in radians mode, we cannot avoid rounding except for 0
id amode = Settings.AngleMode();
if (amode == ID_Rad && !x->is_zero(false))
return false;
algebraic_g degrees = x;
switch(amode)
{
case object::ID_Grad:
degrees = degrees * integer::make(90) / integer::make(100);
break;
case object::ID_PiRadians:
degrees = degrees * integer::make(180);
break;
default:
break;
}
ularge angle = 42; // Not a special case...
if (integer_p posint = degrees->as<integer>())
angle = posint->value<ularge>();
else if (const neg_integer *negint = degrees->as<neg_integer>())
angle = 360 - negint->value<ularge>() % 360;
else if (bignum_p posint = degrees->as<bignum>())
angle = posint->value<ularge>();
else if (const neg_bignum *negint = degrees->as<neg_bignum>())
angle = 360 - negint->value<ularge>() % 360;
angle %= 360;
switch(op)
{
case ID_cos:
angle = (angle + 90) % 360;
// fallthrough
case ID_sin:
switch(angle)
{
case 0:
case 180: x = integer::make(0); return true;
case 270: x = integer::make(-1); return true;
case 90: x = integer::make(1); return true;
case 30:
case 150: x = +fraction::make(integer::make(1),
integer::make(2));
return true;
case 210:
case 330: x = +fraction::make(integer::make(-1),
integer::make(2));
return true;
}
return false;
case ID_tan:
switch(angle)
{
case 0:
case 180: x = integer::make(0); return true;
case 45:
case 225: x = integer::make(1); return true;
case 135:
case 315: x = integer::make(-1); return true;
}
default:
break;
}
return false;
}
algebraic_p function::evaluate(algebraic_r xr, id op, ops_t ops)
// ----------------------------------------------------------------------------
// Shared code for evaluation of all common math functions
// ----------------------------------------------------------------------------
{
cleaner purge;
algebraic_p result = evaluate_noclean(xr, op, ops);
if (result != +xr)
result = purge(result);
return result;
}
algebraic_p function::evaluate_noclean(algebraic_r xr, id op, ops_t ops)
// ----------------------------------------------------------------------------
// Shared code for evaluation of all common math functions
// ----------------------------------------------------------------------------
{
if (!xr)
return nullptr;
algebraic_g x = xr;
// Check if we are computing exact trigonometric values
if (op >= ID_sin && op <= ID_tan)
{
if (id amode = adjust_angle(x))
{
settings::SaveAngleMode saved(amode);
return evaluate(x, op, ops);
}
if (exact_trig(op, x))
return x;
}
// Check if we need to add units
if (op >= ID_asin && op <= ID_atan)
{
if (Settings.SetAngleUnits() && x->is_real())
{
settings::SaveSetAngleUnits save(false);
x = evaluate(x, op, ops);
add_angle(x);
return x;
}
}
// Check if we need to deal with units specially
if (unit_p u = unit::get(x))
{
algebraic_g value = u->value();
algebraic_g uexpr = u->uexpr();
settings::SaveNumericalResults snr(false);
save<bool> ueval(unit::mode, true);
if (op == ID_sqrt || op == ID_cbrt)
{
save<bool> ufactor(unit::factoring, true);
uint divisor = 2 + (op == ID_cbrt);
algebraic_g exponent = +fraction::make(integer::make(1),
integer::make(divisor));
value = evaluate(value, op, ops);
uexpr = pow(uexpr, exponent);
if (value && uexpr)
return unit::make(value, uexpr);
if (!rt.error())
rt.inconsistent_units_error();
return nullptr;
}
unit::factoring = false;
uexpr = uexpr->evaluate();
if (uexpr && uexpr->is_real())
{
value = value * uexpr;
value = evaluate(value, op, ops);
return value;
}
rt.inconsistent_units_error();
return nullptr;
}
// Convert arguments to numeric if necessary
if (Settings.NumericalResults())
{
(void) to_decimal(x, true); // May fail silently, and that's OK
if (!x)
return nullptr;
}
id xt = x->type();
if (should_be_symbolic(xt))
return symbolic(op, x);
if (is_complex(xt))
return algebraic_p(ops.zop(complex_g(complex_p(+x))));
// Check if need to promote integer values to decimal
if (is_integer(xt))
{
// Do not accept sin(#123h)
if (!is_real(xt))
{
rt.type_error();
return nullptr;
}
}
// Call the right hardware-accelerated or decimal function
algebraic_p result = nullptr;
if (hwfp_promotion(x))
{
if (hwfloat_p fp = x->as<hwfloat>())
result = ops.fop(fp);
else if (hwdouble_p dp = x->as<hwdouble>())
result = ops.dop(dp);
}
else if (decimal_promotion(x))
{
decimal_g xv = decimal_p(+x);
xv = ops.decop(xv);
if (xv && !xv->is_normal())
{
if (xv->is_infinity())
return rt.numerical_overflow(xv->is_negative());
rt.domain_error();
return nullptr;
}
result = +xv;
}
else
{
// All other cases: report an error
rt.type_error();
return nullptr;
}
// If we got a null result, try promoting to complex
if (!result && Settings.ComplexResults())
{
rt.clear_error();
complex_g z = rectangular::make(x, integer::make(0));
result = ops.zop(z);
}
return result;
}
object::result function::evaluate(algebraic_fn op, bool mat)
// ----------------------------------------------------------------------------
// Perform the operation from the stack, using a C++ operation
// ----------------------------------------------------------------------------
{
if (object_p top = strip(rt.top()))
{
id topty = top->type();
if (topty == ID_polynomial)
{
if (op == algebraic_fn(sq::evaluate) ||
op == algebraic_fn(cubed::evaluate))
{
polynomial_g xp = polynomial_p(top);
ularge exp = op == algebraic_fn(cubed::evaluate) ? 3 : 2;
top = polynomial::pow(xp, exp);
return (top && rt.top(top)) ? OK : ERROR;
}
else if (op == algebraic_fn(neg::evaluate))
{
polynomial_g xp = polynomial_p(top);
top = polynomial::neg(xp);
return (top && rt.top(top)) ? OK : ERROR;
}
else
{
top = polynomial_p(top)->as_expression();
}
topty = top ? top->type() : ID_expression;
}
if (topty == ID_list || (topty == ID_array && !mat))
{
top = list_p(top)->map(op);
}
else if (is_algebraic(topty) ||
(topty == ID_array && mat) ||
(is_integer(topty) && op == algebraic_fn(neg::evaluate)))
{
algebraic_g x = algebraic_p(top);
x = op(x);
top = +x;
}
else
{
rt.type_error();
return ERROR;
}
if (top && rt.top(top))
return OK;
}
return ERROR;
}
object::result function::evaluate(id op, nfunction_fn fn, uint arity,
bool (*can_be_symbolic)(uint arg))
// ----------------------------------------------------------------------------
// Perform the operation from the stack for n-ary functions
// ----------------------------------------------------------------------------
{
if (!rt.args(arity))
return ERROR;
bool is_symbolic = false;
algebraic_g args[arity];
for (uint a = 0; a < arity; a++)
{
object_g oarg = rt.stack(a);
while (tag_p tagged = oarg->as<tag>())
oarg = tagged->tagged_object();
algebraic_p arg = oarg->as_extended_algebraic();
if (!arg)
{
rt.type_error();
return ERROR;
}
args[a] = arg;
if (arg->is_symbolic())
{
if (!can_be_symbolic(a))
{
if (Settings.NumericalResults())
{
// Conversion to numerical if needed (may fail silently)
(void) to_decimal(args[a], true);
if (!args[a])
return ERROR;
}
if (args[a]->is_symbolic())
is_symbolic = true;
}
}
}
algebraic_g result;
// Check the symbolic case
if (is_symbolic)
result = expression::make(op, args, arity, ID_expression, true);
else
result = fn(op, args, arity);
if (result && rt.drop(arity) && rt.push(+result))
return OK;
return ERROR;
}
FUNCTION_BODY(neg)
// ----------------------------------------------------------------------------
// Implementation of 'neg'
// ----------------------------------------------------------------------------
// Special case where we don't need to promote argument to decimal
{
if (!x)
return nullptr;
id xt = x->type();
switch(xt)
{
case ID_expression:
case ID_local:
case ID_symbol:
case ID_constant:
return symbolic(ID_neg, x);
case ID_polynomial:
{
polynomial_g p = polynomial_p(+x);
return polynomial::neg(p);
}
case ID_integer:
case ID_bignum:
case ID_fraction:
case ID_big_fraction:
case ID_decimal:
{
// We can keep the object, just changing the type
id negty = id(xt + 1);
algebraic_p clone = algebraic_p(rt.clone(x));
byte *tp = (byte *) clone;
*tp = negty;
return clone;
}
case ID_neg_integer:
case ID_neg_bignum:
case ID_neg_fraction:
case ID_neg_big_fraction:
case ID_neg_decimal:
{
// We can keep the object, just changing the type
id negty = id(xt - 1);
algebraic_p clone = algebraic_p(rt.clone(x));
byte *tp = (byte *) clone;
*tp = negty;
return clone;
}
#if CONFIG_FIXED_BASED_OBJECTS
case ID_hex_integer:
case ID_dec_integer:
case ID_oct_integer:
case ID_bin_integer:
case ID_hex_bignum:
case ID_dec_bignum:
case ID_oct_bignum:
case ID_bin_bignum:
#endif // CONFIG_FIXED_BASED_OBJECTS
case ID_based_integer:
case ID_based_bignum:
{
algebraic_g z = integer::make(0);
z = z - x;
return z;
}
case ID_rectangular:
return rectangular::make(-rectangular_p(+x)->re(),
-rectangular_p(+x)->im());
case ID_polar:
return polar::make(-polar_p(+x)->mod(),
polar_p(+x)->arg(object::ID_PiRadians),
object::ID_PiRadians);
case ID_unit:
return unit::simple(neg::run(unit_p(+x)->value()),
unit_p(+x)->uexpr());
case ID_tag:
{
algebraic_g tagged = tag_p(+x)->tagged_object()->as_algebraic();
return evaluate(tagged);
}
case ID_array:
case ID_list:
return list_p(+x)->map(neg::evaluate);
case ID_hwfloat:
return hwfloat::neg((hwfloat::hwfp_r) x);
case ID_hwdouble:
return hwdouble::neg((hwdouble::hwfp_r) x);
default:
break;
}
rt.type_error();
return nullptr;
}
FUNCTION_BODY(abs)
// ----------------------------------------------------------------------------
// Implementation of 'abs'
// ----------------------------------------------------------------------------
// Special case where we don't need to promote argument to decimal
{
if (!x)
return nullptr;
id xt = x->type();
switch(xt)
{
case ID_expression:
case ID_local:
case ID_symbol:
case ID_constant:
return symbolic(ID_abs, x);
case ID_integer:
case ID_bignum:
case ID_fraction:
case ID_big_fraction:
case ID_decimal:
return x;
case ID_neg_integer:
case ID_neg_bignum:
case ID_neg_fraction:
case ID_neg_big_fraction:
case ID_neg_decimal:
{
// We can keep the object, just changing the type
id absty = id(xt - 1);
algebraic_p clone = algebraic_p(rt.clone(x));
byte *tp = (byte *) clone;
*tp = absty;
return clone;
}
case ID_rectangular:
case ID_polar:
return complex_p(+x)->mod();
case ID_unit:
return unit::simple(abs::run(unit_p(+x)->value()),
unit_p(+x)->uexpr());
case ID_tag:
{
algebraic_g tagged = tag_p(+x)->tagged_object()->as_algebraic_or_list();
return evaluate(tagged);
}
case ID_array:
return array_p(+x)->norm();
case ID_list:
return list_p(+x)->map(abs::evaluate);
case ID_hwfloat:
return hwfloat::abs((hwfloat::hwfp_r) x);
case ID_hwdouble:
return hwdouble::abs((hwdouble::hwfp_r) x);
default:
break;
}
rt.type_error();
return nullptr;
}
FUNCTION_BODY(arg)
// ----------------------------------------------------------------------------
// Implementation of the complex argument (0 for non-complex values)
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
id xt = x->type();
if (should_be_symbolic(xt))
return symbolic(ID_arg, x);
auto angle_mode = Settings.AngleMode();
algebraic_g a;
if (is_complex(xt))
{
a = complex_p(algebraic_p(x))->arg(angle_mode);
}
else
{
bool negative = x->is_negative(false);
a = integer::make(0);
a = complex::convert_angle(a, angle_mode, angle_mode, negative);
}
if (a && Settings.SetAngleUnits() && a->is_real())
add_angle(a);
return a;
}
FUNCTION_BODY(re)
// ----------------------------------------------------------------------------
// Extract the real part of a number
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
id xt = x->type();
if (should_be_symbolic(xt))
return symbolic(ID_re, x);
if (is_complex(xt))
return complex_p(algebraic_p(x))->re();
if (!is_real(xt))
rt.type_error();
return x;
}
FUNCTION_BODY(im)
// ----------------------------------------------------------------------------
// Extract the imaginary part of a number (0 for real values)
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
id xt = x->type();
if (should_be_symbolic(xt))
return symbolic(ID_im, x);
if (is_complex(xt))
return complex_p(algebraic_p(x))->im();
if (!is_real(xt))
rt.type_error();
return integer::make(0);
}
FUNCTION_BODY(conj)
// ----------------------------------------------------------------------------
// Compute the conjugate of input
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
id xt = x->type();
if (should_be_symbolic(xt))
return symbolic(ID_conj, x);
if (is_complex(xt))
return complex_p(algebraic_p(x))->conjugate();
if (!is_real(xt))
rt.type_error();
return x;
}
FUNCTION_BODY(sign)
// ----------------------------------------------------------------------------
// Implementation of 'sign'
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
id xt = x->type();
if (should_be_symbolic(xt))
return symbolic(ID_sign, x);
if (x->is_negative(false))
{
return integer::make(-1);
}
else if (x->is_zero(false))
{
return integer::make(0);
}
else if (is_integer(xt) || is_bignum(xt) || is_fraction(xt) || is_real(xt))
{
return integer::make(1);
}
else if (is_complex(xt))
{
return polar::make(integer::make(1),
complex_p(algebraic_p(x))->pifrac(),
object::ID_PiRadians);
}
rt.type_error();
return nullptr;
}
FUNCTION_BODY(IntPart)
// ----------------------------------------------------------------------------
// Implementation of 'IP'
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
id xt = x->type();
if (should_be_symbolic(xt))
return symbolic(ID_IntPart, x);
if (is_decimal(xt))
return decimal::IntPart(decimal_p(+x));
if (is_real(xt))
{
// This code works for integer, fraction and decimal types
algebraic_g one = integer::make(1);
algebraic_g r = rem::evaluate(x, one);
return x - r;
}
rt.type_error();
return nullptr;
}
FUNCTION_BODY(FracPart)
// ----------------------------------------------------------------------------
// Implementation of 'FP'
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
id xt = x->type();
if (should_be_symbolic(xt))
return symbolic(ID_FracPart, x);
if (is_decimal(xt))
return decimal::FracPart(decimal_p(+x));
if (is_real(xt))
{
algebraic_g one = integer::make(1);
return rem::evaluate(x, one);
}
rt.type_error();
return nullptr;
}
FUNCTION_BODY(ceil)
// ----------------------------------------------------------------------------
// The `ceil` command returns the integer, or the integer immediately above
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
id xt = x->type();
if (should_be_symbolic(xt))
return symbolic(ID_ceil, x);
if (is_decimal(xt))
return decimal::ceil(decimal_p(+x));
if (is_real(xt))
{
algebraic_g one = integer::make(1);
algebraic_g r = mod::evaluate(one - x, one);
return x + r;
}
rt.type_error();
return nullptr;
}
FUNCTION_BODY(floor)
// ----------------------------------------------------------------------------
// The `floor` command returns the integer imediately below
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
id xt = x->type();
if (should_be_symbolic(xt))
return symbolic(ID_floor, x);
if (is_decimal(xt))
return decimal::floor(decimal_p(+x));
if (is_real(xt))
{
algebraic_g one = integer::make(1);
algebraic_g r = mod::evaluate(x, one);
return x - r;
}
rt.type_error();
return nullptr;
}
FUNCTION_BODY(inv)
// ----------------------------------------------------------------------------
// Invert is implemented as 1/x
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
if (x->is_symbolic())
return symbolic(ID_inv, x);
else if (x->type() == ID_array)
return array_p(+x)->invert();
if (x->is_decimal())
return decimal::inv(decimal_p(+x));
algebraic_g one = rt.make<integer>(ID_integer, 1);
return one / x;
}
INSERT_BODY(inv)
// ----------------------------------------------------------------------------
// x⁻¹ is a postfix
// ----------------------------------------------------------------------------
{
return ui.insert(o->fancy(), ui.POSTFIX);
}
FUNCTION_BODY(sq)
// ----------------------------------------------------------------------------
// Square is implemented using a multiplication
// ----------------------------------------------------------------------------
{
if (!+x)
return nullptr;
if (x->is_symbolic())
return symbolic(ID_sq, x);
return x * x;
}
INSERT_BODY(sq)
// ----------------------------------------------------------------------------
// x² is a postfix
// ----------------------------------------------------------------------------
{
return ui.insert(o->fancy(), ui.POSTFIX);
}
FUNCTION_BODY(cubed)
// ----------------------------------------------------------------------------
// Cubed is implemented as two multiplications
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
if (x->is_symbolic())
return symbolic(ID_cubed, x);
return x * x * x;
}
FUNCTION_BODY(mant)
// ----------------------------------------------------------------------------
// Return mantissa of object
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
if (x->is_symbolic())
return symbolic(ID_mant, x);
algebraic_g a = x;
if (unit_p u = unit::get(a))
a = u->value();
if (!decimal_promotion(a))
{
rt.type_error();
return nullptr;
}
decimal_p d = decimal_p(+a);
decimal::info i = d->shape();
// The mantissa is always positive on HP calculators
gcbytes bytes = i.base;
return rt.make<decimal>(1, i.nkigits, bytes);
}
FUNCTION_BODY(xpon)
// ----------------------------------------------------------------------------
// Return exponent of object
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
if (x->is_symbolic())
return symbolic(ID_xpon, x);
algebraic_g a = x;
if (unit_p u = unit::get(a))
a = u->value();
if (!decimal_promotion(a))
{
rt.type_error();
return nullptr;
}
decimal_p d = decimal_p(+a);
return integer::make(d->exponent() - 1LL);
}
FUNCTION_BODY(SigDig)
// ----------------------------------------------------------------------------
// Return number of significant digits in object