16 {
if FLONUMP(x) return(truth); else return(NIL);}
18 static LISP lplus(LISP args)
22 for (sum=0.0,l=args; l != NIL; l=cdr(l))
24 if (NFLONUMP(car(l))) err(
"wrong type of argument to plus",car(l));
30 static LISP ltimes(LISP args)
34 for (product=1.0,l=args; l != NIL; l=cdr(l))
36 if (NFLONUMP(car(l))) err(
"wrong type of argument to times",car(l));
37 product *= FLONM(car(l));
39 return flocons(product);
42 static LISP difference(LISP x,LISP y)
43 {
if NFLONUMP(x) err("wrong type of argument(1st) to difference",x);
44 if NFLONUMP(y) err("wrong type of argument(2nd) to difference",y);
45 return(flocons(FLONM(x) - FLONM(y)));}
47 static LISP quotient(LISP x,LISP y)
48 {
if NFLONUMP(x) err("wrong type of argument(1st) to quotient",x);
49 if NFLONUMP(y) err("wrong type of argument(2nd) to quotient",y);
50 return(flocons(FLONM(x)/FLONM(y)));}
52 static LISP greaterp(LISP x,LISP y)
53 {
if NFLONUMP(x) err("wrong type of argument(1st) to greaterp",x);
54 if NFLONUMP(y) err("wrong type of argument(2nd) to greaterp",y);
55 if (FLONM(x)>FLONM(y)) return(truth);
58 static LISP lessp(LISP x,LISP y)
59 {
if NFLONUMP(x) err("wrong type of argument(1st) to lessp",x);
60 if NFLONUMP(y) err("wrong type of argument(2nd) to lessp",y);
61 if (FLONM(x)<FLONM(y)) return(truth);
64 static LISP l_nint(LISP number)
66 if (TYPEP(number,tc_flonum))
68 int iii = (int)(FLONM(number)+0.5);
71 else if (TYPEP(number,tc_symbol))
73 int iii = (int)(atof(get_c_string(number))+0.5);
77 err(
"nint: argument not a number",number);
82 static LISP l_log(LISP n)
84 if (n && (TYPEP(n,tc_flonum)))
85 return flocons(log(FLONM(n)));
87 err(
"log: not a number",n);
94 double r = (double)abs(rand())/(
double)RAND_MAX;
99 static LISP l_srand(LISP seed)
101 if (seed && (TYPEP(seed,tc_flonum)))
102 srand((
int) FLONM(seed));
104 err(
"srand: not a number", seed);
108 static LISP l_exp(LISP n)
110 if (n && (TYPEP(n,tc_flonum)))
111 return flocons(exp(FLONM(n)));
113 err(
"exp: not a number",n);
117 static LISP l_sqrt(LISP n)
119 if (n && (TYPEP(n,tc_flonum)))
120 return flocons(sqrt(FLONM(n)));
122 err(
"sqrt: not a number",n);
126 static LISP l_pow(LISP x, LISP y)
128 if (x && (TYPEP(x,tc_flonum)) &&
129 y && (TYPEP(y,tc_flonum)))
130 return flocons(pow(FLONM(x),FLONM(y)));
132 err(
"pow: x or y not a number",cons(x,cons(y,NIL)));
136 static LISP l_mod(LISP x, LISP y)
138 if (x && (TYPEP(x,tc_flonum)) &&
139 y && (TYPEP(y,tc_flonum)))
146 err(
"mod: y cannot be 0",cons(x,cons(y,NIL)));
148 return flocons((
float)(a%b));
151 err(
"mod: x or y not a number",cons(x,cons(y,NIL)));
155 void init_subrs_math(
void)
157 init_subr_1(
"number?",numberp,
159 Returns t if DATA is a number, nil otherwise.");
160 init_lsubr(
"+",lplus,
161 "(+ NUM1 NUM2 ...)\n\
162 Returns the sum of NUM1 and NUM2 ... An error is given is any argument\n\
164 init_subr_2(
"-",difference,
166 Returns the difference between NUM1 and NUM2. An error is given is any\n\
167 argument is not a number.");
168 init_lsubr(
"*",ltimes,
169 "(* NUM1 NUM2 ...)\n\
170 Returns the product of NUM1 and NUM2 ... An error is given is any\n\
171 argument is not a number.");
172 init_subr_2(
"/",quotient,
174 Returns the quotient of NUM1 and NUM2. An error is given is any\n\
175 argument is not a number.");
176 init_subr_2(
">",greaterp,
178 Returns t if NUM1 is greater than NUM2, nil otherwise. An error is\n\
179 given is either argument is not a number.");
180 init_subr_2(
"<",lessp,
182 Returns t if NUM1 is less than NUM2, nil otherwise. An error is\n\
183 given is either argument is not a number.");
184 init_subr_1(
"nint",l_nint,
186 Returns nearest int to NUMBER.");
187 init_subr_1(
"log",l_log,
189 Return natural log of NUM.");
190 init_subr_0(
"rand",l_rand,
192 Returns a pseudo random number between 0 and 1 using the libc rand()\n\
194 init_subr_1(
"srand",l_srand,
196 Seeds the libc pseudo random number generator with the integer SEED.");
197 init_subr_1(
"exp",l_exp,
200 init_subr_1(
"sqrt",l_sqrt,
202 Return square root of NUM.");
203 init_subr_2(
"pow",l_pow,
206 init_subr_2(
"%",l_mod,