[ < ]  [ > ]  [ << ]  [ 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 9.4.4 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 9.4 The Bignum Extension.
The minimum range is 1073741824 to 1073741823 (31 bits; i.e., 2**30 to 2**30  1), 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:
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 9.10 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 42. 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 9.4 The Bignum Extension. The following functions are defined regardless of the presence of the extension, but have trivial results for integers.
[ < ]  [ > ]  [ << ]  [ 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 9.4 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):
The range of floats is available to Lisp programs:
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
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
9.6 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 42.2 Predicates on Markers.
t
if so, nil
otherwise.
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
.
t
if so, nil
otherwise. The argument must be a number.
These two forms are equivalent: (zerop x)
== (= x 0)
.
t
if so, nil
otherwise.
oddp
predicate tests to see whether integer is odd, and
returns t
if so, nil
otherwise. integer must be an
integer.
evenp
predicate tests to see whether integer is even,
and returns t
if so, nil
otherwise. integer must be
an integer.
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.
predicate tests to see whether its argument is an integer
represented as a fixnum, and returns t
if so, nil
otherwise.
bignump
predicate tests to see whether object is an
integer represented as a bignum, and returns t
if so, nil
otherwise.
rationalp
predicate tests to see whether object is a
rational number, and returns t
if so, nil
otherwise.
ratiop
predicate tests to see whether object is a
number represented as a ratio, and returns t
if so, nil
otherwise.
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.
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.
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 numberit 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.
t
if all of its arguments are numerically
equal, nil
otherwise.
(= 5) => t (= 5 6) => nil (= 5 5.0) => t (= 5 5 6) => nil 
t
if no two arguments are numerically
equal, nil
otherwise.
(/= 5 6) => t (/= 5 5 6) => nil (/= 5 6 1) => t 
t
if the sequence of its arguments is
monotonically increasing, nil
otherwise.
(< 5 6) => t (< 5 6 6) => nil (< 5 6 7) => t 
t
if the sequence of its arguments is
monotonically nondecreasing, nil
otherwise.
(<= 5 6) => t (<= 5 6 6) => t (<= 5 6 5) => nil 
t
if the sequence of its arguments is
monotonically decreasing, nil
otherwise.
t
if the sequence of its arguments is
monotonically nonincreasing, nil
otherwise.
(max 20) => 20 (max 1 2.5) => 2.5 (max 1 3 2.5) => 3 
(min 4 1) => 4 
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
To convert an integer to floating point, use the function float
.
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, @xref{(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.
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.
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)
.
+
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 

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 
*
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 
/
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 15.5.3 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.
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.
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 9.7 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,
@xref{(cl.info)Multiple values}; the second value is the remainder.
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 
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 
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 
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 
(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.
(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.
(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.
(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)
.
e
is used. If number
is negative, the result is a NaN.
(log10 x)
== (log x 10)
, at least approximately.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
A deterministic computer program cannot generate true random numbers. For most purposes, pseudorandom numbers suffice. A series of pseudorandom numbers is generated in a deterministic fashion. The numbers are not truly random, but they have certain properties that mimic a random series. For example, all possible values occur equally often in a pseudorandom series.
In XEmacs, pseudorandom numbers are generated from a "seed" number.
Starting from any given seed, the random
function always
generates the same sequence of numbers. XEmacs always starts with the
same seed value, so the sequence of values of random
is actually
the same in each XEmacs run! For example, in one operating system, the
first call to (random)
after you start XEmacs always returns
1457731, and the second one always returns 7692030. This
repeatability is helpful for debugging.
If you want reasonably unpredictable random numbers, execute
(random t)
. This chooses a new seed based on the current time of
day and on XEmacs's process ID number. (This is not
cryptographically strong, it's just hard for a human to
anticipate.)
If limit is a positive integer, the value is chosen to be nonnegative and less than limit.
If limit is t
, it means to choose a new seed based on the
current time of day and on XEmacs's process ID number.
The range of random is implementationdependent. On any machine, the
result of (random)
is an arbitrary fixnum, so on 32bit
architectures it is normally in the range 2^30 (inclusive) to +2^30
(exclusive). With the optional integer argument limit, the result
is in the range 0 (inclusive) to limit (exclusive). Note this is
regardless of the presence of the bignum extension.
[ << ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 