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