2
Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa
4
This file is part of GNU Common Lisp, herein referred to as GCL
6
GCL is free software; you can redistribute it and/or modify it under
7
the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
8
the Free Software Foundation; either version 2, or (at your option)
11
GCL is distributed in the hope that it will be useful, but WITHOUT
12
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14
License for more details.
16
You should have received a copy of the GNU Library General Public License
17
along with GCL; see the file COPYING. If not, write to the Free Software
18
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28
#include "num_include.h"
30
object fixnum_add(i,j)
35
{ if (j<= (MOST_POSITIVE_FIX-i))
36
{ MYmake_fixnum(return,i+j);
38
MPOP(return,addss,i,j);
40
if ((MOST_NEG_FIXNUM -i) <= j) {
41
MYmake_fixnum(return,i+j);
43
MPOP(return,addss,i,j);
47
object fixnum_sub(i,j)
52
{ if (j >= (i - MOST_POSITIVE_FIX))
53
{ MYmake_fixnum(return,i-j);
55
MPOP(return,subss,i,j);
57
if ((MOST_NEG_FIXNUM -i) <= -j) {
58
MYmake_fixnum(return,i-j);
60
MPOP(return,subss,i,j);
69
MPOP(return,mulss,i,j);
85
z = alloc_object(t_complex);
87
z->cmp.cmp_imag = small_fixnum(0);
94
FEwrong_type_argument(sLnumber, x);
105
switch (type_of(x)) {
109
return fixnum_add(fix(x),fix(y));
111
MPOP(return, addsi,fix(x),MP(y));
113
z = number_plus(number_times(x, y->rat.rat_den),
115
return make_ratio(z, y->rat.rat_den);
117
dx = (double)(fix(x));
118
dy = (double)(sf(y));
121
dx = (double)(fix(x));
127
FEwrong_type_argument(sLnumber, y);
131
switch (type_of(y)) {
133
MPOP(return,addsi,fix(y),MP(x));
135
MPOP(return,addii,MP(y),MP(x));
137
z = number_plus(number_times(x, y->rat.rat_den), y->rat.rat_num);
138
return make_ratio(z, y->rat.rat_den);
140
dx = number_to_double(x);
141
dy = (double)(sf(y));
144
dx = number_to_double(x);
150
FEwrong_type_argument(sLnumber, y);
154
switch (type_of(y)) {
158
z = number_plus(x->rat.rat_num,
159
number_times(x->rat.rat_den, y));
160
z = make_ratio(z, x->rat.rat_den);
164
z = number_plus(number_times(x->rat.rat_num,y->rat.rat_den),
165
number_times(x->rat.rat_den,y->rat.rat_num));
166
z = make_ratio(z,number_times(x->rat.rat_den,y->rat.rat_den));
169
dx = number_to_double(x);
170
dy = (double)(sf(y));
173
dx = number_to_double(x);
179
FEwrong_type_argument(sLnumber, y);
183
switch (type_of(y)) {
185
dx = (double)(sf(x));
186
dy = (double)(fix(y));
189
dx = (double)(sf(x));
190
dy = (double)(sf(y));
193
dx = (double)(sf(x));
199
dx = (double)(sf(x));
200
dy = number_to_double(y);
204
z = alloc_object(t_shortfloat);
205
sf(z) = (shortfloat)(dx + dy);
210
switch (type_of(y)) {
212
dy = (double)(fix(y));
215
dy = (double)(sf(y));
223
dy = number_to_double(y);
227
z = alloc_object(t_longfloat);
233
x = number_to_complex(x);
234
y = number_to_complex(y);
235
z = make_complex(number_plus(x->cmp.cmp_real, y->cmp.cmp_real),
236
number_plus(x->cmp.cmp_imag, y->cmp.cmp_imag));
240
FEwrong_type_argument(sLnumber, x);
253
switch (type_of(x)) {
257
if (fix(x)< MOST_POSITIVE_FIX-1) {
258
MYmake_fixnum(return,fix(x)+1);
260
MPOP(return,addss,1,fix(x));
262
MPOP(return,addsi,1,MP(x));
264
z = number_plus(x->rat.rat_num, x->rat.rat_den);
265
z = make_ratio(z, x->rat.rat_den);
269
dx = (double)(sf(x));
270
z = alloc_object(t_shortfloat);
271
sf(z) = (shortfloat)(dx + 1.0);
276
z = alloc_object(t_longfloat);
282
z = make_complex(one_plus(x->cmp.cmp_real), x->cmp.cmp_imag);
286
FEwrong_type_argument(sLnumber, x);
299
switch (type_of(x)) {
304
MPOP(return,subss,fix(x),fix(y));
306
MPOP(return, subsi,fix(x),MP(y));
308
z = number_minus(number_times(x, y->rat.rat_den), y->rat.rat_num);
309
z = make_ratio(z, y->rat.rat_den);
312
dx = (double)(fix(x));
313
dy = (double)(sf(y));
316
dx = (double)(fix(x));
322
FEwrong_type_argument(sLnumber, y);
326
switch (type_of(y)) {
328
MPOP(return,subis,MP(x),fix(y));
330
MPOP(return,subii,MP(x),MP(y));
332
z = number_minus(number_times(x, y->rat.rat_den), y->rat.rat_num);
333
z = make_ratio(z, y->rat.rat_den);
336
dx = number_to_double(x);
337
dy = (double)(sf(y));
340
dx = number_to_double(x);
346
FEwrong_type_argument(sLnumber, y);
350
switch (type_of(y)) {
353
z = number_minus(x->rat.rat_num, number_times(x->rat.rat_den, y));
354
z = make_ratio(z, x->rat.rat_den);
357
z = number_minus(number_times(x->rat.rat_num,y->rat.rat_den),
358
(number_times(x->rat.rat_den,y->rat.rat_num)));
359
z = make_ratio(z,number_times(x->rat.rat_den,y->rat.rat_den));
362
dx = number_to_double(x);
363
dy = (double)(sf(y));
366
dx = number_to_double(x);
372
FEwrong_type_argument(sLnumber, y);
376
switch (type_of(y)) {
378
dx = (double)(sf(x));
379
dy = (double)(fix(y));
382
dx = (double)(sf(x));
383
dy = (double)(sf(y));
386
dx = (double)(sf(x));
392
dx = (double)(sf(x));
393
dy = number_to_double(y);
397
z = alloc_object(t_shortfloat);
398
sf(z) = (shortfloat)(dx - dy);
403
switch (type_of(y)) {
405
dy = (double)(fix(y));
408
dy = (double)(sf(y));
416
dy = number_to_double(y);
419
z = alloc_object(t_longfloat);
425
x = number_to_complex(x);
426
y = number_to_complex(y);
427
z = make_complex(number_minus(x->cmp.cmp_real, y->cmp.cmp_real),
428
number_minus(x->cmp.cmp_imag, y->cmp.cmp_imag));
432
FEwrong_type_argument(sLnumber, x);
443
switch (type_of(x)) {
446
MPOP(return,addss,fix(x),-1);
448
MPOP(return,addsi,-1,MP(x));
450
z = number_minus(x->rat.rat_num, x->rat.rat_den);
451
z = make_ratio(z, x->rat.rat_den);
455
dx = (double)(sf(x));
456
z = alloc_object(t_shortfloat);
457
sf(z) = (shortfloat)(dx - 1.0);
462
z = alloc_object(t_longfloat);
468
z = make_complex(one_minus(x->cmp.cmp_real), x->cmp.cmp_imag);
472
FEwrong_type_argument(sLnumber, x);
482
switch (type_of(x)) {
485
if(fix(x) == MOST_NEGATIVE_FIX)
486
return fixnum_add(1,MOST_POSITIVE_FIX);
488
return(make_fixnum(-fix(x)));
492
z1 = number_negate(x->rat.rat_num);
493
z = alloc_object(t_ratio);
495
z->rat.rat_den = x->rat.rat_den;
499
z = alloc_object(t_shortfloat);
504
z = alloc_object(t_longfloat);
509
z = make_complex(number_negate(x->cmp.cmp_real),
510
number_negate(x->cmp.cmp_imag));
514
FEwrong_type_argument(sLnumber, x);
525
switch (type_of(x)) {
528
switch (type_of(y)) {
530
MPOP(return,mulss,fix(x),fix(y));
532
MPOP(return,mulsi,fix(x),MP(y));
534
z = make_ratio(number_times(x, y->rat.rat_num), y->rat.rat_den);
537
dx = (double)(fix(x));
538
dy = (double)(sf(y));
541
dx = (double)(fix(x));
547
FEwrong_type_argument(sLnumber, y);
551
switch (type_of(y)) {
553
MPOP(return,mulsi,fix(y),MP(x));
556
MPOP(return,mulii,MP(y),MP(x));
558
z = make_ratio(number_times(x, y->rat.rat_num), y->rat.rat_den);
561
dx = number_to_double(x);
562
dy = (double)(sf(y));
565
dx = number_to_double(x);
571
FEwrong_type_argument(sLnumber, y);
575
switch (type_of(y)) {
578
z = make_ratio(number_times(x->rat.rat_num, y), x->rat.rat_den);
581
z = make_ratio(number_times(x->rat.rat_num,y->rat.rat_num),
582
number_times(x->rat.rat_den,y->rat.rat_den));
585
dx = number_to_double(x);
586
dy = (double)(sf(y));
589
dx = number_to_double(x);
595
FEwrong_type_argument(sLnumber, y);
599
switch (type_of(y)) {
601
dx = (double)(sf(x));
602
dy = (double)(fix(y));
605
dx = (double)(sf(x));
606
dy = (double)(sf(y));
609
dx = (double)(sf(x));
615
dx = (double)(sf(x));
616
dy = number_to_double(y);
620
z = alloc_object(t_shortfloat);
621
sf(z) = (shortfloat)(dx * dy);
626
switch (type_of(y)) {
628
dy = (double)(fix(y));
631
dy = (double)(sf(y));
639
dy = number_to_double(y);
642
z = alloc_object(t_longfloat);
649
object z1, z2, z11, z12, z21, z22;
651
x = number_to_complex(x);
652
y = number_to_complex(y);
653
z11 = number_times(x->cmp.cmp_real, y->cmp.cmp_real);
654
z12 = number_times(x->cmp.cmp_imag, y->cmp.cmp_imag);
655
z21 = number_times(x->cmp.cmp_imag, y->cmp.cmp_real);
656
z22 = number_times(x->cmp.cmp_real, y->cmp.cmp_imag);
657
z1 = number_minus(z11, z12);
658
z2 = number_plus(z21, z22);
659
z = make_complex(z1, z2);
664
FEwrong_type_argument(sLnumber, x);
675
switch (type_of(x)) {
679
switch (type_of(y)) {
682
if(number_zerop(y) == TRUE)
684
if (number_minusp(y) == TRUE) {
685
x = number_negate(x);
686
y = number_negate(y);
688
z = make_ratio(x, y);
691
if(number_zerop(y->rat.rat_num))
693
z = make_ratio(number_times(x, y->rat.rat_den), y->rat.rat_num);
696
dx = number_to_double(x);
697
dy = (double)(sf(y));
700
dx = number_to_double(x);
706
FEwrong_type_argument(sLnumber, y);
710
switch (type_of(y)) {
715
z = make_ratio(x->rat.rat_num, number_times(x->rat.rat_den, y));
718
z = make_ratio(number_times(x->rat.rat_num,y->rat.rat_den),
719
number_times(x->rat.rat_den,y->rat.rat_num));
722
dx = number_to_double(x);
723
dy = (double)(sf(y));
726
dx = number_to_double(x);
732
FEwrong_type_argument(sLnumber, y);
736
switch (type_of(y)) {
738
dx = (double)(sf(x));
739
dy = (double)(fix(y));
742
dx = (double)(sf(x));
743
dy = (double)(sf(y));
746
dx = (double)(sf(x));
752
dx = (double)(sf(x));
753
dy = number_to_double(y);
757
z = alloc_object(t_shortfloat);
760
sf(z) = (shortfloat)(dx / dy);
766
switch (type_of(y)) {
768
dy = (double)(fix(y));
771
dy = (double)(sf(y));
779
dy = number_to_double(y);
782
z = alloc_object(t_longfloat);
793
x = number_to_complex(x);
794
y = number_to_complex(y);
795
z1 = number_times(y->cmp.cmp_real, y->cmp.cmp_real);
796
z2 = number_times(y->cmp.cmp_imag, y->cmp.cmp_imag);
797
if (number_zerop(z3 = number_plus(z1, z2)))
799
z1 = number_times(x->cmp.cmp_real, y->cmp.cmp_real);
800
z2 = number_times(x->cmp.cmp_imag, y->cmp.cmp_imag);
801
z1 = number_plus(z1, z2);
802
z = number_times(x->cmp.cmp_imag, y->cmp.cmp_real);
803
z2 = number_times(x->cmp.cmp_real, y->cmp.cmp_imag);
804
z2 = number_minus(z, z2);
805
z1 = number_divide(z1, z3);
806
z2 = number_divide(z2, z3);
807
z = make_complex(z1, z2);
812
FEwrong_type_argument(sLnumber, x);
817
integer_divide1(x, y)
822
integer_quotient_remainder_1(x, y, &q, &r);
833
if (number_minusp(x))
834
x = number_negate(x);
835
if (number_minusp(y))
836
y = number_negate(y);
839
if (type_of(x) == t_fixnum && type_of(y) == t_fixnum) {
849
return(make_fixnum(i));
857
if (number_compare(x, y) < 0) {
862
if (type_of(y) == t_fixnum && fix(y) == 0) {
865
integer_quotient_remainder_1(x, y, &q, &r);
876
j = vs_top - vs_base;
878
vs_push(small_fixnum(0));
881
for (i = 0; i < j; i++)
882
check_type_number(&vs_base[i]);
883
for (i = 1; i < j; i++)
884
vs_base[0] = number_plus(vs_base[0], vs_base[i]);
892
j = vs_top - vs_base;
895
for (i = 0; i < j ; i++)
896
check_type_number(&vs_base[i]);
898
vs_base[0] = number_negate(vs_base[0]);
901
for (i = 1; i < j; i++)
902
vs_base[0] = number_minus(vs_base[0], vs_base[i]);
910
j = vs_top - vs_base;
912
vs_push(small_fixnum(1));
915
for (i = 0; i < j; i++)
916
check_type_number(&vs_base[i]);
917
for (i = 1; i < j; i++)
918
vs_base[0] = number_times(vs_base[0], vs_base[i]);
926
j = vs_top - vs_base;
929
for(i = 0; i < j; i++)
930
check_type_number(&vs_base[i]);
932
vs_base[0] = number_divide(small_fixnum(1), vs_base[0]);
935
for (i = 1; i < j; i++)
936
vs_base[0] = number_divide(vs_base[0], vs_base[i]);
945
check_type_number(&vs_base[0]);
946
vs_base[0] = one_plus(vs_base[0]);
954
check_type_number(&vs_base[0]);
955
vs_base[0] = one_minus(vs_base[0]);
963
check_type_number(&vs_base[0]);
965
if (type_of(c) == t_complex) {
966
i = number_negate(c->cmp.cmp_imag);
968
vs_base[0] = make_complex(c->cmp.cmp_real, i);
977
narg = vs_top - vs_base;
979
vs_push(small_fixnum(0));
982
for (i = 0; i < narg; i++)
983
check_type_integer(&vs_base[i]);
985
if (number_minusp(vs_base[0]))
986
vs_base[0] = number_negate(vs_base[0]);
989
for (i = 1; i < narg; i++)
990
vs_base[0] = get_gcd(vs_base[0], vs_base[i]);
999
narg = vs_top - vs_base;
1001
too_few_arguments();
1002
for (i = 0; i < narg; i++)
1003
check_type_integer(&vs_base[i]);
1005
if (number_minusp(vs_base[0]))
1006
vs_base[0] = number_negate(vs_base[0]);
1009
for (i = 1; i < narg; i++) {
1010
t = number_times(vs_base[0], vs_base[i]);
1012
g = get_gcd(vs_base[0], vs_base[i]);
1014
vs_base[0] = number_divide(t, g);
1018
if (number_minusp(vs_base[0]))
1019
vs_base[0] = number_negate(vs_base[0]);
1025
FEerror("Zero divisor.", 0);
1030
make_function("+", Lplus);
1031
make_function("-", Lminus);
1032
make_function("*", Ltimes);
1033
make_function("/", Ldivide);
1034
make_function("1+", Lone_plus);
1035
make_function("1-", Lone_minus);
1036
make_function("CONJUGATE", Lconjugate);
1037
make_function("GCD", Lgcd);
1038
make_function("LCM", Llcm);