document distributed with 4.1BSD
[unix-history] / usr / src / old / lisp / PSD.doc / ch3.n
CommitLineData
8dd50195
NC
1.\" Copyright (c) 1980 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
dd881dde 5.\" @(#)ch3.n 6.1 (Berkeley) %G%
8dd50195 6.\"
dd881dde
NC
7." $Header: ch3.n,v 1.3 83/06/21 13:00:48 sklower Exp $
8.Lc Arithmetic\ Functions 3
9.pp
10This chapter describes
11.Fr "'s"
12functions for doing arithmetic.
13Often the same function is known by many names.
14For example,
15.i add
16is also
17.i plus ,
18and
19.i sum .
20This is caused by our desire to be compatible with other Lisps.
21The
22.Fr
23user should avoid using functions with names
24such as \(pl and \(** unless
25their arguments are fixnums.
26The Lisp compiler takes advantage of these implicit declarations.
27.pp
28An attempt to divide or to generate a floating
29point result outside of the range of
30floating point numbers
31will cause a floating exception signal
32from the UNIX operating system.
33The user can catch and process this interrupt if desired (see the
34description of the
35.i signal
36function).
37.sh 2 Simple\ Arithmetic\ Functions \n(ch 1
38.Lf add "['n_arg1 ...]"
39.Lx plus "['n_arg1 ...]"
40.Lx sum "['n_arg1 ...]"
41.Lx \(pl "['x_arg1 ...]"
42.Re
43the sum of the arguments. If no arguments are given, 0 is returned.
44.No
45if the size of the partial sum exceeds the limit of a fixnum, the
46partial sum will be converted to a bignum.
47If any of the arguments are flonums, the partial sum will be
48converted to a flonum when that argument is processed and the
49result will thus be a flonum.
50Currently, if in the process of doing the
51addition a bignum must be converted into
52a flonum an error message will result.
53.Lf add1 'n_arg
54.Lx 1+ 'x_arg
55.Re
56its argument plus 1.
57.Lf diff "['n_arg1 ... ]"
58.Lx difference "['n_arg1 ... ]"
59.Lx \(mi "['x_arg1 ... ]"
60.Re
61the result of subtracting from n_arg1 all subsequent arguments.
62If no arguments are given, 0 is returned.
63.No
64See the description of add for details on data type conversions and
65restrictions.
66.Lf sub1 "'n_arg"
67.Lx 1\(mi "'x_arg"
68.Re
69its argument minus 1.
70.Lf minus "'n_arg"
71.Re
72zero minus n_arg.
73.Lf product "['n_arg1 ... ]"
74.Lx times "['n_arg1 ... ]"
75.Lx \(** "['x_arg1 ... ]"
76.Re
77the product of all of its arguments.
78It returns 1 if there are no arguments.
79.No
80See the description of the function \fIadd\fP for details and restrictions to the
81automatic data type coercion.
82.Lf quotient "['n_arg1 ...]"
83.Lx / "['x_arg1 ...]"
84.Re
85the result of dividing the first argument by succeeding ones.
86.No
87If there are no arguments, 1 is returned.
88See the description of the function \fIadd\fP for details and restrictions
89of data type coercion.
90A divide by zero will cause a floating exception interrupt -- see
91the description of the
92.i signal
93function.
94.Lf *quo "'i_x 'i_y"
95.Re
96the integer part of i_x / i_y.
97.Lf Divide "'i_dividend 'i_divisor"
98.Re
99a list whose car is the quotient and whose cadr is the remainder of the
100division of i_dividend by i_divisor.
101.No
102this is restricted to integer division.
103.Lf Emuldiv "'x_fact1 'x_fact2 'x_addn 'x_divisor"
104.Re
105a list of the quotient and remainder of this operation:
106((x_fact1\ *\ x_fact2)\ +\ (sign\ extended)\ x_addn)\ /\ x_divisor.
107.No
108this is useful for creating a bignum arithmetic package in Lisp.
109.sh 2 predicates
110.Lf numberp "'g_arg"
111.Lf numbp "'g_arg"
112.Re
113t iff g_arg is a number (fixnum, flonum or bignum).
114.Lf fixp "'g_arg"
115.Re
116t iff g_arg is a fixnum or bignum.
117.Lf floatp "'g_arg"
118.Re
119t iff g_arg is a flonum.
120.Lf evenp "'x_arg"
121.Re
122t iff x_arg is even.
123.Lf oddp "'x_arg"
124.Re
125t iff x_arg is odd.
126.Lf zerop "'g_arg"
127.Re
128t iff g_arg is a number equal to 0.
129.Lf onep "'g_arg"
130.Re
131t iff g_arg is a number equal to 1.
132.Lf plusp "'n_arg"
133.Re
134t iff n_arg is greater than zero.
135.Lf minusp "'g_arg"
136.Re
137t iff g_arg is a negative number.
138.Lf greaterp "['n_arg1 ...]"
139.Lx > "'fx_arg1 'fx_arg2"
140.Lx >& "'x_arg1 'x_arg2"
141.Re
142t iff the arguments are in a strictly decreasing order.
143.No
144In functions
145.i greaterp
146and
147.i >
148the function
149.i difference
150is used to compare adjacent values.
151If any of the arguments are non-numbers, the error message will come
152from the
153.i difference
154function.
155The arguments to
156.i >
157must be fixnums or both flonums.
158The arguments to
159.i >&
160must both be fixnums.
161.Lf lessp "['n_arg1 ...]"
162.Lx < "'fx_arg1 'fx_arg2"
163.Lx <& "'x_arg1 'x_arg2"
164.Re
165t iff the arguments are in a strictly increasing order.
166.No
167In functions
168.i lessp
169and
170.i <
171the function \fIdifference\fP is used to compare adjacent values.
172If any of the arguments are non numbers, the error message will come
173from the \fIdifference\fP function.
174The arguments to
175.i <
176may be either fixnums or flonums but must be the same type.
177The arguments to
178.i <&
179must be fixnums.
180.Lf \(eq "'fx_arg1 'fx_arg2"
181.Lf \(eq& "'x_arg1 'x_arg2"
182.Re
183t iff the arguments have the same value.
184The arguments to \(eq must be the either both fixnums or both flonums.
185The arguments to \(eq& must be fixnums.
186.sh 2 Trignometric\ Functions
187.pp
188Some of these funtcions are taken from the host math library, and
189we take no further responsibility for their accuracy.
190.Lf cos "'fx_angle"
191.Re
192the (flonum) cosine of fx_angle (which is assumed to be in radians).
193.Lf sin "'fx_angle"
194.Re
195the sine of fx_angle (which is assumed to be in radians).
196.Lf acos "'fx_arg"
197.Re
198the (flonum) arc cosine of fx_arg in the range 0 to \(*p.
199.Lf asin "'fx_arg"
200.Re
201the (flonum) arc sine of fx_arg in the range \(mi\(*p/2 to \(*p/2.
202.Lf atan "'fx_arg1 'fx_arg2"
203.Re
204the (flonum) arc tangent of fx_arg1/fx_arg2 in the range -\(*p to \(*p.
205.sh 2 Bignum/Fixnum\ Manipulation
206.Lf haipart "bx_number x_bits"
207.Re
208a fixnum (or bignum) which contains
209the x_bits high bits of
210\fI(abs\ bx_number)\fP if x_bits is positive, otherwise
211it returns the \fI(abs\ x_bits)\fP low bits of \fI(abs\ bx_number)\fP.
212.Lf haulong "bx_number"
213.Re
214the number of significant bits in bx_number.
215.No
216the result is equal to the least integer greater to or equal to the
217base two logarithm of
218one plus the absolute value of bx_number.
219.Lf bignum-leftshift "bx_arg x_amount"
220.Re
221bx_arg shifted left by x_amount. If
222x_amount is negative, bx_arg will be shifted right by the magnitude of
223x_amount.
224.No
225If bx_arg is shifted right, it will be rounded to the nearest even number.
226.Lf sticky-bignum-leftshift "'bx_arg 'x_amount"
227.Re
228bx_arg shifted left by x_amount. If
229x_amount is negative, bx_arg will be shifted right by the magnitude of
230x_amount and rounded.
231.No
232sticky rounding is done this way: after shifting,
233the low order bit is changed to 1
234if any 1's were shifted off to the right.
235.sh 2 Bit\ Manipulation
236.Lf boole "'x_key 'x_v1 'x_v2 ..."
237.Re
238the result of the bitwise boolean operation as described in the following
239table.
240.No
241If there are more than 3 arguments, then evaluation proceeds left to
242right with each partial result becoming the new value of x_v1.
243That is,
244.br
245\ \ \ \ \ \fI(boole\ 'key\ 'v1\ 'v2\ 'v3)\ \(==\ (boole\ 'key\ (boole\ 'key\ 'v1\ 'v2)\ 'v3)\fP.
246.br
247In the following table, \(** represents bitwise and, \(pl represents
248bitwise or, \o'\(ci\(pl' represents bitwise xor and \(no represents
249bitwise negation and is the highest precedence operator.
250.ps 8
251.TS
252center box ;
253c s s s s s s s s
254c c c c c c c c c.
255(boole 'key 'x 'y)
256
257=
258key 0 1 2 3 4 5 6 7
259result 0 x \(** y \(no x \(** y y x \(** \(no y x x \o'\(ci\(pl' y x \(pl y
260
261common
262names and bitclear xor or
263
264_
265
266key 8 9 10 11 12 13 14 15
267result \(no (x \(pl y) \(no(x \o'\(ci\(pl' y) \(no x \(no x \(pl y \(no y x \(pl \(no y \(no x \(pl \(no y -1
268common
269names nor equiv implies nand
270.TE
271.ps 10
272.pp
273.Lf lsh "'x_val 'x_amt"
274.Re
275x_val shifted left by x_amt if x_amt is positive.
276If x_amt is negative, then
277.i lsh
278returns x_val shifted right by the magnitude if x_amt.
279.No
280This always returns a fixnum even for those numbers whose magnitude is
281so large that they would normally be represented as a bignum,
282i.e. shifter bits are lost.
283For more general bit shifters, see
284.i bignum-leftshift
285and
286.i sticky-bignum-leftshift.
287.Lf rot "'x_val 'x_amt"
288.Re
289x_val rotated left by x_amt if x_amt is positive.
290If x_amt is negative, then x_val is rotated right by the magnitude of x_amt.
291.sh 2 Other\ Functions
292.pp
293As noted above, some of the following functions are inherited from the
294host math library, with all their virtues and vices.
295.Lf abs 'n_arg
296.Lx absval 'n_arg
297.Re
298the absolute value of n_arg.
299.Lf exp "'fx_arg"
300.Re
301.i e
302raised to the fx_arg power (flonum) .
303.Lf expt "'n_base 'n_power"
304.Re
305n_base raised to the n_power power.
306.No
307if either of the arguments are flonums, the calculation will be done using
308.i log
309and
310.i exp .
311.Lf fact "'x_arg"
312.Re
313x_arg factorial. (fixnum or bignum)
314.Lf fix "'n_arg"
315.Re
316a fixnum as close as we can get to n_arg.
317.No
318\fIfix\fP will round down.
319Currently, if n_arg is a flonum larger
320than the size of a fixnum, this will fail.
321.Lf float "'n_arg"
322.Re
323a flonum as close as we can get to n_arg.
324.No
325if n_arg is a bignum larger than the maximum size of a flonum,
326then a floating exception will occur.
327.Lf log "'fx_arg"
328.Re
329the natural logarithm of fx_arg.
330.Lf max "'n_arg1 ... "
331.Re
332the maximum value in the list of arguments.
333.Lf min "'n_arg1 ... "
334.Re
335the minimum value in the list of arguments.
336.Lf mod "'i_dividend 'i_divisor"
337.Lx remainder "'i_dividend 'i_divisor"
338.Re
339the remainder when i_dividend is divided by i_divisor.
340.No
341The sign of the result will have the same sign as i_dividend.
342.Lf *mod "'x_dividend 'x_divisor"
343.Re
344the balanced representation of x_dividend modulo x_divisor.
345.No
346the range of the balanced representation is abs(x_divisor)/2 to
347(abs(x_divisor)/2) \(mi x_divisor + 1.
348.Lf random "['x_limit]"
349.Re
350a fixnum between 0 and x_limit \(mi 1 if x_limit is given.
351If x_limit is not given, any fixnum, positive or negative, might be
352returned.
353.Lf sqrt "'fx_arg"
354.Re
355the square root of fx_arg.