.\" Copyright (c) 1980 The Regents of the University of California.
.\" %sccs.include.redist.roff%
.\" @(#)ch3.n 6.3 (Berkeley) %G%
." $Header: ch3.n,v 1.3 83/06/21 13:00:48 sklower Exp $
.Lc Arithmetic\ Functions 3
functions for doing arithmetic.
Often the same function is known by many names.
This is caused by our desire to be compatible with other Lisps.
user should avoid using functions with names
such as \(pl and \(** unless
their arguments are fixnums.
The Lisp compiler takes advantage of these implicit declarations.
An attempt to divide or to generate a floating
point result outside of the range of
will cause a floating exception signal
from the UNIX operating system.
The user can catch and process this interrupt if desired (see the
.sh 2 Simple\ Arithmetic\ Functions \n(ch 1
the sum of the arguments. If no arguments are given, 0 is returned.
if the size of the partial sum exceeds the limit of a fixnum, the
partial sum will be converted to a bignum.
If any of the arguments are flonums, the partial sum will be
converted to a flonum when that argument is processed and the
result will thus be a flonum.
Currently, if in the process of doing the
addition a bignum must be converted into
a flonum an error message will result.
.Lf diff "['n_arg1 ... ]"
.Lx difference "['n_arg1 ... ]"
.Lx \(mi "['x_arg1 ... ]"
the result of subtracting from n_arg1 all subsequent arguments.
If no arguments are given, 0 is returned.
See the description of add for details on data type conversions and
.Lf product "['n_arg1 ... ]"
.Lx times "['n_arg1 ... ]"
.Lx \(** "['x_arg1 ... ]"
the product of all of its arguments.
It returns 1 if there are no arguments.
See the description of the function \fIadd\fP for details and restrictions to the
automatic data type coercion.
.Lf quotient "['n_arg1 ...]"
the result of dividing the first argument by succeeding ones.
If there are no arguments, 1 is returned.
See the description of the function \fIadd\fP for details and restrictions
A divide by zero will cause a floating exception interrupt -- see
the integer part of i_x / i_y.
.Lf Divide "'i_dividend 'i_divisor"
a list whose car is the quotient and whose cadr is the remainder of the
division of i_dividend by i_divisor.
this is restricted to integer division.
.Lf Emuldiv "'x_fact1 'x_fact2 'x_addn 'x_divisor"
a list of the quotient and remainder of this operation:
((x_fact1\ *\ x_fact2)\ +\ (sign\ extended)\ x_addn)\ /\ x_divisor.
this is useful for creating a bignum arithmetic package in Lisp.
t iff g_arg is a number (fixnum, flonum or bignum).
t iff g_arg is a fixnum or bignum.
t iff g_arg is a number equal to 0.
t iff g_arg is a number equal to 1.
t iff n_arg is greater than zero.
t iff g_arg is a negative number.
.Lf greaterp "['n_arg1 ...]"
.Lx > "'fx_arg1 'fx_arg2"
t iff the arguments are in a strictly decreasing order.
is used to compare adjacent values.
If any of the arguments are non-numbers, the error message will come
must be fixnums or both flonums.
.Lf lessp "['n_arg1 ...]"
.Lx < "'fx_arg1 'fx_arg2"
t iff the arguments are in a strictly increasing order.
the function \fIdifference\fP is used to compare adjacent values.
If any of the arguments are non numbers, the error message will come
from the \fIdifference\fP function.
may be either fixnums or flonums but must be the same type.
.Lf \(eq "'fx_arg1 'fx_arg2"
.Lf \(eq& "'x_arg1 'x_arg2"
t iff the arguments have the same value.
The arguments to \(eq must be the either both fixnums or both flonums.
The arguments to \(eq& must be fixnums.
.sh 2 Trignometric\ Functions
Some of these funtcions are taken from the host math library, and
we take no further responsibility for their accuracy.
the (flonum) cosine of fx_angle (which is assumed to be in radians).
the sine of fx_angle (which is assumed to be in radians).
the (flonum) arc cosine of fx_arg in the range 0 to \(*p.
the (flonum) arc sine of fx_arg in the range \(mi\(*p/2 to \(*p/2.
.Lf atan "'fx_arg1 'fx_arg2"
the (flonum) arc tangent of fx_arg1/fx_arg2 in the range -\(*p to \(*p.
.sh 2 Bignum/Fixnum\ Manipulation
.Lf haipart "bx_number x_bits"
a fixnum (or bignum) which contains
\fI(abs\ bx_number)\fP if x_bits is positive, otherwise
it returns the \fI(abs\ x_bits)\fP low bits of \fI(abs\ bx_number)\fP.
the number of significant bits in bx_number.
the result is equal to the least integer greater to or equal to the
one plus the absolute value of bx_number.
.Lf bignum-leftshift "bx_arg x_amount"
bx_arg shifted left by x_amount. If
x_amount is negative, bx_arg will be shifted right by the magnitude of
If bx_arg is shifted right, it will be rounded to the nearest even number.
.Lf sticky-bignum-leftshift "'bx_arg 'x_amount"
bx_arg shifted left by x_amount. If
x_amount is negative, bx_arg will be shifted right by the magnitude of
sticky rounding is done this way: after shifting,
the low order bit is changed to 1
if any 1's were shifted off to the right.
.Lf boole "'x_key 'x_v1 'x_v2 ..."
the result of the bitwise boolean operation as described in the following
If there are more than 3 arguments, then evaluation proceeds left to
right with each partial result becoming the new value of x_v1.
\ \ \ \ \ \fI(boole\ 'key\ 'v1\ 'v2\ 'v3)\ \(==\ (boole\ 'key\ (boole\ 'key\ 'v1\ 'v2)\ 'v3)\fP.
In the following table, \(** represents bitwise and, \(pl represents
bitwise or, \o'\(ci\(pl' represents bitwise xor and \(no represents
bitwise negation and is the highest precedence operator.
result 0 x \(** y \(no x \(** y y x \(** \(no y x x \o'\(ci\(pl' y x \(pl y
names and bitclear xor or
key 8 9 10 11 12 13 14 15
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
names nor equiv implies nand
x_val shifted left by x_amt if x_amt is positive.
If x_amt is negative, then
returns x_val shifted right by the magnitude if x_amt.
This always returns a fixnum even for those numbers whose magnitude is
so large that they would normally be represented as a bignum,
i.e. shifter bits are lost.
For more general bit shifters, see
.i sticky-bignum-leftshift.
x_val rotated left by x_amt if x_amt is positive.
If x_amt is negative, then x_val is rotated right by the magnitude of x_amt.
As noted above, some of the following functions are inherited from the
host math library, with all their virtues and vices.
the absolute value of n_arg.
raised to the fx_arg power (flonum) .
.Lf expt "'n_base 'n_power"
n_base raised to the n_power power.
if either of the arguments are flonums, the calculation will be done using
x_arg factorial. (fixnum or bignum)
a fixnum as close as we can get to n_arg.
\fIfix\fP will round down.
Currently, if n_arg is a flonum larger
than the size of a fixnum, this will fail.
a flonum as close as we can get to n_arg.
if n_arg is a bignum larger than the maximum size of a flonum,
then a floating exception will occur.
the natural logarithm of fx_arg.
the maximum value in the list of arguments.
the minimum value in the list of arguments.
.Lf mod "'i_dividend 'i_divisor"
.Lx remainder "'i_dividend 'i_divisor"
the remainder when i_dividend is divided by i_divisor.
The sign of the result will have the same sign as i_dividend.
.Lf *mod "'x_dividend 'x_divisor"
the balanced representation of x_dividend modulo x_divisor.
the range of the balanced representation is abs(x_divisor)/2 to
(abs(x_divisor)/2) \(mi x_divisor + 1.
a fixnum between 0 and x_limit \(mi 1 if x_limit is given.
If x_limit is not given, any fixnum, positive or negative, might be
the square root of fx_arg.