(* 2 3 4) ==> 24
(/ 10 5) ==> 2
(/ 10 5 2) ==> 1
(/ 2 3) ==> 2/3
(/ 4) ==> 1/4
If all the arguments are the same type of number (rational, floating point, or complex), the result will be the same type except in the case where the result of an operation on complex numbers with rational components yields a number with a zero imaginary part, in which case the result will be a rational. However, floating-point and complex numbers are
(+ 1 2.0) ==> 3.0
(/ 2 3.0) ==> 0.6666667
(+ #c(1 2) 3) ==> #c(4 2)
(+ #c(1 2) 3/2) ==> #c(5/2 2)
(+ #c(1 1) #c(2 -1)) ==> 3
Because /
doesn't truncate, Common Lisp provides four flavors of truncating and rounding for converting a real number (rational or floating point) to an integer: FLOOR
truncates toward negative infinity, returning the largest integer less than or equal to the argument. CEILING
truncates toward positive infinity, returning the smallest integer greater than or equal to the argument. TRUNCATE
truncates toward zero, making it equivalent to FLOOR
for positive arguments and to CEILING
for negative arguments. And ROUND
rounds to the nearest integer. If the argument is exactly halfway between two integers, it rounds to the nearest even integer.
Two related functions are MOD
and REM
, which return the modulus and remainder of a truncating division on real numbers. These two functions are related to the FLOOR
and TRUNCATE
functions as follows:
(+ (* (floor (/ x y)) y) (mod x y)) === x
(+ (* (truncate (/ x y)) y) (rem x y)) === x
Thus, for positive quotients they're equivalent, but for negative quotients they produce different results.[116]
The functions 1+
and 1-
provide a shorthand way to express adding and subtracting one from a number. Note that these are different from the macros INCF
and DECF
. 1+
and 1-
are just functions that return a new value, but INCF
and DECF
modify a place. The following equivalences show the relation between INCF
/DECF
, 1+
/1-
, and +
/-
:
(incf x) === (setf x (1+ x)) === (setf x (+ x 1))
(decf x) === (setf x (1- x)) === (setf x (- x 1))
(incf x 10) === (setf x (+ x 10))
(decf x 10) === (setf x (- x 10))
The function =
is the numeric equality predicate. It compares numbers by mathematical value, ignoring differences in type. Thus, =
will consider mathematically equivalent values of different types equivalent while the generic equality predicate EQL
would consider them inequivalent because of the difference in type. (The generic equality predicate EQUALP
, however, uses =
to compare numbers.) If it's called with more than two arguments, it returns true only if they all have the same value. Thus:
(= 1 1) ==> T
(= 10 20/2) ==> T
(= 1 1.0 #c(1.0 0.0) #c(1 0)) ==> T
The /=
function, conversely, returns true only if
(/= 1 1) ==> NIL
(/= 1 2) ==> T
(/= 1 2 3) ==> T
(/= 1 2 3 1) ==> NIL
(/= 1 2 3 1.0) ==> NIL
The functions <
, >
, <=
, and >=
order rationals and floating-point numbers (in other words, the real numbers.) Like =
and /=
, these functions can be called with more than two arguments, in which case each argument is compared to the argument to its right.
(< 2 3) ==> T
(> 2 3) ==> NIL
(> 3 2) ==> T
(< 2 3 4) ==> T
(< 2 3 3) ==> NIL
(<= 2 3 3) ==> T
(<= 2 3 3 4) ==> T
(<= 2 3 4 3) ==> NIL
To pick out the smallest or largest of several numbers, you can use the function MIN
or MAX
, which takes any number of real number arguments and returns the minimum or maximum value.
(max 10 11) ==> 11
(min -12 -10) ==> -12
(max -1 2 -3) ==> 2
Some other handy functions are ZEROP
, MINUSP
, and PLUSP
, which test whether a single real number is equal to, less than, or greater than zero. Two other predicates, EVENP
and ODDP
, test whether a single integer argument is even or odd. The