BSD 3 development
[unix-history] / usr / doc / lisp / ch3.n
CommitLineData
6f721e2c
JF
1.Lc Arithmetic\ Functions 3
2.\".ch 3
3.sh 2 "" \n(ch 1
4This chapter describes
5.Fr "'s"
6functions for doing arithmetic.
7Often the same function is know by many names, such as
8.i add
9which is also
10.i plus ,
11.i sum ,
12and \(pl.
13This is due to our desire to be compatible with other Lisps.
14The
15.Fr
16user is advised to avoid using functions with names
17such as \(pl and \(** unless
18their arguments are fixnums.
19In the future, the
20.Fr
21compiler may be able to take advantage of the fact that their
22arguments are fixnums.
23.sp 1v
24.Lf abs 'n_arg
25.Re
26the absolute value of n_arg.
27.Lf absval "'n_arg"
28.Eq
29abs.
30.Lf add "['n_arg1 ...]"
31.Re
32the sum of the arguments. If no arguments are given, 0 is returned.
33.No
34if the size of the partial sum exceeds the limit of a fixnum, the
35partial sum will be converted to a bignum.
36If any of the arguments are flonums, the partial sum will be
37converted to a flonum when that argument is processed and the
38result will thus be a flonum.
39Currently, if in the process of doing the
40addition a bignum must be converted into
41a flonum an error message will result.
42.Lf add1 'n-arg
43.Re
44n_arg plus 1.
45.bp
46.Lf acos "'fx_arg"
47.Re
48the arc cosine of fx_arg in the range 0 to \(*pi.
49.Lf asin "'fx_arg"
50.Re
51the arc sine of fx_arg in the range \(mi\(*p/2 to \(*p/2.
52.Lf atan "'fx_arg1 'fx_arg2"
53.Re
54the arc tangent of fx_arg1/fx_arg2 in the range -\(*p to \(*p.
55.Lf boole "'x_key 'x_v1 'x_v2 ..."
56.Re
57the result of the bitwise boolean operation as described in the following
58table.
59.No
60If there are more than 3 arguments, then evaluation proceeds left to
61right with each partial result becoming the new value of x_v1.
62That is,
63.br
64\ \ \ \ \ (boole\ 'key\ 'v1\ 'v2\ 'v3)\ \(==\ (boole\ 'key\ (boole\ 'key\ 'v1\ 'v2)\ 'v3).
65.br
66In the following table, \(** represents bitwise and, \(pl represents
67bitwise or, \o'\(ci\(pl' represents bitwise xor and \(no represents
68bitwise negation and is the highest precedence operator.
69.TS
70center box ;
71c s s s s s s s s
72c c c c c c c c c.
73(boole 'key 'x 'y)
74
75=
76key 0 1 2 3 4 5 6 7
77result 0 x \(** y \(no x \(** y y x \(** \(no y x x \o'\(ci\(pl' y x \(pl y
78
79_
80
81key 8 9 10 11 12 13 14 15
82result \(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
83.TE
84.Lf cos "'fx_angle"
85.Re
86the cosine of fx_angle (which is assumed to be in radians).
87.Lf diff "['n_arg1 ... ]"
88.Re
89the result of subtracting from n_arg1 all subsequent arguments.
90If no arguments are given, 0 is returned.
91.No
92See the description of add for details on data type conversions and
93restrictions.
94.Lf difference "['n_arg1 ...]"
95.Eq
96diff.
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_divident 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.\".pg
110.Lf eq "'g_arg1 'g_arg2"
111.Re
112t if g_arg1 and g_arg2 are the exact same lisp object (i.e. they are
113stored in the same place in memory).
114.No
115This is a very restricted type of equality.
116In general numbers are not
117.i eq
118to numbers with the same value, although usually small fixnums with the
119same value are
120.i eq .
121.Lf equal "'g_arg1 'g_arg2"
122.Re t iff g_arg1 and g_arg2 are the same type and their components are
123equal.
124.No
125in the case of numbers, they will be equal if their values are numerically
126equal. If g_arg1 and g_arg2 are both lists then
127.i equal
128is called recursively on their car's and cdr's.
129Two atoms are
130.i equal
131iff they are the same atom (i.e. they are
132.i eq .)
133This is almost equivalent to saying that two atoms are
134.i equal
135if their print names
136are the same.
137.Lf exp "'fx_arg"
138.Re
139.i e
140raised to the fx_arg power.
141.Lf expt "'n_base 'n_power"
142.Re
143n_base raised to the i_power power.
144.No
145if either of the arguments are flonums, the calculation will be done using
146.i log
147and
148.i exp .
149.Lf fact "'x_arg"
150.Re
151x_arg factorial.
152.Lf fix "'n_arg"
153.Re
154a fixnum as close as we can get to n_arg.
155.No
156fix will round down.
157Currently, if n_arg is a flonum larger
158than the size of a fixnum, this will fail.
159.Lf fixp "'g_arg"
160.Re
161t iff g_arg is a fixnum or bignum.
162.Lf floatp "'g_arg"
163.Re
164t iff g_arg is a flonum.
165.\".pg
166.Lf greaterp "['n_arg1 ...]"
167.Re
168t iff the arguments are in a strictly decreasing order.
169.No
170the function difference is used to compare adjacent values.
171If any of the arguments are non numbers, the error message will come
172from the difference function.
173.Lf lessp "['n_arg1 ...]"
174.Re
175t iff the arguments are in a strictly increasing order.
176.No
177the function difference is used to compare adjacent values.
178If any of the arguments are non numbers, the error message will come
179from the difference function.
180.Lf log "'fx_arg"
181.Re
182the natural logarithm of fx_arg.
183.Lf max "'n_arg1 ... "
184.Re
185the maximum value in the list of arguments.
186.Lf min "'n_arg1 ... "
187.Re
188the minimum value in the list of arguments.
189.Lf minus "'n_arg"
190.Re
191zero minus n_arg.
192.Lf minusp "'g_arg"
193.Re
194t iff g_arg is a negative number.
195.Lf mod "'i_dividend 'i_divisor"
196.Re
197the remainder when i_dividend is divided by i_divisor.
198.Lf numberp "'g_arg"
199.Re
200t iff g_arg is a number (fixnum, flonum or bignum).
201.Lf numbp "'g_arg"
202.Eq
203numberp.
204.\".pg
205.Lf onep "'g_arg"
206.Re
207t iff g_arg is a number equal to 1.
208.Lf plusp "'n_arg"
209.Re
210t iff n_arg is greater than zero.
211.Lf product "['n_arg1 ... ]"
212.Re
213the product of all of its arguments.
214It returns 1 if there are no arguments.
215.No
216See the description of the function add for details and restrictions to the
217automatic data type coersion.
218.Lf quotient "['n_arg1 ...]"
219.Re
220the result of dividing the first argument by succeeding ones.
221.No
222If there are no arguments, 1 is returned.
223See the description of the function add for details and restrictions
224of data type coersion..
225A divide by zero will cause a floating exception interrupt -- see the section
226on Interupts.
227.Lf remainder "'i_dividend 'i_divisor"
228.Eq
229mod.
230.Lf sin "'fx_angle"
231.Re
232the sine of fx_angle (which is assumed to be in radians).
233.Lf sqrt "'fx_arg"
234.Re
235the square root of fx_arg.
236.Lf sub1 "'n_arg"
237.Re
238n_arg minus 1.
239.Lf sum "['n_arg1 ...]"
240.Eq
241add and plus.
242.Lf times "['n_arg1 ... ]"
243.Eq
244product.
245.Lf zerop "'g_arg"
246.Re
247t iff g_arg is a number equal to 0.
248.in 0
249.nr si 0 \" kill section indent so what follows will look nice
250.sh 2
251These functions are restricted to fixnum arguments in Maclisp.
252One should avoid using them unless their arguments are known
253to be fixnums since
254.Fr
255may adopt this convention in the future.
256.\".pg
257.Lf 1+ "'n_arg"
258.Eq
259add1.
260.Lf 1\(mi "'n_arg"
261.Eq
262sub1.
263.Lf \(pl "'n_arg"
264.Eq
265add.
266.Lf \(** "'n_arg"
267.Eq
268times.
269.Lf \(mi "'n_arg"
270.Eq
271difference.
272.Lf / "'n_arg1 'n_arg2"
273.Eq
274quotient
275.Lf < "'n_arg1 'n_arg2"
276.Eq
277lessp.
278.Lf \(eq "'g_arg1 'g_arg2"
279.Eq
280equal.
281.Lf > "'n_arg1 'n_arg2"
282.Eq
283greaterp.