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_sin(LISP n)
119 if (n && (TYPEP(n,tc_flonum)))
120 return flocons(sin(FLONM(n)));
122 err(
"sin: not a number",n);
126 static LISP l_cos(LISP n)
128 if (n && (TYPEP(n,tc_flonum)))
129 return flocons(cos(FLONM(n)));
131 err(
"cos: not a number",n);
135 static LISP l_tan(LISP n)
137 if (n && (TYPEP(n,tc_flonum)))
138 return flocons(tan(FLONM(n)));
140 err(
"tan: not a number",n);
144 static LISP l_asin(LISP n)
146 if (n && (TYPEP(n,tc_flonum)))
147 return flocons(asin(FLONM(n)));
149 err(
"asin: not a number",n);
153 static LISP l_acos(LISP n)
155 if (n && (TYPEP(n,tc_flonum)))
156 return flocons(acos(FLONM(n)));
158 err(
"acos: not a number",n);
162 static LISP l_atan(LISP n)
164 if (n && (TYPEP(n,tc_flonum)))
165 return flocons(atan(FLONM(n)));
167 err(
"atan: not a number",n);
171 static LISP l_sqrt(LISP n)
173 if (n && (TYPEP(n,tc_flonum)))
174 return flocons(sqrt(FLONM(n)));
176 err(
"sqrt: not a number",n);
180 static LISP l_pow(LISP x, LISP y)
182 if (x && (TYPEP(x,tc_flonum)) &&
183 y && (TYPEP(y,tc_flonum)))
184 return flocons(pow(FLONM(x),FLONM(y)));
186 err(
"pow: x or y not a number",cons(x,cons(y,NIL)));
190 static LISP l_mod(LISP x, LISP y)
192 if (x && (TYPEP(x,tc_flonum)) &&
193 y && (TYPEP(y,tc_flonum)))
200 err(
"mod: y cannot be 0",cons(x,cons(y,NIL)));
202 return flocons((
float)(a%b));
205 err(
"mod: x or y not a number",cons(x,cons(y,NIL)));
209 void init_subrs_math(
void)
211 init_subr_1(
"number?",numberp,
213 Returns t if DATA is a number, nil otherwise.");
214 init_lsubr(
"+",lplus,
215 "(+ NUM1 NUM2 ...)\n\
216 Returns the sum of NUM1 and NUM2 ... An error is given is any argument\n\
218 init_subr_2(
"-",difference,
220 Returns the difference between NUM1 and NUM2. An error is given is any\n\
221 argument is not a number.");
222 init_lsubr(
"*",ltimes,
223 "(* NUM1 NUM2 ...)\n\
224 Returns the product of NUM1 and NUM2 ... An error is given is any\n\
225 argument is not a number.");
226 init_subr_2(
"/",quotient,
228 Returns the quotient of NUM1 and NUM2. An error is given is any\n\
229 argument is not a number.");
230 init_subr_2(
">",greaterp,
232 Returns t if NUM1 is greater than NUM2, nil otherwise. An error is\n\
233 given is either argument is not a number.");
234 init_subr_2(
"<",lessp,
236 Returns t if NUM1 is less than NUM2, nil otherwise. An error is\n\
237 given is either argument is not a number.");
238 init_subr_1(
"nint",l_nint,
240 Returns nearest int to NUMBER.");
241 init_subr_1(
"log",l_log,
243 Return natural log of NUM.");
244 init_subr_0(
"rand",l_rand,
246 Returns a pseudo random number between 0 and 1 using the libc rand()\n\
248 init_subr_1(
"srand",l_srand,
250 Seeds the libc pseudo random number generator with the integer SEED.");
251 init_subr_1(
"exp",l_exp,
254 init_subr_1(
"sin",l_sin,
256 Return sine of NUM.");
257 init_subr_1(
"cos",l_cos,
259 Return cosine of NUM.");
260 init_subr_1(
"tan",l_tan,
262 Return tangent of NUM.");
263 init_subr_1(
"asin",l_asin,
265 Return arcsine of NUM.");
266 init_subr_1(
"acos",l_acos,
268 Return arccosine of NUM.");
269 init_subr_1(
"atan",l_atan,
271 Return arctangent of NUM.");
272 init_subr_1(
"sqrt",l_sqrt,
274 Return square root of NUM.");
275 init_subr_2(
"pow",l_pow,
278 init_subr_2(
"%",l_mod,