next up previous
Next: 4.3 Programming with Arithmetic Up: 4. ARITHMETIC IN LISP Previous: 4.1 Reading and Printing

4.2 Arithmetic Functions and Predicates

We shall now list all of the arithmetic functions in the System. They must be given numbers as arguments; otherwise an error condition will result. The arguments may be any type of number. A function may be given some fixed-point arguments and some floating-point arguments at the same time.

If all of the arguments for a function are fixed-point numbers, then the value will be a fixed-point number. If at least one argument is a floating-point number, then the value of the function will be a floating-point number.

plus ${[}x_{1}; \ldots ; x_{n}{]}$ is a function of any number of arguments whose value is the algebraic sum of the arguments.

difference[x;y] has for its value the algebraic difference of its arguments.

minus[x] has for its value -x.

times ${[}x_{1}; \ldots ; x_{n}{]}$ is a function of any number of arguments, whose value is the product (with correct sign) of its arguments.

add1[x] has x+1 for its value. The value is fixed-point or floating-point, depending on the argument.

sub1[x] has x-1 for its value. The v-alue is fixed-point or floating-point, depending on the argument.

max ${[}x_{1}; \ldots ; x_{n}{]}$ chooses the largest of its arguments for its value. Note that max[3;2.0] = 3.0.

min ${[}x_{1}; \ldots ; x_{n}{]}$ chooses the smallest of its arguments for its value.

recip[x] computes 1/x. The reciprocal of any fixed point number is defined as zero.

quotient[x;y] computes the quotient of its arguments. For fixed-point arguments, the value is the number theoretic quotient. A divide check or floating-point trap will result in a LISP error.

remainder[x;y] computes the number theoretic remainder for fixed-point numbers, and the floating-point residue for floating-point arguments.

divide[x;y] = cons[quotient[x;y], cons[remainder[x;y];NIL]]

expt[x;y] = xy. If both x and y are fixed-point numbers, this is computed by iterative multiplication. Otherwise the power is computed by using logarithms. The first argument cannot be negative.

We shall now list all of the arithmetic predicates in the System. They may have fixed-point and floating-point arguments mixed freely. The value of a predicate is *T* or NIL.

lessp[x;y] is true if $x < y$, and false otherwise.

greaterp[x;y] is true if $x > y$.

zerop[x] is true if $x=0$, or if $\vert x\vert\leqslant 3x10^{-6}$.

onep[x] is true if $\vert x-1\vert \leqslant 3x10^{-6}$.

minusp[x] is true if x is negative. "-0" is negative.

numberp[x] is true if x is a number (fixed-point or floating-point).

fixp[x] is true only if x is a fixed-point number. If x is not a number at all, an error will result.

floatp[x] is similar to fixp[x] but for floating-point numbers.

equal[x;y] works on any arguments including S-expressions incorporating numbers inside them. Its value is true if the arguments are identical. Floating-point numbers must satisfy $\vert x-y\vert < 3x10^{-6}$.

The logical functions operate on 36-bit words. The only acceptable arguments are fixed-point numbers. These may be read in as octal or decimal integers, or they may be the result of a previous computation.

logand ${[}x_{1}; \ldots ; x_{n}{]}$ performs a logical AND on its arguments.

logxor ${[}x_{1}; \ldots ; x_{n}{]}$ performs an exclusive OR

$(0 \lor 0 = 0, 1 \lor 0 = 0 \lor 1 = 1, 1 \lor 1 = 0)$

leftshift ${[}x,n{]} = x X 2^{n}$. The first argument is shifted left by the number of bits specified by the second argument. If the second argument is negative, the first argument will be shifted right.


next up previous
Next: 4.3 Programming with Arithmetic Up: 4. ARITHMETIC IN LISP Previous: 4.1 Reading and Printing