Next: Rounding Operations, Previous: Numeric Conversions, Up: Numbers [Contents][Index]
SXEmacs Lisp provides the traditional four arithmetic operations: addition, subtraction, multiplication, and division. Remainder and modulus functions supplement the division functions. The functions to add or subtract 1 are provided because they are traditional in Lisp and commonly used.
All of these functions except %
return a floating point value
if any argument is floating.
It is important to note that in SXEmacs Lisp, arithmetic functions
do not check for overflow. Thus (1+ 134217727)
may evaluate to
-134217728, depending on your hardware.
This function returns number plus one. number may be a number, character or marker. Markers and characters are converted to integers.
For example,
(setq foo 4) ⇒ 4 (1+ foo) ⇒ 5
This function is not analogous to the C operator ++
—it does not
increment a variable. It just computes a sum. Thus, if we continue,
foo ⇒ 4
If you want to increment the variable, you must use setq
,
like this:
(setq foo (1+ foo)) ⇒ 5
Now that the cl
package is always available from lisp code, a
more convenient and natural way to increment a variable is
(incf foo)
.
This function returns number minus one. number may be a number, character or marker. Markers and characters are converted to integers.
This returns the absolute value of number.
This function adds its arguments together. When given no arguments,
+
returns 0.
If any of the arguments are characters or markers, they are first converted to integers.
(+) ⇒ 0 (+ 1) ⇒ 1 (+ 1 2 3 4) ⇒ 10
The -
function serves two purposes: negation and subtraction.
When -
has a single argument, the value is the negative of the
argument. When there are multiple arguments, -
subtracts each of
the other-numbers from number, cumulatively. If there are
no arguments, an error is signaled.
If any of the arguments are characters or markers, they are first converted to integers.
(- 10 1 2 3 4) ⇒ 0 (- 10) ⇒ -10 (-) ⇒ 0
This function multiplies its arguments together, and returns the
product. When given no arguments, *
returns 1.
If any of the arguments are characters or markers, they are first converted to integers.
(*) ⇒ 1 (* 1) ⇒ 1 (* 1 2 3 4) ⇒ 24
The /
function serves two purposes: inversion and division. When
/
has a single argument, the value is the inverse of the
argument. When there are multiple arguments, /
divides
dividend by each of the divisors, cumulatively, returning
the quotient. If there are no arguments, an error is signaled.
If none of the arguments are floats, then the result is an integer.
This means the result has to be rounded. On most machines, the result
is rounded towards zero after each division, but some machines may round
differently with negative arguments. This is because the Lisp function
/
is implemented using the C division operator, which also
permits machine-dependent rounding. As a practical matter, all known
machines round in the standard fashion.
If any of the arguments are characters or markers, they are first converted to integers.
If you divide by 0, an arith-error
error is signaled.
(See Errors.)
(/ 6 2) ⇒ 3
(/ 5 2) ⇒ 2 (/ 25 3 2) ⇒ 4 (/ 3.0) ⇒ 0.3333333333333333 (/ -17 6) ⇒ -2
The result of (/ -17 6)
could in principle be -3 on some
machines.
This function returns the integer remainder after division of dividend by divisor. The arguments must be integers or markers.
For negative arguments, the remainder is in principle machine-dependent since the quotient is; but in practice, all known machines behave alike.
An arith-error
results if divisor is 0.
(% 9 4) ⇒ 1 (% -9 4) ⇒ -1 (% 9 -4) ⇒ 1 (% -9 -4) ⇒ -1
For any two integers dividend and divisor,
(+ (% dividend divisor) (* (/ dividend divisor) divisor))
always equals dividend.
This function returns the value of dividend modulo divisor; in other words, the remainder after division of dividend by divisor, but with the same sign as divisor. The arguments must be numbers or markers.
Unlike %
, mod
returns a well-defined result for negative
arguments. It also permits floating point arguments; it rounds the
quotient downward (towards minus infinity) to an integer, and uses that
quotient to compute the remainder.
An arith-error
results if divisor is 0.
(mod 9 4) ⇒ 1
(mod -9 4) ⇒ 3
(mod 9 -4) ⇒ -3
(mod -9 -4) ⇒ -1
(mod 5.5 2.5) ⇒ .5
For any two numbers dividend and divisor,
(+ (mod dividend divisor) (* (floor dividend divisor) divisor))
always equals dividend, subject to rounding error if either
argument is floating point. For floor
, see Numeric Conversions.
Next: Rounding Operations, Previous: Numeric Conversions, Up: Numbers [Contents][Index]