[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
XEmacs supports two to five numeric data types. Fixnums and floating point numbers are always supported. As a buildtime option, bignums, ratios, and bigfloats may be enabled on some platforms.
Fixnums (called just integers in GNU Emacs and older versions of XEmacs) are whole numbers such as 3, 0, #b0111, #xFEED, #o744. Their values are exact, and their range is limited. The number prefixes ‘#b’, ‘#o’, and ‘#x’ are supported to represent numbers in binary, octal, and hexadecimal notation (or radix). Floating point numbers are numbers with fractional parts, such as 4.5, 0.0, or 2.71828. They can also be expressed in exponential notation: 1.5e2 equals 150; in this example, ‘e2’ stands for ten to the second power, and is multiplied by 1.5. Floating point values are not exact; they have a fixed, limited amount of precision.
Bignums are arbitrary precision integers. When supported, XEmacs can handle any integral calculations you have enough virtual memory to store. (More precisely, on current architectures the representation allows integers whose storage would exhaust the address space.) They are notated in the same way as other integers (fixnums). XEmacs automatically converts results of computations from fixnum to bignum, and back, depending on the storage required to represent the number. Thus use of bignums are entirely transparent to the user, except for a few special applications that expect overflows. Ratios are rational numbers with arbitrary precision. They are notated in the usual way with the solidus, for example 5/3 or 22/7.
Bigfloats are floating point numbers with arbitrary precision, which may be specified by the user (and may be different for different bigfloats at the same time). Unlike integers, which are always infinitely precise if they can be represented, floating point numbers are inherently imprecise. This means that choice of precision can be a very delicate issue. XEmacs automatically converts from float to bigfloat when floats and bigfloats are mixed in an expression, but a bigfloat will never be converted to a float unless the user explicitly coerces the value. Nor will the result of a float operation be converted to bigfloat, except for “contagion” from another operand that is already a bigfloat. However, when bigfloats of differing precision are mixed, the result will always have the larger precision. The exact rules are more carefully explained elsewhere (see section Canonicalization and Contagion).
Common Lisp terminology and historical Emacs terminology conflict here, to an extent. We attempt to use “fixnum” and “integer” consistently, but older XEmacs and GNU Emacs code and documentation use the latter to mean the former. “Float” is used in Emacs documentation to mean “fixed precision floating point number”, and the Common Lisp distinctions among shortfloats, longfloats, etc., and bigfloats (which are not standardized in Common Lisp) are not reflected in XEmacs terminology. We’re working on this, but volunteers to fix it in the XEmacs manuals would be heartily welcomed.
9.1 Integer Basics  Representation and range of integers.  
9.2 Rational Basics  Representation and range of rational numbers.  
9.3 Floating Point Basics  Representation and range of floating point.  
9.4 The Bignum Extension  Arbitrary precision integers, ratios, and floats.  
9.5 Type Predicates for Numbers  Testing for numbers.  
9.6 Comparison of Numbers  Equality and inequality predicates.  
9.7 Numeric Conversions  Converting float to integer and vice versa.  
9.8 Arithmetic Operations  How to add, subtract, multiply and divide.  
9.9 Rounding Operations  Explicitly rounding floating point numbers.  
9.10 Bitwise Operations on Integers  Logical and, or, not, shifting.  
9.11 Standard Mathematical Functions  Trig, exponential and logarithmic functions.  
9.12 Random Numbers  Obtaining random integers, predictable or not. 
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
The range of values for an integer depends on the machine. If a multipleprecision arithmetic library is available on your platform, support for bignums, that is, integers with arbitrary precision, may be compiled in to your XEmacs. The rest of this section assumes that the bignum extension is not available. The bignum extension and the uservisible differences in normal integer arithmetic are discussed in a separate section The Bignum Extension.
The minimum range is 1073741824 to 1073741823 (31 bits; i.e., to but some machines may provide a wider range. Many examples in this chapter assume an integer has 31 bits.
The range of fixnums is available to Lisp programs:
The fixedprecision integer closest in value to positive infinity.
The fixedprecision integer closest in value to negative infinity.
Here is a common idiom to temporarily suppress garbage collection:
(garbagecollect) (let ((gcconsthreshold mostpositivefixnum)) ;; allocationintensive computation ) (garbagecollect) 
The Lisp reader reads an integer as a sequence of digits with optional initial sign and optional final period.
1 ; The integer 1. 1. ; The integer 1. +1 ; Also the integer 1. 1 ; The integer 1. 2147483648 ; Read error, due to overflow. 0 ; The integer 0. 0 ; The integer 0. 
To understand how various functions work on integers, especially the bitwise operators (see section Bitwise Operations on Integers), it is often helpful to view the numbers in their binary form.
In 31bit binary, the decimal integer 5 looks like this:
000 0000 0000 0000 0000 0000 0000 0101 
(We have inserted spaces between groups of 4 bits, and two spaces between groups of 8 bits, to make the binary integer easier to read.)
The integer 1 looks like this:
111 1111 1111 1111 1111 1111 1111 1111 
1 is represented as 31 ones. (This is called two’s complement notation.)
The negative integer, 5, is creating by subtracting 4 from 1. In binary, the decimal integer 4 is 100. Consequently, 5 looks like this:
111 1111 1111 1111 1111 1111 1111 1011 
In this implementation, the largest 31bit binary integer is the decimal integer 1,073,741,823. In binary, it looks like this:
011 1111 1111 1111 1111 1111 1111 1111 
Since the arithmetic functions do not check whether integers go outside their range, when you add 1 to 1,073,741,823, the value is the negative integer 1,073,741,824:
(+ 1 1073741823) ⇒ 1073741824 ⇒ 100 0000 0000 0000 0000 0000 0000 0000 
Many of the arithmetic functions accept markers for arguments as well as integers. (See section Markers.) More precisely, the actual arguments to such functions may be either integers or markers, which is why we often give these arguments the name intormarker. When the argument value is a marker, its position value is used and its buffer is ignored.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
Ratios (builtin rational numbers) are available only when the bignum extension is built into your XEmacs. This facility is new and experimental. It is discussed in a separate section for convenience of updating the documentation The Bignum Extension. The following functions are defined regardless of the presence of the extension, but have trivial results for integers.
Return the numerator of the canonical form of rational. If rational is an integer, rational is returned. rational must be an integer or a ratio.
Return the denominator of the canonical form of rational. If rational is an integer, 1 is returned. rational must be an integer or a ratio.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
XEmacs supports floating point numbers. The precise range of floating
point numbers is machinespecific; it is the same as the range of the C
data type double
on the machine in question. If a
multipleprecision arithmetic library is available on your platform,
support for bigfloats, that is, floating point numbers with arbitrary
precision, may be compiled in to your XEmacs. The rest of this section
assumes that the bignum extension is not available. The bigfloat
extension and the uservisible differences in normal float arithmetic
are discussed in a separate section The Bignum Extension.
The printed representation for floating point numbers requires either a decimal point (with at least one digit following), an exponent, or both. For example, ‘1500.0’, ‘15e2’, ‘15.0e2’, ‘1.5e3’, and ‘.15e4’ are five ways of writing a floating point number whose value is 1500. They are all equivalent. You can also use a minus sign to write negative floating point numbers, as in ‘1.0’.
Most modern computers support the IEEE floating point standard, which
provides for positive infinity and negative infinity as floating point
values. It also provides for a class of values called NaN or
“notanumber”; numerical functions return such values in cases where
there is no correct answer. For example, (sqrt 1.0)
returns a
NaN. For practical purposes, there’s no significant difference between
different NaN values in XEmacs Lisp, and there’s no rule for precisely
which NaN value should be used in a particular case, so this manual
doesn’t try to distinguish them. XEmacs Lisp has no read syntax for NaNs
or infinities; perhaps we should create a syntax in the future.
You can use logb
to extract the binary exponent of a floating
point number (or estimate the logarithm of an integer):
This function returns the binary exponent of number. More precisely, the value is the logarithm of number base 2, rounded down to an integer.
The range of floats is available to Lisp programs:
The fixedprecision floatingpointnumber closest in value to positive infinity.
The fixedprecision floating point number closest in value to negative infinity.
The positive float closest in value to 0. May not be normalized.
The positive float closest in value to 0. Must be normalized.
The negative float closest in value to 0. May not be normalized.
The negative float closest in value to 0. Must be normalized.
Note that for floating point numbers there is an interesting limit on
how small they can get, as well as a limit on how big they can get. In
some representations, a floating point number is normalized if the
leading digit is nonzero. This allows representing numbers smaller
than the mostnegative exponent can express, by having fractional
mantissas. This means that the number is less precise than a normalized
floating point number, so Lisp programs can detect loss of precision due
to unnormalized floats by checking whether the number is between
leastpositivefloat
and leastpositivenormalizedfloat
.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
In XEmacs 21.5.18, an extension was added by Jerry James to allow linking with arbitraryprecision arithmetic libraries if they are available on your platform. “Arbitrary” precision means precisely what it says. Your ability to work with large numbers is limited only by the amount of virtual memory (and time) you can throw at them.
As of 09 April 2004, support for the GNU Multiple Precision arithmetic library (GMP) is nearly complete, and support for the BSD Multiple Precision arithmetic library (MP) is being debugged. To enable bignum support using GMP (respectively MP), invoke configure with your usual options, and add ‘usenumberlib=gmp’ (respectively ‘usenumberlib=mp’). The default is to disable bignum support, but if you are using a script to automate the build process, it may be convenient to explicitly disable support by appending ‘usenumberlib=no’ to your invocation of configure. GMP has an MP compatibility mode, but it is not recommended, as there remain poorly understood bugs (even more so than for other vendors’ versions of MP).
With GMP, exact arithmetic with integers and ratios of arbitrary precision and approximate (“floating point”) arithmetic of arbitrary precision are implemented efficiently in the library. (Note that numerical implementations are quite delicate and sensitive to optimization. If the library was poorly optimized for your hardware, as is often the case with Linux distributions for 80x86, you may achieve gains of several orders of magnitude by rebuilding the MP library. See http://www.swox.com/gmp/gmpspeed.html.) The MP implementation provides arbitrary precision integers. Ratios and arbitrary precision floats are not available with MP.
If your code needs to run correctly whether or not the feature is
provided, you may test for the features bignum
, ratio
, and
bigfloat
.
The XEmacs bignum facility implements the Common Lisp notions of
canonicalization and contagion. Canonicalization means that
in exact (integer and ratio) arithmetic, a result of an operation is
always converted to the “smallest” type that can represent it
exactly. For exact numbers, the user only cares if efficiency is
extremely important; Lisp does not try to determine an order of
computation that avoids conversion to bignum (or ratio) even if one is
available. (Note that integers are never silently converted to
ratios: the result of (/ 1 2)
is the integer 0
. You can
request that a ratio be used if needed with (div 1 2)
.)
Since floating point arithmetic is inherently imprecise, numbers are
implicitly coerced to bigfloats only if other operands in the expression
are bigfloat, and bigfloats are only coerced to other numerical types by
explicit calls to the function coerce
.
Bignum support is incomplete. If you would like to help with bignum support, especially on BSD MP, please subscribe to the XEmacs Beta mailing list, and book up on ‘numbergmp.h’ and ‘numbermp.h’. Jerry has promised to write internals documentation eventually, but if your skills run more to analysis and documentation than to writing new code, feel free to fill in the gap!
9.4.1 Bignum Basics  Representation and range of integers.  
9.4.2 Ratio Basics  Representation and range of rational numbers.  
9.4.3 Bigfloat Basics  Representation and range of floating point.  
9.4.4 Canonicalization and Contagion  Automatic coercion to other types.  
9.4.5 Compatibility Issues  Changes in fixedprecision arithmetic. 
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
In most cases, bignum support should be transparent to users and Lisp programmers. A bignumenabled XEmacs will automatically convert from fixnums to bignums and back in pure integer arithmetic, and for GNU MP, from floats to bigfloats. (Bigfloats must be explicitly coerced to other types, even if they are exactly representable by less precise types.) The Lisp reader and printer have been enhanced to handle bignums, as have the mathematical functions. Rationals (fixnums, bignums, and ratios) are printed using the ‘%d’, ‘%o’, ‘%x’, and ‘%u’ format conversions.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
Ratios, when available have the read syntax and print representation ‘3/5’. Like other rationals (fixnums and bignums), they are printed using the ‘%d’, ‘%o’, ‘%x’, and ‘%u’ format conversions.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
Bigfloats, when available, have the same read syntax and print representations as fixedprecision floats.
It is possible to make bigfloat the default floating point format by
setting defaultfloatprecision
to a nonzero value. Precision
is given in bits, with a maximum precision of
bigfloatmaximumprecision
.
Bigfloats are created automatically when a number with yes
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
Canonicalization is a rule intended to enhance the time and space efficiency of exact arithmetic. Because bignums and ratios are implemented as record objects, they take up much more space than fixnums, which are implemented as an immediate object. Conversions and calls to the MP library also take time. So the implementation always converts the result of exact arithmetic to the smallest representation that can exactly represent the quantity.
(+ 3/4 5) ⇒ 23/4 (+ 3/4 1/4 2) ⇒ 3 
Conversely, if an integer (read or computed) cannot be represented as a
fixnum, a bignum will be used. Integer division is a somewhat
exceptional case. Because it is useful and is the historical meaning of
the function /
, a separate function div
is provided.
div
is identical to /
except that when the rational result
is not an integer, it is represented exactly as a ratio. In both cases
if a rational result is an integer, it is automatically converted to the
appropriate integral representation.
Note that the efficiency gain from canonicalization is likely to be less than you might think. Experience with numerical analysis shows that in very precise calculations, the required precision tends to increase. Thus it is typically wasted effort to attempt to convert to smaller representations, as the number is often reused and requires a larger representation. However, XEmacs Lisp presumes that calculations using bignums are the exception, so it applies canonicalization.
Contagion is one way to address the requirement that an arithmetic operation should not fail because of differing types of the operands. Contagion is the idea that less precise operands are converted to the more precise type, and then the operation is performed. While changing precision is a delicate issue, contagion is so useful that XEmacs performs it automatically.
In XEmacs, the following rules of contagion are used:
Note that there are no rules to canonicalize floats or bigfloats. This might seem surprising, but in both cases information will be lost. Any floating point representation is implicitly approximate. A conversion to a rational type, even if it seems exact, loses this information. More subtly, demoting a bigfloat to a smaller bigfloat or to a float would lose information about the precision of the result, and thus some information about the accuracy. Thus floating point numbers are always already in canonical form.
Of course the programmer can explicitly request canonicalization, or
more coercion to another type. Coercion uses the Common Lisp
compatibility function coerce
from the ‘clextra.el’
library. A number can be explicitly converted to canonical form
according to the above rules using
Return the canonical form of number.
However, if we’ve done our job properly, this is always a noop. That is, if you find a number in uncanonicalized form, please report it as a bug.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
Surgeon General’s Warning: The automatic conversions cannot be disabled at runtime. Old functions will not produce ratios unless there is a ratio operand, so there should be few surprises with type conflicts (the contagion rules are quite natural for Lisp programmers used to the behavior of integers and floats in pre21.5.18 XEmacsen), but they can’t be ruled out. Also, if you work with extremely large numbers, your machine may arbitrarily decide to hand you an unpleasant surprise rather than a bignum.
Uservisible changes in behavior include (in probable order of annoyance)
GMP by default allocates temporaries on the stack. If you run out of stack space, you’re dead; there is no way that we know of to reliably detect this condition, because ‘alloca’ is typically implemented to be fast rather than robust. If you just need a little more oomph, use a bigger stack (e.g., the ‘ulimit s’ command in bash(1)). If you want robustness at the cost of speed, configure GMP with ‘disablealloca’ and rebuild the GMP library.
We do not know whether BSD MP uses ‘alloca’ or not. Please send any information you have as a bug report (Mx reportxemacsbug <RET>), which will give us platform information. (We do know that BSD MP implementations vary across vendors, but how much, we do not know yet.)
This is not a compatibility issue in the sense of specification, but
careless programmers who have taken advantage of the immediate
representation for numbers and written (eq x y)
are in for a
surprise. This doesn’t work with bignums, even if both arguments are
bignums! Arbitrary precision obviously requires consing new objects
because the objects are “large” and of variable size, and the
definition of ‘eq’ does not permit different objects to compare as
equal. Instead of eq
, use eql
, in which numbers of the
same type which have equal values compare equal, or =
, which does
any necessary type coercions before comparing for equality
Comparison of Numbers.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
The functions in this section test whether the argument is a number or
whether it is a certain sort of number. The functions which test for
type can take any type of Lisp object as argument (the more general
predicates would not be of much use otherwise). However, the
zerop
predicate requires a number as its argument, and the
evenp
, and oddp
predicates require integers as their
arguments. See also integerormarkerp
,
integercharormarkerp
, numberormarkerp
and
numbercharormarkerp
, in Predicates on Markers.
This predicate tests whether its argument is a number (either integer or
floating point), and returns t
if so, nil
otherwise.
The realp
predicate tests to see whether object is a
rational or floating point number, and returns t
if so,
nil
otherwise. Currently equivalent to numberp
.
This predicate tests whether its argument is zero, and returns t
if so, nil
otherwise. The argument must be a number.
These two forms are equivalent: (zerop x)
≡ (= x 0)
.
This predicate tests whether its argument is an integer, and returns
t
if so, nil
otherwise.
The oddp
predicate tests to see whether integer is odd, and
returns t
if so, nil
otherwise. integer must be an
integer.
The evenp
predicate tests to see whether integer is even,
and returns t
if so, nil
otherwise. integer must be
an integer.
The natnump
predicate (whose name comes from the phrase
“naturalnumberp”) tests to see whether its argument is a nonnegative
integer, and returns t
if so, nil
otherwise. 0 is
considered nonnegative.
The predicate tests to see whether its argument is an integer
represented as a fixnum, and returns
t
if so, nil
otherwise.
The bignump
predicate tests to see whether object is an
integer represented as a bignum, and returns t
if so, nil
otherwise.
The rationalp
predicate tests to see whether object is a
rational number, and returns t
if so, nil
otherwise.
The ratiop
predicate tests to see whether object is a
number represented as a ratio, and returns t
if so, nil
otherwise.
The floatingp
predicate tests to see whether object is a
floating point number represented as a float or a bigfloat, and returns
t
if so, nil
otherwise.
This predicate tests whether its argument is a floating point
number and returns t
if so, nil
otherwise.
floatp
does not exist in Emacs versions 18 and earlier. If the
bignum extension is present, it returns nil
for a bigfloat.
The bigfloatp
predicate tests to see whether object is an
floating point number represented as a bigfloat, and returns t
if
so, nil
otherwise.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
To test numbers for numerical equality, you should normally use
=
, not eq
. There can be many distinct floating point,
bignum, and ratio number objects with the same numeric value. If you
use eq
to compare them, then you test whether two values are the
same object. By contrast, =
compares only the numeric
values of the objects.
In versions before 21.5.18, each integer value had a unique Lisp
object in XEmacs Lisp. Therefore, eq
was equivalent to =
where integers are concerned. Even with the introduction of bignums, it
is sometimes convenient to use eq
for comparing an unknown value
with an integer, because eq
does not report an error if the
unknown value is not a number—it accepts arguments of any type. By
contrast, =
signals an error if the arguments are not numbers or
markers. However, it is a good idea to use =
if you can, even
for comparing exact values, because two bignums or ratios with the same
value will often not be the same object.
On the other hand, some functions, such as the string and
buffersearching functions, will return an integer on success, but
something else (usually nil
) on failure. If it is known what the
numerical subtype (float, bigfloat, or exact) of the returned object
will be if it is a number, then the predicate eql
can be used for
comparison without signaling an error on some expected return values.
Because of canonicalization, eql
can be used to compare a fixnum
value to something that might be a ratio; if the potential ratio value
is representable as a fixnum, it will be canonicalized to fixnum before
comparing. However, although floats and bigfloats are of different
types for the purpose of comparisons via eql
, two bigfloats of
different precision that are =
will always be eql
.
(eql 2 (stringmatch "ere" "there")) ⇒ t (eql 2 (stringmatch "ere" "three")) ⇒ nil (eql 2 2.0) ⇒ nil (= 2 (stringmatch "ere" "there")) ⇒ t (= 2 (stringmatch "ere" "three")) error> Wrong type argument: numbercharormarkerp, nil (= 2 2.0) ⇒ t 
There is another wrinkle: because floating point arithmetic is not exact, it is often a bad idea to check for equality of two floating point values. Usually it is better to test for approximate equality. Here’s a function to do this:
(defconst fuzzfactor 1.0e6) (defun approxequal (x y) (or (and (= x 0) (= y 0)) (< (/ (abs ( x y)) (max (abs x) (abs y))) fuzzfactor))) 
Common Lisp note: Comparing numbers in Common Lisp always requires
=
because Common Lisp implements multiword integers, and two distinct integer objects can have the same numeric value. XEmacs Lisp can have just one fixnum object for any given value because it has a limited range of fixnum values.
In addition to numbers, all of the following functions also accept characters and markers as arguments, and treat them as their number equivalents.
This function returns t
if all of its arguments are numerically
equal, nil
otherwise.
(= 5) ⇒ t (= 5 6) ⇒ nil (= 5 5.0) ⇒ t (= 5 5 6) ⇒ nil 
This function returns t
if no two arguments are numerically
equal, nil
otherwise.
(/= 5 6) ⇒ t (/= 5 5 6) ⇒ nil (/= 5 6 1) ⇒ t 
This function returns t
if the sequence of its arguments is
monotonically increasing, nil
otherwise.
(< 5 6) ⇒ t (< 5 6 6) ⇒ nil (< 5 6 7) ⇒ t 
This function returns t
if the sequence of its arguments is
monotonically nondecreasing, nil
otherwise.
(<= 5 6) ⇒ t (<= 5 6 6) ⇒ t (<= 5 6 5) ⇒ nil 
This function returns t
if the sequence of its arguments is
monotonically decreasing, nil
otherwise.
This function returns t
if the sequence of its arguments is
monotonically nonincreasing, nil
otherwise.
This function returns the largest of its arguments.
(max 20) ⇒ 20 (max 1 2.5) ⇒ 2.5 (max 1 3 2.5) ⇒ 3 
This function returns the smallest of its arguments.
(min 4 1) ⇒ 4 
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
To convert an integer to floating point, use the function float
.
This returns number converted to floating point.
If number is already a floating point number, float
returns
it unchanged.
There are four functions to convert floating point numbers to integers; they differ in how they round. These functions accept integer arguments also, and return such arguments unchanged. They return multiple values, see (cl.info)Multiple values.
All these functions take optional divisor arguments, and if this
argument is specified, the number argument is divided by
divisor before the calculation is made. An aritherror
results if divisor is 0.
This returns number, converted to an integer by rounding towards zero.
This returns number, converted to an integer by rounding downward (towards negative infinity).
This returns number, converted to an integer by rounding upward (towards positive infinity).
This returns number, converted to an integer by rounding towards the nearest integer.
Rounding a value equidistant between two integers chooses the even integer. GNU Emacs and older XEmacs did not guarantee this, and the direction of rounding depended on the underlying machine and the C implementation.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
XEmacs 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 XEmacs Lisp, arithmetic functions
do not check for overflow. Thus (1+ 134217727)
may evaluate to
134217728, depending on your hardware and whether your XEmacs
supports bignums.
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 othernumbers 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 machinedependent 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 aritherror
error is signaled.
(See section 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 machinedependent since the quotient is; but in practice, all known machines behave alike.
An aritherror
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 welldefined 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 aritherror
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.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
The functions ffloor
, fceiling
, fround
and
ftruncate
take a floating point argument and return a floating
point result whose value is a nearby integer. ffloor
returns the
nearest integer below; fceiling
, the nearest integer above;
ftruncate
, the nearest integer in the direction towards zero;
fround
, the nearest integer.
All these functions take optional divisor arguments, and if this
argument is specified, the number argument is divided by
divisor before the calculation is made. An aritherror
results if divisor is 0. Also, they return multiple values,
see (cl.info)Multiple values; the second value is the remainder.
This function rounds number to the next lower integral value, and returns that value as a floating point number.
This function rounds number to the next higher integral value, and returns that value as a floating point number.
This function rounds number towards zero to an integral value, and returns that value as a floating point number.
This function rounds number to the nearest integral value, and returns that value as a floating point number.
Rounding a value equidistant between two integral values chooses the even value. While this is specified by Common Lisp, GNU Emacs and older XEmacs did not make this guarantee, and the direction of rounding depended on the underlying machine and the C implementation.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
In a computer, an integer is represented as a binary number, a sequence of bits (digits which are either zero or one). A bitwise operation acts on the individual bits of such a sequence. For example, shifting moves the whole sequence left or right one or more places, reproducing the same pattern “moved over”.
The bitwise operations in XEmacs Lisp apply only to integers.
lsh
, which is an abbreviation for logical shift, shifts the
bits in integer1 to the left count places, or to the right
if count is negative, bringing zeros into the vacated bits. If
count is negative, lsh
shifts zeros into the leftmost
(mostsignificant) bit, producing a positive result even if
integer1 is negative. Contrast this with ash
, below.
Here are two examples of lsh
, shifting a pattern of bits one
place to the left. We show only the loworder eight bits of the binary
pattern; the rest are all zero.
(lsh 5 1) ⇒ 10 ;; Decimal 5 becomes decimal 10. 00000101 ⇒ 00001010 (lsh 7 1) ⇒ 14 ;; Decimal 7 becomes decimal 14. 00000111 ⇒ 00001110 
As the examples illustrate, shifting the pattern of bits one place to the left produces a number that is twice the value of the previous number.
Shifting a pattern of bits two places to the left produces results like this (with 8bit binary numbers):
(lsh 3 2)
⇒ 12
;; Decimal 3 becomes decimal 12.
00000011 ⇒ 00001100

On the other hand, shifting one place to the right looks like this:
(lsh 6 1)
⇒ 3
;; Decimal 6 becomes decimal 3.
00000110 ⇒ 00000011
(lsh 5 1)
⇒ 2
;; Decimal 5 becomes decimal 2.
00000101 ⇒ 00000010

As the example illustrates, shifting one place to the right divides the value of a positive integer by two, rounding downward.
The function lsh
, like all XEmacs Lisp arithmetic functions, does
not check for overflow, so shifting left can discard significant bits
and change the sign of the number. For example, left shifting
134,217,727 produces 2 on a 28bit machine:
(lsh 134217727 1) ; left shift
⇒ 2

In binary, in the 28bit implementation, the argument looks like this:
;; Decimal 134,217,727
0111 1111 1111 1111 1111 1111 1111

which becomes the following when left shifted:
;; Decimal 2
1111 1111 1111 1111 1111 1111 1110

ash
(arithmetic shift) shifts the bits in integer1
to the left count places, or to the right if count
is negative.
ash
gives the same results as lsh
except when
integer1 and count are both negative. In that case,
ash
puts ones in the empty bit positions on the left, while
lsh
puts zeros in those bit positions.
Thus, with ash
, shifting the pattern of bits one place to the right
looks like this:
(ash 6 1) ⇒ 3
;; Decimal 6 becomes decimal 3.
1111 1111 1111 1111 1111 1111 1010
⇒
1111 1111 1111 1111 1111 1111 1101

In contrast, shifting the pattern of bits one place to the right with
lsh
looks like this:
(lsh 6 1) ⇒ 134217725
;; Decimal 6 becomes decimal 134,217,725.
1111 1111 1111 1111 1111 1111 1010
⇒
0111 1111 1111 1111 1111 1111 1101

Here are other examples:
; 28bit binary values (lsh 5 2) ; 5 = 0000 0000 0000 0000 0000 0000 0101 ⇒ 20 ; = 0000 0000 0000 0000 0000 0001 0100 (ash 5 2) ⇒ 20 (lsh 5 2) ; 5 = 1111 1111 1111 1111 1111 1111 1011 ⇒ 20 ; = 1111 1111 1111 1111 1111 1110 1100 (ash 5 2) ⇒ 20 (lsh 5 2) ; 5 = 0000 0000 0000 0000 0000 0000 0101 ⇒ 1 ; = 0000 0000 0000 0000 0000 0000 0001 (ash 5 2) ⇒ 1 (lsh 5 2) ; 5 = 1111 1111 1111 1111 1111 1111 1011 ⇒ 4194302 ; = 0011 1111 1111 1111 1111 1111 1110 (ash 5 2) ; 5 = 1111 1111 1111 1111 1111 1111 1011 ⇒ 2 ; = 1111 1111 1111 1111 1111 1111 1110 
This function returns the “logical and” of the arguments: the nth bit is set in the result if, and only if, the nth bit is set in all the arguments. (“Set” means that the value of the bit is 1 rather than 0.)
For example, using 4bit binary numbers, the “logical and” of 13 and 12 is 12: 1101 combined with 1100 produces 1100. In both the binary numbers, the leftmost two bits are set (i.e., they are 1’s), so the leftmost two bits of the returned value are set. However, for the rightmost two bits, each is zero in at least one of the arguments, so the rightmost two bits of the returned value are 0’s.
Therefore,
(logand 13 12) ⇒ 12 
If logand
is not passed any argument, it returns a value of
1. This number is an identity element for logand
because its binary representation consists entirely of ones. If
logand
is passed just one argument, it returns that argument.
; 28bit binary values (logand 14 13) ; 14 = 0000 0000 0000 0000 0000 0000 1110 ; 13 = 0000 0000 0000 0000 0000 0000 1101 ⇒ 12 ; 12 = 0000 0000 0000 0000 0000 0000 1100 (logand 14 13 4) ; 14 = 0000 0000 0000 0000 0000 0000 1110 ; 13 = 0000 0000 0000 0000 0000 0000 1101 ; 4 = 0000 0000 0000 0000 0000 0000 0100 ⇒ 4 ; 4 = 0000 0000 0000 0000 0000 0000 0100 (logand)
⇒ 1 ; 1 = 1111 1111 1111 1111 1111 1111 1111

This function returns the “inclusive or” of its arguments: the nth bit
is set in the result if, and only if, the nth bit is set in at least
one of the arguments. If there are no arguments, the result is zero,
which is an identity element for this operation. If logior
is
passed just one argument, it returns that argument.
; 28bit binary values (logior 12 5) ; 12 = 0000 0000 0000 0000 0000 0000 1100 ; 5 = 0000 0000 0000 0000 0000 0000 0101 ⇒ 13 ; 13 = 0000 0000 0000 0000 0000 0000 1101 (logior 12 5 7) ; 12 = 0000 0000 0000 0000 0000 0000 1100 ; 5 = 0000 0000 0000 0000 0000 0000 0101 ; 7 = 0000 0000 0000 0000 0000 0000 0111 ⇒ 15 ; 15 = 0000 0000 0000 0000 0000 0000 1111 
This function returns the “exclusive or” of its arguments: the
nth bit is set in the result if, and only if, the nth bit is
set in an odd number of the arguments. If there are no arguments, the
result is 0, which is an identity element for this operation. If
logxor
is passed just one argument, it returns that argument.
; 28bit binary values (logxor 12 5) ; 12 = 0000 0000 0000 0000 0000 0000 1100 ; 5 = 0000 0000 0000 0000 0000 0000 0101 ⇒ 9 ; 9 = 0000 0000 0000 0000 0000 0000 1001 (logxor 12 5 7) ; 12 = 0000 0000 0000 0000 0000 0000 1100 ; 5 = 0000 0000 0000 0000 0000 0000 0101 ; 7 = 0000 0000 0000 0000 0000 0000 0111 ⇒ 14 ; 14 = 0000 0000 0000 0000 0000 0000 1110 
This function returns the logical complement of its argument: the nth bit is one in the result if, and only if, the nth bit is zero in integer, and viceversa.
(lognot 5) ⇒ 6 ;; 5 = 0000 0000 0000 0000 0000 0000 0101 ;; becomes ;; 6 = 1111 1111 1111 1111 1111 1111 1010 
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
These mathematical functions are available if floating point is supported (which is the normal state of affairs). They allow integers as well as floating point numbers as arguments.
These are the ordinary trigonometric functions, with argument measured in radians.
The value of (asin number)
is a number between pi/2
and pi/2 (inclusive) whose sine is number; if, however, number
is out of range (outside [1, 1]), then the result is a NaN.
The value of (acos number)
is a number between 0 and pi
(inclusive) whose cosine is number; if, however, number
is out of range (outside [1, 1]), then the result is a NaN.
The value of (atan number)
is a number between pi/2
and pi/2 (exclusive) whose tangent is number.
If optional argument number2 is supplied, the function returns
atan2(number,number2)
.
These are the ordinary hyperbolic trigonometric functions.
These are the inverse hyperbolic trigonometric functions.
This is the exponential function; it returns e to the power number. e is a fundamental mathematical constant also called the base of natural logarithms.
This function returns the logarithm of number, with base base.
If you don’t specify base, the base e
is used. If number
is negative, the result is a NaN.
This function returns the logarithm of number, with base 10. If
number is negative, the result is a NaN. (log10 x)
≡ (log x 10)
, at least approximately.
This function returns x raised to power y. If both arguments are integers and y is positive, the result is an integer; in this case, it is truncated to fit the range of possible integer values.
This returns the square root of number. If number is negative, the value is a NaN.
This returns the cube root of number.
[ << ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
This document was generated by Aidan Kehoe on December 27, 2016 using texi2html 1.82.