forked from verhas/ScriptBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathops.c
More file actions
914 lines (734 loc) · 21.9 KB
/
mathops.c
File metadata and controls
914 lines (734 loc) · 21.9 KB
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
/*mathop.c
--GNU LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/* comparision operators compare strings as well */
#include <string.h>
#include <limits.h>
#include "../command.h"
/* stringcompare two string values. The values SHOULD be string.
*/
static int STRCMP(pExecuteObject pEo,VARIABLE Op1, VARIABLE Op2, int iCase){
unsigned long n;
char *a,*b;
char ca,cb;
if( memory_IsUndef(Op1) && memory_IsUndef(Op2) )return 0;
if( memory_IsUndef(Op1) )return 1;
if( memory_IsUndef(Op2) )return -1;
iCase &= 1;/* only the lowest bit is about case sensitivity */
n = STRLEN(Op1);
if( n > STRLEN(Op2) ) n= STRLEN(Op2);
a = STRINGVALUE(Op1);
b = STRINGVALUE(Op2);
while( n-- ){
ca = *a;
cb = *b;
if( iCase ){
if( isupper(ca) )ca = tolower(ca);
if( isupper(cb) )cb = tolower(cb);
}
if( ca != cb )return ( (ca)-(cb) );
a++;
b++;
}
if( STRLEN(Op1) == STRLEN(Op2) )return 0;
if( STRLEN(Op1) > STRLEN(Op2) )return 1;
return -1;
}
static long longpow(long a,long b){
long result;
result = 1;
while( b ){
if( b&1 )result *= a;
b /= 2;
a *= a;
}
return result;
}
static double doublepow(double a,long b){
double result;
result = 1.0;
while( b ){
if( b&1 )result *= a;
b /= 2;
a *= a;
}
return result;
}
#define RAISEMATHERROR "raisematherror"
long *RaiseError(pExecuteObject pEo){
long *plCache;
plCache = (long *) PARAMPTR(CMD_DIV);
if( plCache == NULL ){
plCache = options_GetR(pEo,RAISEMATHERROR);
if( plCache == NULL )
options_Set(pEo,RAISEMATHERROR,0);
plCache = options_GetR(pEo,RAISEMATHERROR);
}
return plCache;
}
/*POD
=H Mathematical operators
This file defines all the mathematical operators that are implemented in ScriptBasic.
CUT*/
/*POD
=section MULT
=H Multiplication
This operator multiplies two numbers. If one of the arguments is double then the result is double, otherwise the result is long.
If one of the operators is undefined the result is undefined.
CUT*/
COMMAND(MULT)
#if NOTIMP_MULT
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2;
double dResult;
long lResult,lop1,lop2;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = EVALUATEEXPRESSION(CAR(nItem));
NONULOP(Op1)
nItem = CDR(nItem);
Op2 = EVALUATEEXPRESSION(CAR(nItem));
NONULOP(Op2)
/* if any of the arguments is double then the result is double */
if( ! ISINTEGER(Op1) || ! ISINTEGER(Op2) ){
RETURN_DOUBLE_VALUE_OR_LONG( GETDOUBLEVALUE(Op1) * GETDOUBLEVALUE(Op2) )
}
lop1 = GETLONGVALUE(Op1);
lop2 = GETLONGVALUE(Op2);
lResult = lop1 * lop2;
if( 0 == lop1 ){
RETURN_LONG_VALUE( lResult );
}
if( lResult / lop1 == lop2 ){
RETURN_LONG_VALUE( lResult );
}
RETURN_DOUBLE_VALUE_OR_LONG( GETDOUBLEVALUE(Op1) * GETDOUBLEVALUE(Op2) )
#endif
END
COMMAND(EQ)
#if NOTIMP_EQ
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = EVALUATEEXPRESSION(CAR(nItem));
NONULOPE(Op1)
nItem = CDR(nItem);
Op2 = EVALUATEEXPRESSION(CAR(nItem));
NONULOPE(Op2)
/* undef is equal to undef */
if( memory_IsUndef(Op1) && memory_IsUndef(Op2) ){
RETURN_LONG_VALUE(-1L);
}
/* undef is not equal to anything else */
if( memory_IsUndef(Op1) || memory_IsUndef(Op2) ){
RETURN_LONG_VALUE(0)
}
/* if any of the arguments is string then we compare strings */
if( TYPE(Op1) == VTYPE_STRING || TYPE(Op2) == VTYPE_STRING ){
Op1 = CONVERT2STRING(Op1);
Op2 = CONVERT2STRING(Op2);
RETURN_LONG_VALUE( STRCMP(pEo,Op1,Op2,OPTION("compare")) == 0 ? -1L : 0 )
}
/* if any of the arguments is double then we compare double */
if( TYPE(Op1) == VTYPE_DOUBLE || TYPE(Op2) == VTYPE_DOUBLE ){
RETURN_LONG_VALUE( GETDOUBLEVALUE(Op1) == GETDOUBLEVALUE(Op2) ? -1L : 0L )
}
RETURN_LONG_VALUE( GETLONGVALUE(Op1) == GETLONGVALUE(Op2) ? -1L : 0L )
#endif
END
COMMAND(NE)
#if NOTIMP_NE
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = EVALUATEEXPRESSION(CAR(nItem));
NONULOPE(Op1)
nItem = CDR(nItem);
Op2 = EVALUATEEXPRESSION(CAR(nItem));
NONULOPE(Op2)
/* undef is equal to undef */
if( memory_IsUndef(Op1) && memory_IsUndef(Op2) ){
RETURN_LONG_VALUE( 0 )
}
/* undef is not equal to anything else */
if( memory_IsUndef(Op1) || memory_IsUndef(Op2) ){
RETURN_LONG_VALUE( -1L )
}
/* if any of the arguments is string then we compare strings */
if( TYPE(Op1) == VTYPE_STRING || TYPE(Op2) == VTYPE_STRING ){
Op1 = CONVERT2STRING(Op1);
Op2 = CONVERT2STRING(Op2);
RETURN_LONG_VALUE( STRCMP(pEo,Op1,Op2,OPTION("compare")) != 0 ? -1L : 0 )
}
/* if any of the arguments is double then we compare double */
if( TYPE(Op1) == VTYPE_DOUBLE || TYPE(Op2) == VTYPE_DOUBLE ){
RETURN_LONG_VALUE( GETDOUBLEVALUE(Op1) != GETDOUBLEVALUE(Op2) ? -1L : 0L )
}
RETURN_LONG_VALUE( GETLONGVALUE(Op1) != GETLONGVALUE(Op2) ? -1L : 0L )
#endif
END
#define LOGOP(NAME,OP) \
COMMAND(NAME)\
NODE nItem;\
VARIABLE Op1,Op2;\
\
/* this is an operator and not a command, therefore we do not have our own mortal list */\
USE_CALLER_MORTALS;\
\
/* evaluate the parameters */\
nItem = PARAMETERLIST;\
Op1 = EVALUATEEXPRESSION(CAR(nItem));\
NONULOPE(Op1)\
nItem = CDR(nItem);\
Op2 = EVALUATEEXPRESSION(CAR(nItem));\
NONULOPE(Op2)\
\
/* undef is not comparable except for equality */\
if( memory_IsUndef(Op1) || memory_IsUndef(Op2) ){\
RETURN_LONG_VALUE( 0 )\
}\
\
/* if any of the arguments is string then we compare strings */\
if( TYPE(Op1) == VTYPE_STRING || TYPE(Op2) == VTYPE_STRING ){\
Op1 = CONVERT2STRING(Op1);\
Op2 = CONVERT2STRING(Op2);\
RETURN_LONG_VALUE( STRCMP(pEo,Op1,Op2,OPTION("compare")) OP 0 ? -1L : 0 )\
RETURN;\
}\
\
/* if any of the arguments is double then we compare double */\
if( TYPE(Op1) == VTYPE_DOUBLE || TYPE(Op2) == VTYPE_DOUBLE ){\
RETURN_LONG_VALUE( GETDOUBLEVALUE(Op1) OP GETDOUBLEVALUE(Op2) ? -1L : 0L )\
}\
RETURN_LONG_VALUE( GETLONGVALUE(Op1) OP GETLONGVALUE(Op2) ? -1L : 0L )\
END
/*POD
=section compare
=H Comparing operators
The comparing operators compare long, double and string values. Whenever any of the
arguments is string the comparisionis done stringwise. If none of the arguments are strings
but one of then is double then the comparision is done between doubles. Otherwise we compare
long values.
The comparing operators are
=itemize
=item = equality operator
=item <> non equality operator
=item < less than
=item > greather than
=item <= less than or equal
=item >= greather than or equal
=noitemize
When comparing T<undef> values the following statements should be taken into account:
=itemize
=item T<undef> is equal to T<undef>
=item T<undef> is not equal anything else than T<undef>
=item T<undef> is comparable with anything only for equality or non equality. Any other comparision
having an operand T<undef> results an undefined value.
=noitemize
CUT*/
#define NOCOMMAND(XXX) \
COMMAND(XXX)\
NOTIMPLEMENTED;\
END
#if NOTIMP_LT
NOCOMMAND(LT)
#else
LOGOP(LT,<)
#endif
#if NOTIMP_LE
NOCOMMAND(LE)
#else
LOGOP(LE,<=)
#endif
#if NOTIMP_GT
NOCOMMAND(GT)
#else
LOGOP(GT,>)
#endif
#if NOTIMP_GE
NOCOMMAND(GE)
#else
LOGOP(GE,>=)
#endif
#define LONGOP(NAME,OP) \
COMMAND(NAME)\
NODE nItem;\
VARIABLE Op1,Op2;\
USE_CALLER_MORTALS;\
nItem = PARAMETERLIST;\
Op1 = EVALUATEEXPRESSION(CAR(nItem));\
NONULOP(Op1)\
nItem = CDR(nItem);\
Op2 = EVALUATEEXPRESSION(CAR(nItem));\
NONULOP(Op2)\
RETURN_LONG_VALUE( GETLONGVALUE(Op1) OP GETLONGVALUE(Op2) )\
END
/*POD
=section longoperators
=H Long operators
These operators are defined only for long arguments, and they result long value.
If any of their argument is T<undef> the result is T<undef>. The operators are
=itemize
=item T<and> bitwise and
=item T<or> bitwise or
=item T<xor> bitwise xor
=noitemize
Note that the logical operators can be used to evaluate logical expressions as
well as bitwise expressions, because logical TRUE value is -1L which means all
bits set to 1. In commands that take a logical value any nonzero value is true.
CUT*/
#if NOTIMP_AND
NOCOMMAND(AND)
#else
LONGOP(AND,&)
#endif
#if NOTIMP_OR
NOCOMMAND(OR)
#else
LONGOP(OR,|)
#endif
#if NOTIMP_XOR
NOCOMMAND(XOR)
#else
LONGOP(XOR,^)
#endif
/*POD
=section mod
=H Modulo operators
This operator calculates the modulo of two numbers.
CUT*/
COMMAND(MOD)
#if NOTIMP_MOD
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2;
long lop1,lop2;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = EVALUATEEXPRESSION(CAR(nItem));
NONULOP(Op1)
nItem = CDR(nItem);
Op2 = EVALUATEEXPRESSION(CAR(nItem));
NONULOP(Op2)
lop1 = GETLONGVALUE(Op1);
lop2 = GETLONGVALUE(Op2);
if( lop2 == 0 ){
ERRORUNDEF
}
RETURN_LONG_VALUE( lop1 % lop2 )
#endif
END
/*POD
=section plusminus
=H unary and binary plus and minus
These functions implement the unary and binary plus and minus operands.
If any of the arguments is T<undef> then the result is T<undef>.
If any of the arguments is double or a string evaluating to a float value
then the result is double.
The result is long or double, never string.
CUT*/
COMMAND(PLUS)
#if NOTIMP_PLUS
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2;
double dResult;
long lResult,lop1,lop2;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = EVALUATEEXPRESSION(CAR(nItem));
NONULOP(Op1)
nItem = CDR(nItem);
/* if there is second operand then this is binary operation*/
if( nItem ){
Op2 = EVALUATEEXPRESSION(CAR(nItem));
NONULOP(Op2)
/* if any of the arguments is double then the result is double */
if( ! ISINTEGER(Op1) || !ISINTEGER(Op2) ){
RETURN_DOUBLE_VALUE_OR_LONG( GETDOUBLEVALUE(Op1) + GETDOUBLEVALUE(Op2) )
}
lop1 = GETLONGVALUE(Op1);
lop2 = GETLONGVALUE(Op2);
lResult = lop1 + lop2;
if( lop1 == 0 || lop2 == 0 ){
RETURN_LONG_VALUE( lResult );
}
/* if operands have different sign then there can not be overflow */
if( ( lop1 < 0 && lop2 > 0 ) || ( lop1 > 0 && lop2 < 0 ) ){
RETURN_LONG_VALUE( lResult );
}
/* if operands are positive */
if( lop1 > 0 ){
if( LONG_MAX - lop1 >= lop2 ){
RETURN_LONG_VALUE( lResult );
}
RETURN_DOUBLE_VALUE_OR_LONG( GETDOUBLEVALUE(Op1) + GETDOUBLEVALUE(Op2) )
}
/* if operands are negative */
if( lop1 < 0 ){
if( LONG_MIN - lop1 <= lop2 ){
RETURN_LONG_VALUE( lResult );
}
RETURN_DOUBLE_VALUE_OR_LONG( GETDOUBLEVALUE(Op1) + GETDOUBLEVALUE(Op2) )
}
/* we should not ever get here */
RETURN_DOUBLE_VALUE_OR_LONG( GETDOUBLEVALUE(Op1) + GETDOUBLEVALUE(Op2) )
}
/* we get here if this is unary */
if( ISINTEGER(Op1) ){
RETURN_LONG_VALUE( GETLONGVALUE(Op1) )
}
RETURN_DOUBLE_VALUE_OR_LONG( GETLONGVALUE(Op1) )
#endif
END
COMMAND(MINUS)
#if NOTIMP_MINUS
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2;
double dResult;
long lResult,lop1,lop2;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = EVALUATEEXPRESSION(CAR(nItem));
NONULOP(Op1)
nItem = CDR(nItem);
if( nItem ){
Op2 = EVALUATEEXPRESSION(CAR(nItem));
NONULOP(Op2)
/* if any of the arguments is double then the result is double */
if( ! ISINTEGER(Op1) || ! ISINTEGER(Op2) ){
RETURN_DOUBLE_VALUE_OR_LONG( GETDOUBLEVALUE(Op1) - GETDOUBLEVALUE(Op2) )
}
lop1 = GETLONGVALUE(Op1);
lop2 = GETLONGVALUE(Op2);
lResult = lop1 - lop2;
if( lop1 == 0 || lop2 == 0 ){
RETURN_LONG_VALUE( lResult );
}
/* if operands have the same sign then there can not be overflow */
if( ( lop1 < 0 && lop2 < 0 ) || ( lop1 > 0 && lop2 > 0 ) ){
RETURN_LONG_VALUE( lResult );
}
/* if lop1 is positive and we substract a negative number from it */
if( lop1 > 0 ){
if( LONG_MAX - lop1 >= -lop2 ){
RETURN_LONG_VALUE( lResult );
}
RETURN_DOUBLE_VALUE_OR_LONG( GETDOUBLEVALUE(Op1) - GETDOUBLEVALUE(Op2) )
}
/* if lop1 is negative and we substract from it */
if( lop1 < 0 ){
if( LONG_MIN - lop1 <= -lop2 ){
RETURN_LONG_VALUE( lResult );
}
RETURN_DOUBLE_VALUE_OR_LONG( GETDOUBLEVALUE(Op1) - GETDOUBLEVALUE(Op2) )
}
/* we should never get here */
RETURN_DOUBLE_VALUE_OR_LONG( GETDOUBLEVALUE(Op1) - GETDOUBLEVALUE(Op2) )
}
/* this is unary */
if( ! ISINTEGER(Op1) ){
RETURN_DOUBLE_VALUE_OR_LONG( - GETDOUBLEVALUE(Op1) )
}
RETURN_LONG_VALUE( - GETLONGVALUE(Op1) )
#endif
END
/*POD
=section NOT
=H unary NOT operator
This operator takes one argument converts it to long and inverts all bits.
If the argument is T<undef> the result is -1L which is the absolute TRUE value,
havinbg all bits set.
CUT*/
COMMAND(NOT)
#if NOTIMP_NOT
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = EVALUATEEXPRESSION(CAR(nItem));
NONULOP(Op1)
RETURN_LONG_VALUE( ~ GETLONGVALUE(Op1) )
#endif
END
/*POD
=section POWER
=H powering operator
This is a binary operator that calculates I<x> powered to I<y>.
If any of the arguments is T<undef> then the result is also T<undef>.
If the exponent is negative the result is double.
If any of the operators is double or is a string evaluating to a non-integer
value then the result is double.
Otherwise the operator makes integer operations calculating the power value.
CUT*/
COMMAND(POWER)
#if NOTIMP_POWER
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE vMantissa,vExponent;
double dMantissa,dExponent,dRoot,dResult;
long lMantissa,lExponent,lRoot;
int bMantIsInt,bExpIsInt;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
vMantissa = EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(vMantissa) ){
RESULT = NULL;
RETURN;
}
nItem = CDR(nItem);
vExponent = EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(vExponent) ){
RESULT = NULL;
RETURN;
}
bMantIsInt = ISINTEGER(vMantissa);
bExpIsInt = ISINTEGER(vExponent);
if( bExpIsInt ){/* if the exponent is integer */
lExponent = GETLONGVALUE(vExponent);
if( bMantIsInt ){
/* both exponent and mantissa are integer */
lMantissa = GETLONGVALUE(vMantissa);
/* 0 ^ 0 is undefined, because this functional has a singularity there */
if( lMantissa == 0 && lExponent == 0 ){
RESULT = NULL;
RETURN;
}
if( lExponent < 0 ){
if( lMantissa == 0 ){/* the result is zero */
RETURN_LONG_VALUE(0);
}
if( lMantissa == 1 ){/* the result is 1 */
RETURN_LONG_VALUE(1);
}
/* The result is double because it is between one and zero excluding the boundaries. */
/* the value in the denominator is zero only if lMantissa is zero, but that was already handled above */
RETURN_DOUBLE_VALUE( 1.0 / (double)longpow(lMantissa,-lExponent) );
}else{
/* if the exponent is positive (or zero) and both mantissa and exponent are integers */
RETURN_LONG_VALUE(longpow(lMantissa,lExponent));
}
}else{
/* Exponent is integer, but the mantissa is not. */
dMantissa = GETDOUBLEVALUE(vMantissa);
if( lExponent < 0 ){
/* The demoninator can not be zero, because doublepow is zero only if the mantissa is zero.
However zero is an integer value, and the mantissa is double. What about rounding errors? */
dResult = 1.0 / doublepow(dMantissa,-lExponent);
/* if the result is integer amd what is more: it can be stored in an integer... */
if( dResult == floor(dResult) && fabs(dResult) <= LONG_MAX ){
RETURN_LONG_VALUE((long)dResult);
}else{
RETURN_DOUBLE_VALUE(dResult);
}
}else{
/* if the exponent is positive (or zero) */
dResult = doublepow(dMantissa,lExponent);
if( dResult == floor(dResult) && fabs(dResult) <= LONG_MAX ){
RETURN_LONG_VALUE((long)dResult);
}else{
RETURN_DOUBLE_VALUE(dResult);
}
}
}
}else{ /* not bExpIsInt ***************************************************** */
/* If the exponent is not integer then we can not use any kind of integer calculation. */
dMantissa = GETDOUBLEVALUE(vMantissa);
dExponent = GETDOUBLEVALUE(vExponent);
if( dMantissa < 0.0 ){
dRoot = 1.0 / dExponent;
if( dRoot == floor(dRoot) && fabs(dRoot) <= LONG_MAX ){
lRoot = ((long)dRoot);
if( lRoot & 1 ){
dResult = -pow(-dMantissa,dExponent);
if( dResult == floor(dResult) && dResult >= -LONG_MAX ){
RESULT = NEWMORTALLONG;
ASSERTNULL(RESULT);
LONGVALUE(RESULT) = ((long)dResult);
RETURN;
}
RESULT = NEWMORTALDOUBLE;
ASSERTNULL(RESULT);
DOUBLEVALUE(RESULT) = dResult;
RETURN;
}
}
/* bad luck, the result is complex */
RESULT = NULL;
RETURN;
}else{/* dMantissa is positive and the exponent is not integer */
dResult = pow(dMantissa,dExponent);
if( dResult == floor(dResult) && fabs(dResult) <= LONG_MAX ){
RESULT = NEWMORTALLONG;
ASSERTNULL(RESULT)
LONGVALUE(RESULT) = ((long)dResult);
RETURN;
}else{
RESULT = NEWMORTALDOUBLE;
ASSERTNULL(RESULT)
DOUBLEVALUE(RESULT) = dResult;
RETURN;
}
}
}
#endif
END
/*POD
=section IDIV
=H Integer division
This operator converts the arguments to long and divides the first argument with the second.
The result is a truncated long. The truncation is done towards zero like it is done by the function
FIX and B<unlike> by the function INT.
CUT*/
COMMAND(IDIV)
#if NOTIMP_IDIV
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2;
double dop1,dop2;
long lop1,lop2;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = EVALUATEEXPRESSION(CAR(nItem));
NONULOP(Op1)
nItem = CDR(nItem);
Op2 = EVALUATEEXPRESSION(CAR(nItem));
NONULOP(Op2)
/* if any of the arguments is double then the result is double */
if( !ISINTEGER(Op1) || ! ISINTEGER(Op2) ){
dop1 = GETDOUBLEVALUE(Op1);
dop2 = GETDOUBLEVALUE(Op2);
if( dop2 == 0.0 ){
ERRORUNDEF
}
RETURN_LONG_VALUE( ((long)(dop1 / dop2)) )
}
lop1 = GETLONGVALUE(Op1);
lop2 = GETLONGVALUE(Op2);
if( lop2 == 0 ){
ERRORUNDEF
}
RETURN_LONG_VALUE( ((long)(lop1 / lop2)) )
#endif
END
/*POD
=section DIV
=H Division
This operator divides two numbers. If some of the arguments are strings then they are
converted to double or long. The result is double unless both operands are long and
the operation can be performed to result an integer value without truncation.
CUT*/
COMMAND(DIV)
#if NOTIMP_DIV
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2;
double dop1,dop2,dResult;
long lop1,lop2;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = EVALUATEEXPRESSION(CAR(nItem));
NONULOP(Op1)
nItem = CDR(nItem);
Op2 = EVALUATEEXPRESSION(CAR(nItem));
NONULOP(Op2)
/* if any of the arguments is double then the result is double */
if( ! ISINTEGER(Op1) || !ISINTEGER(Op2) ){
dop1 = GETDOUBLEVALUE(Op1);
dop2 = GETDOUBLEVALUE(Op2);
if( dop2 == 0.0 ){
ERRORUNDEF
}
RETURN_DOUBLE_VALUE_OR_LONG( dop1 / dop2 )
}
lop1 = GETLONGVALUE(Op1);
lop2 = GETLONGVALUE(Op2);
if( lop2 == 0 ){
ERRORUNDEF
}
if( lop1 % lop2 ){
RETURN_DOUBLE_VALUE( ((double)lop1) / ((double)lop2) )
}
RETURN_LONG_VALUE( lop1 / lop2)
#endif
END
/*POD
=section BYVAL
=H unary ByVal operator
This operator does nothing. Does it? It can be used to alter pass by reference
variables and help the caller to pass a variable by value. Istead of writing
=verbatim
call sub(a)
=noverbatim
it can do
=verbatim
call sub(ByVal a)
=noverbatim
and this way the subroutine can NOT alter the value of the variable T<a>.
CUT*/
COMMAND(BYVAL)
#if NOTIMP_BYVAL
NOTIMPLEMENTED;
#else
VARIABLE Op1;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
Op1 = EVALUATEEXPRESSION(CAR(PARAMETERLIST));
ASSERTOKE;
RESULT = Op1;
#endif
END