|
|
1.1 root 1:
2:
3:
4:
5:
6:
7:
8: CHAPTER 3
9:
10:
11: Arithmetic Functions
12:
13:
14:
15:
16: This chapter describes FRANZ LISP's functions for doing
17: arithmetic. Often the same function is known by many names,
18: such as _a_d_d which is also _p_l_u_s, _s_u_m, and +. This is due to
19: our desire to be compatible with other Lisps. The FRANZ
20: LISP user is advised to avoid using functions with names
21: such as + and * unless their arguments are fixnums. The
22: Lisp compiler takes advantage of the fact that their argu-
23: ments are fixnums.
24:
25: An attempt to divide or to generate a floating point
26: result outside of the range of floating point numbers will
27: cause a floating exception signal from the UNIX operating
28: system. The user can catch and process this interrupt if
29: desired (see the description of the _s_i_g_n_a_l function).
30:
31:
32:
33: 3.1. simple arithmetic functions
34:
35: (add ['n_arg1 ...])
36: (plus ['n_arg1 ...])
37: (sum ['n_arg1 ...])
38: (+ ['x_arg1 ...])
39:
40: RETURNS: the sum of the arguments. If no arguments are
41: given, 0 is returned.
42:
43: NOTE: if the size of the partial sum exceeds the limit
44: of a fixnum, the partial sum will be converted to
45: a bignum. If any of the arguments are flonums,
46: the partial sum will be converted to a flonum
47: when that argument is processed and the result
48: will thus be a flonum. Currently, if in the pro-
49: cess of doing the addition a bignum must be con-
50: verted into a flonum an error message will
51: result.
52:
53:
54:
55:
56:
57:
58:
59:
60: 9
61:
62: 9Arithmetic Functions 3-1
63:
64:
65:
66:
67:
68:
69:
70: Arithmetic Functions 3-2
71:
72:
73: (add1 'n_arg)
74: (1+ 'x_arg)
75:
76: RETURNS: its argument plus 1.
77:
78: (diff ['n_arg1 ... ])
79: (difference ['n_arg1 ... ])
80: (- ['x_arg1 ... ])
81:
82: RETURNS: the result of subtracting from n_arg1 all sub-
83: sequent arguments. If no arguments are given,
84: 0 is returned.
85:
86: NOTE: See the description of add for details on data
87: type conversions and restrictions.
88:
89: (sub1 'n_arg)
90: (1- 'x_arg)
91:
92: RETURNS: its argument minus 1.
93:
94: (minus 'n_arg)
95:
96: RETURNS: zero minus n_arg.
97:
98: (product ['n_arg1 ... ])
99: (times ['n_arg1 ... ])
100: (* ['x_arg1 ... ])
101:
102: RETURNS: the product of all of its arguments. It
103: returns 1 if there are no arguments.
104:
105: NOTE: See the description of the function _a_d_d for
106: details and restrictions to the automatic data
107: type coercion.
108:
109: (quotient ['n_arg1 ...])
110: (/ ['x_arg1 ...])
111:
112: RETURNS: the result of dividing the first argument by
113: succeeding ones.
114:
115: NOTE: If there are no arguments, 1 is returned. See
116: the description of the function _a_d_d for details
117: and restrictions of data type coercion. A divide
118: by zero will cause a floating exception interrupt
119: -- see the description of the _s_i_g_n_a_l function.
120:
121:
122:
123:
124:
125: 9
126:
127: 9 Printed: July 21, 1983
128:
129:
130:
131:
132:
133:
134:
135: Arithmetic Functions 3-3
136:
137:
138: (*quo 'i_x 'i_y)
139:
140: RETURNS: the integer part of i_x / i_y.
141:
142: (Divide 'i_dividend 'i_divisor)
143:
144: RETURNS: a list whose car is the quotient and whose
145: cadr is the remainder of the division of
146: i_dividend by i_divisor.
147:
148: NOTE: this is restricted to integer division.
149:
150: (Emuldiv 'x_fact1 'x_fact2 'x_addn 'x_divisor)
151:
152: RETURNS: a list of the quotient and remainder of this
153: operation:
154: ((x_fact1 * x_fact2) + (sign extended) x_addn) / x_divisor.
155:
156: NOTE: this is useful for creating a bignum arithmetic
157: package in Lisp.
158:
159:
160:
161: 3.2. predicates
162:
163: (numberp 'g_arg)
164:
165: (numbp 'g_arg)
166:
167: RETURNS: t iff g_arg is a number (fixnum, flonum or
168: bignum).
169:
170: (fixp 'g_arg)
171:
172: RETURNS: t iff g_arg is a fixnum or bignum.
173:
174: (floatp 'g_arg)
175:
176: RETURNS: t iff g_arg is a flonum.
177:
178: (evenp 'x_arg)
179:
180: RETURNS: t iff x_arg is even.
181:
182:
183:
184:
185:
186:
187:
188:
189:
190: 9
191:
192: 9 Printed: July 21, 1983
193:
194:
195:
196:
197:
198:
199:
200: Arithmetic Functions 3-4
201:
202:
203: (oddp 'x_arg)
204:
205: RETURNS: t iff x_arg is odd.
206:
207: (zerop 'g_arg)
208:
209: RETURNS: t iff g_arg is a number equal to 0.
210:
211: (onep 'g_arg)
212:
213: RETURNS: t iff g_arg is a number equal to 1.
214:
215: (plusp 'n_arg)
216:
217: RETURNS: t iff n_arg is greater than zero.
218:
219: (minusp 'g_arg)
220:
221: RETURNS: t iff g_arg is a negative number.
222:
223: (greaterp ['n_arg1 ...])
224: (> 'fx_arg1 'fx_arg2)
225: (>& 'x_arg1 'x_arg2)
226:
227: RETURNS: t iff the arguments are in a strictly decreas-
228: ing order.
229:
230: NOTE: In functions _g_r_e_a_t_e_r_p and > the function _d_i_f_f_e_r_-
231: _e_n_c_e is used to compare adjacent values. If any
232: of the arguments are non-numbers, the error mes-
233: sage will come from the _d_i_f_f_e_r_e_n_c_e function. The
234: arguments to > must be fixnums or both flonums.
235: The arguments to >& must both be fixnums.
236:
237: (lessp ['n_arg1 ...])
238: (< 'fx_arg1 'fx_arg2)
239: (<& 'x_arg1 'x_arg2)
240:
241: RETURNS: t iff the arguments are in a strictly increas-
242: ing order.
243:
244: NOTE: In functions _l_e_s_s_p and < the function _d_i_f_f_e_r_e_n_c_e
245: is used to compare adjacent values. If any of the
246: arguments are non numbers, the error message will
247: come from the _d_i_f_f_e_r_e_n_c_e function. The arguments
248: to < may be either fixnums or flonums but must be
249: the same type. The arguments to <& must be fix-
250: nums.
251:
252:
253:
254:
255: 9
256:
257: 9 Printed: July 21, 1983
258:
259:
260:
261:
262:
263:
264:
265: Arithmetic Functions 3-5
266:
267:
268: (= 'fx_arg1 'fx_arg2)
269:
270: (=& 'x_arg1 'x_arg2)
271:
272: RETURNS: t iff the arguments have the same value. The
273: arguments to = must be the either both fixnums
274: or both flonums. The arguments to =& must be
275: fixnums.
276:
277:
278:
279: 3.3. trignometric functions
280:
281: (cos 'fx_angle)
282:
283: RETURNS: the (flonum) cosine of fx_angle (which is
284: assumed to be in radians).
285:
286: (sin 'fx_angle)
287:
288: RETURNS: the sine of fx_angle (which is assumed to be
289: in radians).
290:
291: (acos 'fx_arg)
292:
293: RETURNS: the (flonum) arc cosine of fx_arg in the range
294: 0 to J.
295:
296: (asin 'fx_arg)
297:
298: RETURNS: the (flonum) arc sine of fx_arg in the range
299: -J/2 to J/2.
300:
301: (atan 'fx_arg1 'fx_arg2)
302:
303: RETURNS: the (flonum) arc tangent of fx_arg1/fx_arg2 in
304: the range -J to J.
305:
306:
307:
308: 3.4. bignum functions
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320: 9
321:
322: 9 Printed: July 21, 1983
323:
324:
325:
326:
327:
328:
329:
330: Arithmetic Functions 3-6
331:
332:
333: (haipart bx_number x_bits)
334:
335: RETURNS: a fixnum (or bignum) which contains the x_bits
336: high bits of (_a_b_s _b_x__n_u_m_b_e_r) if x_bits is
337: positive, otherwise it returns the
338: (_a_b_s _x__b_i_t_s) low bits of (_a_b_s _b_x__n_u_m_b_e_r).
339:
340: (haulong bx_number)
341:
342: RETURNS: the number of significant bits in bx_number.
343:
344: NOTE: the result is equal to the least integer greater
345: to or equal to the base two logarithm of one plus
346: the absolute value of bx_number.
347:
348: (bignum-leftshift bx_arg x_amount)
349:
350: RETURNS: bx_arg shifted left by x_amount. If x_amount
351: is negative, bx_arg will be shifted right by
352: the magnitude of x_amount.
353:
354: NOTE: If bx_arg is shifted right, it will be rounded to
355: the nearest even number.
356:
357: (sticky-bignum-leftshift 'bx_arg 'x_amount)
358:
359: RETURNS: bx_arg shifted left by x_amount. If x_amount
360: is negative, bx_arg will be shifted right by
361: the magnitude of x_amount and rounded.
362:
363: NOTE: sticky rounding is done this way: after shifting,
364: the low order bit is changed to 1 if any 1's were
365: shifted off to the right.
366:
367:
368:
369: 3.5. bit manipulation
370:
371: (boole 'x_key 'x_v1 'x_v2 ...)
372:
373: RETURNS: the result of the bitwise boolean operation as
374: described in the following table.
375:
376: NOTE: If there are more than 3 arguments, then evalua-
377: tion proceeds left to right with each partial
378: result becoming the new value of x_v1. That is,
379: (_b_o_o_l_e '_k_e_y '_v_1 '_v_2 '_v_3) =_ (_b_o_o_l_e '_k_e_y (_b_o_o_l_e '_k_e_y '_v_1 '_v_2) '_v_3).
380: In the following table, * represents bitwise and,
381: + represents bitwise or, O+ represents bitwise xor
382: and _ represents bitwise negation and is the
383: highest precedence operator.
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396: Arithmetic Functions 3-7
397:
398:
399: 8____________________________________________________________________________________________
400: (boole 'key 'x 'y)
401:
402: 8________________________________________________________________________________________________________________________________________________________________________________________
403: key 0 1 2 3 4 5 6 7
404: result 0 x * y _ x * y y x * _ y x x O+ y x + y
405:
406: common
407: names and bitclear xor or
408:
409: 8____________________________________________________________________________________________
410:
411: key 8 9 10 11 12 13 14 15
412: result _ (x + y) _(x O+ y) _ x _ x + y _ y x + _ y _ x + _ y -1
413: common
414: names nor equiv implies nand
415: 8____________________________________________________________________________________________
416: 7|8|7|7|7|7|7|7|7|7|7|7|7|7|7|
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429: 9 |8|7|7|7|7|7|7|7|7|7|7|7|7|7|
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444: 9
445: (lsh 'x_val 'x_amt)
446:
447: RETURNS: x_val shifted left by x_amt if x_amt is posi-
448: tive. If x_amt is negative, then _l_s_h returns
449: x_val shifted right by the magnitude if x_amt.
450:
451: NOTE: This always returns a fixnum even for those
452: numbers whose magnitude is so large that they
453: would normally be represented as a bignum, i.e.
454: shifter bits are lost. For more general bit
455: shifters, see _b_i_g_n_u_m-_l_e_f_t_s_h_i_f_t and _s_t_i_c_k_y-
456: _b_i_g_n_u_m-_l_e_f_t_s_h_i_f_t.
457:
458: (rot 'x_val 'x_amt)
459:
460: RETURNS: x_val rotated left by x_amt if x_amt is posi-
461: tive. If x_amt is negative, then x_val is
462: rotated right by the magnitude of x_amt.
463:
464:
465:
466: 3.6. other functions
467:
468: (abs 'n_arg)
469: (absval 'n_arg)
470:
471: RETURNS: the absolute value of n_arg.
472:
473:
474:
475:
476:
477:
478:
479:
480: 9
481:
482: 9 Printed: July 21, 1983
483:
484:
485:
486:
487:
488:
489:
490: Arithmetic Functions 3-8
491:
492:
493: (exp 'fx_arg)
494:
495: RETURNS: _e raised to the fx_arg power (flonum) .
496:
497: (expt 'n_base 'n_power)
498:
499: RETURNS: n_base raised to the n_power power.
500:
501: NOTE: if either of the arguments are flonums, the cal-
502: culation will be done using _l_o_g and _e_x_p.
503:
504: (fact 'x_arg)
505:
506: RETURNS: x_arg factorial. (fixnum or bignum)
507:
508: (fix 'n_arg)
509:
510: RETURNS: a fixnum as close as we can get to n_arg.
511:
512: NOTE: _f_i_x will round down. Currently, if n_arg is a
513: flonum larger than the size of a fixnum, this
514: will fail.
515:
516: (float 'n_arg)
517:
518: RETURNS: a flonum as close as we can get to n_arg.
519:
520: NOTE: if n_arg is a bignum larger than the maximum size
521: of a flonum, then a floating exception will
522: occur.
523:
524: (log 'fx_arg)
525:
526: RETURNS: the natural logarithm of fx_arg.
527:
528: (max 'n_arg1 ... )
529:
530: RETURNS: the maximum value in the list of arguments.
531:
532: (min 'n_arg1 ... )
533:
534: RETURNS: the minimum value in the list of arguments.
535:
536:
537:
538:
539:
540:
541:
542:
543:
544:
545: 9
546:
547: 9 Printed: July 21, 1983
548:
549:
550:
551:
552:
553:
554:
555: Arithmetic Functions 3-9
556:
557:
558: (mod 'i_dividend 'i_divisor)
559: (remainder 'i_dividend 'i_divisor)
560:
561: RETURNS: the remainder when i_dividend is divided by
562: i_divisor.
563:
564: NOTE: The sign of the result will have the same sign as
565: i_dividend.
566:
567: (*mod 'x_dividend 'x_divisor)
568:
569: RETURNS: the balanced representation of x_dividend
570: modulo x_divisor.
571:
572: NOTE: the range of the balanced representation is
573: abs(x_divisor)/2 to (abs(x_divisor)/2) -
574: x_divisor + 1.
575:
576: (random ['x_limit])
577:
578: RETURNS: a fixnum between 0 and x_limit - 1 if x_limit
579: is given. If x_limit is not given, any fix-
580: num, positive or negative, might be returned.
581:
582: (sqrt 'fx_arg)
583:
584: RETURNS: the square root of fx_arg.
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: 9
611:
612: 9 Printed: July 21, 1983
613:
614:
615:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.