PSL Manual 7 February 1983 Arithmetic Functions
section 5.0 page 5.1
CHAPTER 5
CHAPTER 5
CHAPTER 5
NUMBERS AND ARITHMETIC FUNCTIONS
NUMBERS AND ARITHMETIC FUNCTIONS
NUMBERS AND ARITHMETIC FUNCTIONS
5.1. Big Integers . . . . . . . . . . . . . . . 5.1
5.2. Conversion Between Integers and Floats. . . . . . . 5.2
5.3. Arithmetic Functions. . . . . . . . . . . . . 5.2
5.4. Functions for Numeric Comparison. . . . . . . . . 5.5
5.5. Bit Operations. . . . . . . . . . . . . . . 5.7
5.6. Various Mathematical Functions . . . . . . . . . 5.8
______
Most of the arithmetic functions in PSL expect numbers as arguments. In
all cases an error occurs if the parameter to an arithmetic function is not
______
a number:
***** Non-numeric argument in arithmetic
Exceptions to the rule are noted.
The underlying machine arithmetic requires parameters to be either all
_______ _____
integers or all floats. If a function receives mixed types of arguments,
_______ _____
integers are converted to floats before arithmetic operations are
______ _______
performed. The range of numbers which can be represented by an integer is
_____
different than that represented by a float. Because of this difference, a
conversion is not always possible; an unsuccessful attempt to convert may
cause an error to be signalled.
The MATHLIB package contains some useful mathematical functions. See
Section 5.6 for documentation for these functions.
5.1. Big Integers
5.1. Big Integers
5.1. Big Integers
Loading the BIG module redefines the basic arithmetic operations,
including the logical operations, to permit arbitrary precision (or
"bignum") integer operations.
Note that fixnums which are present before loading BIG can cause
problems, because loading BIG restricts the legal range of fixnums.
5.2. Conversion Between Integers and Floats
5.2. Conversion Between Integers and Floats
5.2. Conversion Between Integers and Floats
The conversions mentioned above can be done explicitly by the following
functions. Other functions which alter types can be found in Section 4.3.
Arithmetic Functions 7 February 1983 PSL Manual
page 5.2 section 5.2
Fix
Fix _ ______ _______ ____
(Fix U:number): integer expr
_______
Returns the integer which corresponds to the truncated value of
_
U. The result of conversion must retain all significant portions
_ _ _______
of U. If U is an integer it is returned unchanged.
_____
_____
_____
[??? Note that unless big is loaded, a float with value
[??? Note that unless big is loaded, a float with value
[??? Note that unless big is loaded, a float with value
larger than 2**35-1 on the DEC-20 is converted into something
larger than 2**35-1 on the DEC-20 is converted into something
larger than 2**35-1 on the DEC-20 is converted into something
strange but without any error message. Note how truncation
strange but without any error message. Note how truncation
strange but without any error message. Note how truncation
works on negative numbers (always towards zero). ???]
works on negative numbers (always towards zero). ???]
works on negative numbers (always towards zero). ???]
(Fix 2.1) % returns 2
(Fix -2.1) % returns -2
Float
Float _ ______ _____ ____
(Float U:number): float expr
_____ _
The float corresponding to the value of the argument U is
_______
returned. Some of the least significant digits of an integer may
Float Float
Float Float _____
be lost due to the implementation of Float. Float of a float
______ _
returns the number unchanged. If U is too large to represent in
_____
float, an error occurs:
***** Argument to FLOAT is too large
_______
_______
_______
[??? Only if big is loaded can one make an integer of value
[??? Only if big is loaded can one make an integer of value
[??? Only if big is loaded can one make an integer of value
greater than 2**35-1, so without big you won't get this error
greater than 2**35-1, so without big you won't get this error
greater than 2**35-1, so without big you won't get this error
message. The largest representable float is
message. The largest representable float is
message. The largest representable float is
(2**62-1)*(2**65) on the DEC-20. ???]
(2**62-1)*(2**65) on the DEC-20. ???]
(2**62-1)*(2**65) on the DEC-20. ???]
5.3. Arithmetic Functions
5.3. Arithmetic Functions
5.3. Arithmetic Functions
The functions described below handle arithmetic operations. Please note
the remarks at the beginning of this Chapter regarding the mixing of
argument types.
Abs
Abs _ ______ ______ ____
(Abs U:number): number expr
Returns the absolute value of its argument.
Add1
Add1 _ ______ ______ ____
(Add1 U:number): number expr
_
Returns the value of U plus 1; the returned value is of the same
_ _______ _____
type as U (integer or float).
PSL Manual 7 February 1983 Arithmetic Functions
section 5.3 page 5.3
Decr
Decr _ ____ __ ______ ______ _____
(Decr U:form [Xi:number]): number macro
Part of the USEFUL package (LOAD USEFUL). With only one
argument, this is equivalent to
(SETF U (SUB1 U))
With multiple arguments, it is equivalent to
(SETF U (DIFFERENCE U (PLUS X1 ... Xn)))
1 lisp> (Load Useful)
NIL
2 lisp> (Setq Y '(1 5 7))
(1 5 7)
3 lisp> (Decr (Car Y))
0
4 lisp> Y
(0 5 7)
5 lisp> (Decr (Cadr Y) 3 4)
-2
6 lisp> Y
(0 -2 7)
Difference
Difference _ ______ _ ______ ______ ____
(Difference U:number V:number): number expr
_ _
The value of U - V is returned.
Divide
Divide _ ______ _ ______ ____ ____
(Divide U:number V:number): pair expr
____ ________ _________
The pair (quotient . remainder) is returned, as if the quotient
Quotient
Quotient
part was computed by the Quotient function and the remainder by
Remainder
Remainder
the Remainder function. An error occurs if division by zero is
attempted:
***** Attempt to divide by 0 in Divide
Expt
Expt _ ______ _ _______ ______ ____
(Expt U:number V:integer): number expr
_ _ _____ _ _______ _
Returns U raised to the V power. A float U to an integer power V
___ _ _____
does not have V changed to a float before exponentiation.
Incr
Incr _ ____ __ ______ ______ _____
(Incr U:form [Xi:number]): number macro
Part of the USEFUL package (LOAD USEFUL). With only one
argument, this is equivalent to
Arithmetic Functions 7 February 1983 PSL Manual
page 5.4 section 5.3
(SETF U (ADD1 U))
With multiple arguments it is equivalent to
(SETF U (PLUS U X1 ... Xn))
Minus
Minus _ ______ ______ ____
(Minus U:number): number expr
_
Returns -U.
Plus
Plus _ ______ ______ _____
(Plus [U:number]): number macro
Plus
Plus
Forms the sum of all its arguments. Plus may be called with only
Plus
Plus
one argument. In this case it returns its argument. If Plus is
called with no arguments, it returns zero.
Plus2
Plus2 _ ______ _ ______ ______ ____
(Plus2 U:number V:number): number expr
_ _
Returns the sum of U and V.
Quotient
Quotient _ ______ _ ______ ______ ____
(Quotient U:number V:number): number expr
Quotient
Quotient _ _
The Quotient of U divided by V is returned. Division of two
_______ _
positive or two negative integers is conventional. If both U and
_ _______
V are integers and exactly one of them is negative, the value
Quotient Abs
Quotient Abs _
returned is the negative truncation of the Quotient of Abs U and
Abs
Abs _ _____ _____
Abs V. If either argument is a float, a float is returned which
_____
is exact within the implemented precision of floats. An error
occurs if division by zero is attempted:
***** Attempt to divide by 0 in QUOTIENT
Recip
Recip _ ______ _____ ____
(Recip U:number): float expr
Recip
Recip _ _____
Recip converts U to a float if necessary, and then finds the
Quotient
Quotient
inverse using the function Quotient.
Remainder
Remainder _ ______ _ ______ ______ ____
(Remainder U:number V:number): number expr
_ _ _______ _______
If both U and V are integers the result is the integer remainder
_ _ _____
of U divided by V. If either parameter is a float, the result is
_ _ _ _ _____
the difference between U and V*(U/V), all in float (probably
______
0.0). If either number is negative the remainder is negative.
If both are positive or both are negative the remainder is
_
positive. An error occurs if V is zero:
PSL Manual 7 February 1983 Arithmetic Functions
section 5.3 page 5.5
***** Attempt to divide by 0 in REMAINDER
Remainder Mod
Remainder Mod
Note that the Remainder function differs from the Mod function in
Remainder
Remainder _ _
that Remainder returns a negative number when U is negative and V
is positive.
Sub1
Sub1 _ ______ ______ ____
(Sub1 U:number): number expr
_ _ _____
Returns the value of U minus 1. If U is a float, the value
_
returned is U minus 1.0.
Times
Times _ ______ ______ _____
(Times [U:number]): number macro
Times
Times
Returns the product of all its arguments. Times may be called
with only one argument. In this case it returns the value of its
Times
Times
argument. If Times is called with no arguments, it returns 1.
Times2
Times2 _ ______ _ ______ ______ ____
(Times2 U:number V:number): number expr
_ _
Returns the product of U and V.
5.4. Functions for Numeric Comparison
5.4. Functions for Numeric Comparison
5.4. Functions for Numeric Comparison
The following functions compare the values of their arguments. For
functions testing equality (or non-equality) see Section 4.2.1.
Geq
Geq _ ___ _ ___ _______ ____
(Geq U:any V:any): boolean expr
_ _
Returns T if U >= V, otherwise returns NIL. In RLISP, the symbol
">=" can be used.
GreaterP
GreaterP _ ______ _ ______ _______ ____
(GreaterP U:number V:number): boolean expr
_ _
Returns T if U is strictly greater than V, otherwise returns NIL.
In RLISP, the symbol ">" can be used.
Leq
Leq _ ______ _ ______ _______ ____
(Leq U:number V:number): boolean expr
_ _
Returns T if U <= V, otherwise returns NIL. In RLISP, the symbol
"<=" can be used.
Arithmetic Functions 7 February 1983 PSL Manual
page 5.6 section 5.4
LessP
LessP _ ______ _ ______ _______ ____
(LessP U:number V:number): boolean expr
_ _
Returns T if U is strictly less than V, otherwise returns NIL.
In RLISP, the symbol "<" can be used.
Max
Max _ ______ ______ _____
(Max [U:number]): number macro
_
Returns the largest of the values in U (numeric maximum). If two
or more values are the same, the first is returned.
Max2
Max2 _ ______ _ ______ ______ ____
(Max2 U:number V:number): number expr
_ _ _ _
Returns the larger of U and V. If U and V are of the same value
_ _ _
U is returned (U and V might be of different types).
Min
Min _ ______ ______ _____
(Min [U:number]): number macro
_
Returns the smallest (numeric minimum) of the values in U. If
two or more values are the same, the first of these is returned.
Min2
Min2 _ ______ _ ______ ______ ____
(Min2 U:number V:number): number expr
_ _
Returns the smaller of its arguments. If U and V are the same
_ _ _
value, U is returned (U and V might be of different types).
MinusP
MinusP _ ___ _______ ____
(MinusP U:any): boolean expr
_ ______ _ ______
Returns T if U is a number and less than 0. If U is not a number
______
or is a positive number, NIL is returned.
OneP
OneP _ ___ _______ ____
(OneP U:any): boolean expr
_ ______
Returns T if U is a number and has the value 1 or 1.0. Returns
NIL otherwise.
ZeroP
ZeroP _ ___ _______ ____
(ZeroP U:any): boolean expr
_ ______
Returns T if U is a number and has the value 0 or 0.0. Returns
NIL otherwise.
PSL Manual 7 February 1983 Arithmetic Functions
section 5.5 page 5.7
5.5. Bit Operations
5.5. Bit Operations
5.5. Bit Operations
The functions described in this section operate on the binary
_______
representation of the integers given as arguments. The returned value is
_______
an integer.
LAnd
LAnd _ _______ _ _______ _______ ____
(LAnd U:integer V:integer): integer expr
And
And
Bitwise or logical And. Each bit of the result is independently
determined from the corresponding bits of the operands according
to the following table.
_
U 0 0 1 1
_
V 0 1 0 1
Returned Value 0 0 0 1
LOr
LOr _ _______ _ _______ _______ ____
(LOr U:integer V:integer): integer expr
Or
Or
Bitwise or logical Or. Each bit of the result is independently
determined from corresponding bits of the operands according to
the following table.
_
U 0 0 1 1
_
V 0 1 0 1
Returned Value 0 1 1 1
LNot
LNot _ _______ _______ ____
(LNot U:integer): integer expr
Not
Not _ ______
Logical Not. Defined as (-U + 1) so that it works for bignums as
if they were 2's complement.
[??? need to clarify a bit more ???]
[??? need to clarify a bit more ???]
[??? need to clarify a bit more ???]
LXOr
LXOr _ _______ _ _______ _______ ____
(LXOr U:integer V:integer): integer expr
Or
Or
Bitwise or logical exclusive Or. Each bit of the result is
independently determined from the corresponding bits of the
operands according to the following table.
_
U 0 0 1 1
_
V 0 1 0 1
Returned Value 0 1 1 0
LShift
LShift _ _______ _ _______ _______ ____
(LShift N:integer K:integer): integer expr
_ _
Shifts N to the left by K bits. The effect is similar to
Arithmetic Functions 7 February 1983 PSL Manual
page 5.8 section 5.5
_
K
_
multiplying by 2 . It is an arithmetic shift. Negative values
_
are acceptable for K, and cause a right shift (in the usual
manner).
5.6. Various Mathematical Functions
5.6. Various Mathematical Functions
5.6. Various Mathematical Functions
The optionally loadable MATHLIB module defines several commonly used
mathematical functions. Some effort has been made to be compatible with
Common Lisp, but this implementation tends to support fewer features. The
examples used here should be taken with a grain of salt, since the
precision of the results will depend on the machine being used, and may
change in later implementations of the module.
Ceiling
Ceiling _ ______ _______ ____
(Ceiling X:number): integer expr
_______ _
Returns the smallest integer greater than or equal to X. For
example:
1 lisp> (ceiling 2.1)
3
2 lisp> (ceiling -2.1)
-2
Floor
Floor _ ______ _______ ____
(Floor X:number): integer expr
_
Returns the largest integer less than or equal to X. (Note that
Fix
Fix
this differs from the Fix function.)
1 lisp> (floor 2.1)
2
2 lisp> (floor -2.1)
-3
3 lisp> (fix -2.1)
-2
Round
Round _ ______ _______ ____
(Round X:number): integer expr
1
_
Returns the nearest integer to X.
_______________
1
Round
Round
The behavior of Round is ambiguous when its argument ends in ".5"--needs
more work.
PSL Manual 7 February 1983 Arithmetic Functions
section 5.6 page 5.9
TransferSign
TransferSign _ ______ ___ ______ ______ ____
(TransferSign S:number Val:number): number expr
abs
_ ___ abs ___ _
Transfers the sign of S to VAL by returning abs(VAL) if S >= 0,
abs sign
abs ___ sign
and -abs(VAL) otherwise. (The same as FORTRANs sign function.)
Mod
Mod _ _______ _ _______ _______ ____
(Mod M:integer N:integer): integer expr
remainder
_ _ remainder
Returns M modulo N. Unlike the remainder function, it returns a
_ _ _ _ _
positive number in the range 0..N-1 when N is positive, even if M
is negative.
1 lisp> (mod -7 5)
3
2 lisp> (remainder -7 5)
-2
[??? Allow to "number" arguments instead of just "integers"?
[??? Allow to "number" arguments instead of just "integers"?
[??? Allow to "number" arguments instead of just "integers"?
???]
???]
???]
DegreesToRadians
DegreesToRadians _ ______ ______ ____
(DegreesToRadians X:number): number expr
Returns an angle in radians given an angle in degrees.
1 lisp> (DegreesToRadians 180)
3.1415926
RadiansToDegrees
RadiansToDegrees _ ______ ______ ____
(RadiansToDegrees X:number): number expr
Returns an angle in degrees given an angle in radians.
1 lisp> (RadiansToDegrees 3.1415926)
180.0
RadiansToDMS
RadiansToDMS _ ______ ____ ____
(RadiansToDMS X:number): list expr
_ _______
Given an angle X in radians, returns a list of three integers
giving the angle in
(Degrees Minutes Seconds)
.
1 lisp> (RadiansToDMS 1.0)
(57 17 45)
Arithmetic Functions 7 February 1983 PSL Manual
page 5.10 section 5.6
DMStoRadians
DMStoRadians ____ ______ ____ ______ ____ ______ ______ ____
(DMStoRadians Degs:number Mins:number Secs:number): number expr
Returns an angle in radians, given three arguments representing
an angle in degrees minutes and seconds.
1 lisp> (DMStoRadians 57 17 45)
1.0000009
2 lisp> (DMStoRadians 180 0 0)
3.1415926
DegreesToDMS
DegreesToDMS _ ______ ____ ____
(DegreesToDMS X:number): list expr
_ _______
Given an angle X in degrees, returns a list of three integers
giving the angle in (Degrees Minutes Seconds).
DMStoDegrees
DMStoDegrees ____ ______ ____ ______ ____ ______ ______ ____
(DMStoDegrees Degs:number Mins:number Secs:number): number expr
Returns an angle in degrees, given three arguments representing
an angle in degrees minutes and seconds.
Sin
Sin _ ______ ______ ____
(Sin X:number): number expr
sine
sine _
Returns the sine of X, an angle in radians.
SinD
SinD _ ______ ______ ____
(SinD X:number): number expr
sine
sine _
Returns the sine of X, an angle in degrees.
Cos
Cos _ ______ ______ ____
(Cos X:number): number expr
cosine
cosine _
Returns the cosine of X, an angle in radians.
CosD
CosD _ ______ ______ ____
(CosD X:number): number expr
cosine
cosine _
Returns the cosine of X, an angle in degrees.
Tan
Tan _ ______ ______ ____
(Tan X:number): number expr
tangent
tangent _
Returns the tangent of X, an angle in radians.
TanD
TanD _ ______ ______ ____
(TanD X:number): number expr
tangent
tangent _
Returns the tangent of X, an angle in degrees.
PSL Manual 7 February 1983 Arithmetic Functions
section 5.6 page 5.11
Cot
Cot _ ______ ______ ____
(Cot X:number): number expr
cotangent
cotangent _
Returns the cotangent of X, an angle in radians.
CotD
CotD _ ______ ______ ____
(CotD X:number): number expr
cotangent
cotangent _
Returns the cotangent of X, an angle in degrees.
Sec
Sec _ ______ ______ ____
(Sec X:number): number expr
secant
secant _
Returns the secant of X, an angle in radians.
secant(X) = 1/cos(X)
SecD
SecD _ ______ ______ ____
(SecD X:number): number expr
secant
secant _
Returns the secant of X, an angle in degrees.
Csc
Csc _ ______ ______ ____
(Csc X:number): number expr
cosecant
cosecant _
Returns the cosecant of X, an angle in radians.
secant(X) = 1/sin(X)
CscD
CscD _ ______ ______ ____
(CscD X:number): number expr
cosecant
cosecant _
Returns the cosecant of X, an angle in degrees.
Asin
Asin _ ______ ______ ____
(Asin X:number): number expr
arc sine
arc sine _
Returns the arc sine, as an angle in radians, of X.
sin(asin(X)) = X
AsinD
AsinD _ ______ ______ ____
(AsinD X:number): number expr
arc sine
arc sine _
Returns the arc sine, as an angle in degrees, of X.
Arithmetic Functions 7 February 1983 PSL Manual
page 5.12 section 5.6
Acos
Acos _ ______ ______ ____
(Acos X:number): number expr
arc cosine
arc cosine _
Returns the arc cosine, as an angle in radians, of X.
cos(acos(X)) = X
AcosD
AcosD _ ______ ______ ____
(AcosD X:number): number expr
arc cosine
arc cosine _
Returns the arc cosine, as an angle in degrees, of X.
Atan
Atan _ ______ ______ ____
(Atan X:number): number expr
arc tangent
arc tangent _
Returns the arc tangent, as an angle in radians, of X.
tan(atan(X)) = X
AtanD
AtanD _ ______ ______ ____
(AtanD X:number): number expr
arc tangent
arc tangent _
Returns the arc tangent, as an angle in degrees, of X.
Atan2
Atan2 _ ______ _ ______ ______ ____
(Atan2 Y:number X:number): number expr
Returns an angle in radians corresponding to the angle between
_ _ _
the X axis and the vector (X,Y). (Note that Y is the first
argument.)
1 lisp> (atan2 0 -1)
3.1415927
Atan2D
Atan2D _ ______ _ ______ ______ ____
(Atan2D Y:number X:number): number expr
Returns an angle in degrees corresponding to the angle between
_ _
the X axis and the vector (X,Y).
1 lisp> (atan2D -1 1)
315.0
Acot
Acot _ ______ ______ ____
(Acot X:number): number expr
arc cotangent
arc cotangent _
Returns the arc cotangent, as an angle in radians, of X.
cot(acot(X)) = X
PSL Manual 7 February 1983 Arithmetic Functions
section 5.6 page 5.13
AcotD
AcotD _ ______ ______ ____
(AcotD X:number): number expr
arc cotangent
arc cotangent _
Returns the arc cotangent, as an angle in degrees, of X.
Asec
Asec _ ______ ______ ____
(Asec X:number): number expr
arc secant
arc secant _
Returns the arc secant, as an angle in radians, of X.
sec(asec(X)) = X
AsecD
AsecD _ ______ ______ ____
(AsecD X:number): number expr
arc secant
arc secant _
Returns the arc secant, as an angle in degrees, of X.
Acsc
Acsc _ ______ ______ ____
(Acsc X:number): number expr
arc cosecant
arc cosecant _
Returns the arc cosecant, as an angle in radians, of X.
csc(acsc(X)) = X
AcscD
AcscD _ ______ ______ ____
(AcscD X:number): number expr
arc cosecant
arc cosecant _
Returns the arc cosecant, as an angle in degrees, of X.
Sqrt
Sqrt _ ______ ______ ____
(Sqrt X:number): number expr
_
Returns the square root of X.
Exp
Exp _ ______ ______ ____
(Exp X:number): number expr
_
X
_ _
Returns the exponential of X, i.e. e .
Log
Log _ ______ ______ ____
(Log X:number): number expr
_ _
Returns the natural (base e) logarithm of X.
log(exp(X)) = X
Arithmetic Functions 7 February 1983 PSL Manual
page 5.14 section 5.6
Log2
Log2 _ ______ ______ ____
(Log2 X:number): number expr
_
Returns the base two logarithm of X.
Log10
Log10 _ ______ ______ ____
(Log10 X:number): number expr
_
Returns the base ten logarithm of X.
Random
Random _ _______ _______ ____
(Random N:integer): integer expr
Returns a pseudo-random number uniformly selected from the range
_
0..N-1.
The random number generator uses a linear congruential method.
To get a reproducible sequence of random numbers you should
assign one (or some other small number) to the FLUID variable
RANDOMSEED.
__________ ______
RANDOMSEED [Initially: set from time] global
Factorial
Factorial _ _______ _______ ____
(Factorial N:integer): integer expr
_
Returns the factorial of N.
factorial(0) = 1
factorial(N) = N*factorial(N-1)