@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
-@c Copyright (C) 1990-1995, 1998-1999, 2001-2011
+@c Copyright (C) 1990-1995, 1998-1999, 2001-2012
@c Free Software Foundation, Inc.
@c See the file elisp.texi for copying conditions.
@setfilename ../../info/numbers
@tex
@math{2^{29}-1}),
@end tex
-but some machines may provide a wider range. Many examples in this
-chapter assume an integer has 30 bits.
+but some machines provide a wider range. Many examples in this
+chapter assume that an integer has 30 bits and that floating point
+numbers are IEEE double precision.
@cindex overflow
The Lisp reader reads an integer as a sequence of digits with optional
In 30-bit binary, the decimal integer 5 looks like this:
@example
-00 0000 0000 0000 0000 0000 0000 0101
+0000...000101 (30 bits total)
@end example
@noindent
-(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 @samp{...} stands for enough bits to fill out a 30-bit word; in
+this case, @samp{...} stands for twenty 0 bits. Later examples also
+use the @samp{...} notation to make binary integers easier to read.)
The integer @minus{}1 looks like this:
@example
-11 1111 1111 1111 1111 1111 1111 1111
+1111...111111 (30 bits total)
@end example
@noindent
@minus{}5 looks like this:
@example
-11 1111 1111 1111 1111 1111 1111 1011
+1111...111011 (30 bits total)
@end example
In this implementation, the largest 30-bit binary integer value is
536,870,911 in decimal. In binary, it looks like this:
@example
-01 1111 1111 1111 1111 1111 1111 1111
+0111...111111 (30 bits total)
@end example
Since the arithmetic functions do not check whether integers go
@example
(+ 1 536870911)
@result{} -536870912
- @result{} 10 0000 0000 0000 0000 0000 0000 0000
+ @result{} 1000...000000 (30 bits total)
@end example
Many of the functions described in this chapter accept markers for
give these arguments the name @var{number-or-marker}. When the argument
value is a marker, its position value is used and its buffer is ignored.
+@cindex largest Lisp integer number
+@cindex maximum Lisp integer number
@defvar most-positive-fixnum
The value of this variable is the largest integer that Emacs Lisp
can handle.
@end defvar
+@cindex smallest Lisp integer number
+@cindex minimum Lisp integer number
@defvar most-negative-fixnum
The value of this variable is the smallest integer that Emacs Lisp can
handle. It is negative.
@node Float Basics
@section Floating Point Basics
+@cindex @acronym{IEEE} floating point
Floating point numbers are useful for representing numbers that are
not integral. The precise range of floating point numbers is
machine-specific; it is the same as the range of the C data type
-@code{double} on the machine you are using.
+@code{double} on the machine you are using. Emacs uses the
+@acronym{IEEE} floating point standard where possible (the standard is
+supported by most modern computers).
- The read-syntax for floating point numbers requires either a decimal
+ The read syntax for floating point numbers requires either a decimal
point (with at least one digit following), an exponent, or both. For
example, @samp{1500.0}, @samp{15e2}, @samp{15.0e2}, @samp{1.5e3}, and
@samp{.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 @samp{-1.0}.
+value is 1500. They are all equivalent. You can also use a minus
+sign to write negative floating point numbers, as in @samp{-1.0}.
+
+ Emacs Lisp treats @code{-0.0} as equal to ordinary zero (with
+respect to @code{equal} and @code{=}), even though the two are
+distinguishable in the @acronym{IEEE} floating point standard.
-@cindex @acronym{IEEE} floating point
@cindex positive infinity
@cindex negative infinity
@cindex infinity
@cindex NaN
- Most modern computers support the @acronym{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
-``not-a-number''; numerical functions return such values in cases where
-there is no correct answer. For example, @code{(/ 0.0 0.0)} returns a
-NaN. For practical purposes, there's no significant difference between
-different NaN values in Emacs Lisp, and there's no rule for precisely
-which NaN value should be used in a particular case, so Emacs Lisp
-doesn't try to distinguish them (but it does report the sign, if you
-print it). Here are the read syntaxes for these special floating
-point values:
+ The @acronym{IEEE} floating point standard supports positive
+infinity and negative infinity as floating point values. It also
+provides for a class of values called NaN or ``not-a-number'';
+numerical functions return such values in cases where there is no
+correct answer. For example, @code{(/ 0.0 0.0)} returns a NaN. (NaN
+values can also carry a sign, but for practical purposes there's no
+significant difference between different NaN values in Emacs Lisp.)
+Here are the read syntaxes for these special floating point values:
@table @asis
@item positive infinity
@samp{0.0e+NaN} or @samp{-0.0e+NaN}.
@end table
- To test whether a floating point value is a NaN, compare it with
-itself using @code{=}. That returns @code{nil} for a NaN, and
-@code{t} for any other floating point value.
+@defun isnan number
+This predicate tests whether its argument is NaN, and returns @code{t}
+if so, @code{nil} otherwise. The argument must be a number.
+@end defun
+
+ The following functions are specialized for handling floating point
+numbers:
+
+@defun frexp x
+This function returns a cons cell @code{(@var{sig} . @var{exp})},
+where @var{sig} and @var{exp} are respectively the significand and
+exponent of the floating point number @var{x}:
+
+@smallexample
+@var{x} = @var{sig} * 2^@var{exp}
+@end smallexample
+
+@var{sig} is a floating point number between 0.5 (inclusive) and 1.0
+(exclusive). If @var{x} is zero, the return value is @code{(0 . 0)}.
+@end defun
- The value @code{-0.0} is distinguishable from ordinary zero in
-@acronym{IEEE} floating point, but Emacs Lisp @code{equal} and
-@code{=} consider them equal values.
+@defun ldexp sig &optional exp
+This function returns a floating point number corresponding to the
+significand @var{sig} and exponent @var{exp}.
+@end defun
- You can use @code{logb} to extract the binary exponent of a floating
-point number (or estimate the logarithm of an integer):
+@defun copysign x1 x2
+This function copies the sign of @var{x2} to the value of @var{x1},
+and returns the result. @var{x1} and @var{x2} must be floating point
+numbers.
+@end defun
@defun logb number
This function returns the binary exponent of @var{number}. More
@end example
@end defun
-@defvar float-e
-The mathematical constant @math{e} (2.71828@dots{}).
-@end defvar
-
-@defvar float-pi
-The mathematical constant @math{pi} (3.14159@dots{}).
-@end defvar
-
@node Predicates on Numbers
@section Type Predicates for Numbers
@cindex predicates for numbers
floating point), and returns @code{t} if so, @code{nil} otherwise.
@end defun
-@defun wholenump object
+@defun natnump object
@cindex natural numbers
-The @code{wholenump} predicate (whose name comes from the phrase
-``whole-number-p'') tests to see whether its argument is a nonnegative
-integer, and returns @code{t} if so, @code{nil} otherwise. 0 is
-considered non-negative.
+This predicate (whose name comes from the phrase ``natural number'')
+tests to see whether its argument is a nonnegative integer, and
+returns @code{t} if so, @code{nil} otherwise. 0 is considered
+non-negative.
-@findex natnump
-@code{natnump} is an obsolete synonym for @code{wholenump}.
+@findex wholenump number
+This is a synonym for @code{natnump}.
@end defun
@defun zerop number
if any argument is floating.
It is important to note that in Emacs Lisp, arithmetic functions
-do not check for overflow. Thus @code{(1+ 268435455)} may evaluate to
-@minus{}268435456, depending on your hardware.
+do not check for overflow. Thus @code{(1+ 536870911)} may evaluate to
+@minus{}536870912, depending on your hardware.
@defun 1+ number-or-marker
This function returns @var{number-or-marker} plus 1.
The function @code{lsh}, like all Emacs 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
-536,870,911 produces @minus{}2 on a 30-bit machine:
+536,870,911 produces @minus{}2 in the 30-bit implementation:
@example
(lsh 536870911 1) ; @r{left shift}
@result{} -2
@end example
-In binary, in the 30-bit implementation, the argument looks like this:
+In binary, the argument looks like this:
@example
@group
;; @r{Decimal 536,870,911}
-01 1111 1111 1111 1111 1111 1111 1111
+0111...111111 (30 bits total)
@end group
@end example
@example
@group
;; @r{Decimal @minus{}2}
-11 1111 1111 1111 1111 1111 1111 1110
+1111...111110 (30 bits total)
@end group
@end example
@end defun
@group
(ash -6 -1) @result{} -3
;; @r{Decimal @minus{}6 becomes decimal @minus{}3.}
-11 1111 1111 1111 1111 1111 1111 1010
+1111...111010 (30 bits total)
@result{}
-11 1111 1111 1111 1111 1111 1111 1101
+1111...111101 (30 bits total)
@end group
@end example
@group
(lsh -6 -1) @result{} 536870909
;; @r{Decimal @minus{}6 becomes decimal 536,870,909.}
-11 1111 1111 1111 1111 1111 1111 1010
+1111...111010 (30 bits total)
@result{}
-01 1111 1111 1111 1111 1111 1111 1101
+0111...111101 (30 bits total)
@end group
@end example
@c with smallbook but not with regular book! --rjc 16mar92
@smallexample
@group
- ; @r{ 30-bit binary values}
+ ; @r{ 30-bit binary values}
-(lsh 5 2) ; 5 = @r{00 0000 0000 0000 0000 0000 0000 0101}
- @result{} 20 ; = @r{00 0000 0000 0000 0000 0000 0001 0100}
+(lsh 5 2) ; 5 = @r{0000...000101}
+ @result{} 20 ; = @r{0000...010100}
@end group
@group
(ash 5 2)
@result{} 20
-(lsh -5 2) ; -5 = @r{11 1111 1111 1111 1111 1111 1111 1011}
- @result{} -20 ; = @r{11 1111 1111 1111 1111 1111 1110 1100}
+(lsh -5 2) ; -5 = @r{1111...111011}
+ @result{} -20 ; = @r{1111...101100}
(ash -5 2)
@result{} -20
@end group
@group
-(lsh 5 -2) ; 5 = @r{00 0000 0000 0000 0000 0000 0000 0101}
- @result{} 1 ; = @r{00 0000 0000 0000 0000 0000 0000 0001}
+(lsh 5 -2) ; 5 = @r{0000...000101}
+ @result{} 1 ; = @r{0000...000001}
@end group
@group
(ash 5 -2)
@result{} 1
@end group
@group
-(lsh -5 -2) ; -5 = @r{11 1111 1111 1111 1111 1111 1111 1011}
- @result{} 268435454 ; = @r{00 0111 1111 1111 1111 1111 1111 1110}
+(lsh -5 -2) ; -5 = @r{1111...111011}
+ @result{} 268435454
+ ; = @r{0011...111110}
@end group
@group
-(ash -5 -2) ; -5 = @r{11 1111 1111 1111 1111 1111 1111 1011}
- @result{} -2 ; = @r{11 1111 1111 1111 1111 1111 1111 1110}
+(ash -5 -2) ; -5 = @r{1111...111011}
+ @result{} -2 ; = @r{1111...111110}
@end group
@end smallexample
@end defun
@smallexample
@group
- ; @r{ 30-bit binary values}
+ ; @r{ 30-bit binary values}
-(logand 14 13) ; 14 = @r{00 0000 0000 0000 0000 0000 0000 1110}
- ; 13 = @r{00 0000 0000 0000 0000 0000 0000 1101}
- @result{} 12 ; 12 = @r{00 0000 0000 0000 0000 0000 0000 1100}
+(logand 14 13) ; 14 = @r{0000...001110}
+ ; 13 = @r{0000...001101}
+ @result{} 12 ; 12 = @r{0000...001100}
@end group
@group
-(logand 14 13 4) ; 14 = @r{00 0000 0000 0000 0000 0000 0000 1110}
- ; 13 = @r{00 0000 0000 0000 0000 0000 0000 1101}
- ; 4 = @r{00 0000 0000 0000 0000 0000 0000 0100}
- @result{} 4 ; 4 = @r{00 0000 0000 0000 0000 0000 0000 0100}
+(logand 14 13 4) ; 14 = @r{0000...001110}
+ ; 13 = @r{0000...001101}
+ ; 4 = @r{0000...000100}
+ @result{} 4 ; 4 = @r{0000...000100}
@end group
@group
(logand)
- @result{} -1 ; -1 = @r{11 1111 1111 1111 1111 1111 1111 1111}
+ @result{} -1 ; -1 = @r{1111...111111}
@end group
@end smallexample
@end defun
@smallexample
@group
- ; @r{ 30-bit binary values}
+ ; @r{ 30-bit binary values}
-(logior 12 5) ; 12 = @r{00 0000 0000 0000 0000 0000 0000 1100}
- ; 5 = @r{00 0000 0000 0000 0000 0000 0000 0101}
- @result{} 13 ; 13 = @r{00 0000 0000 0000 0000 0000 0000 1101}
+(logior 12 5) ; 12 = @r{0000...001100}
+ ; 5 = @r{0000...000101}
+ @result{} 13 ; 13 = @r{0000...001101}
@end group
@group
-(logior 12 5 7) ; 12 = @r{00 0000 0000 0000 0000 0000 0000 1100}
- ; 5 = @r{00 0000 0000 0000 0000 0000 0000 0101}
- ; 7 = @r{00 0000 0000 0000 0000 0000 0000 0111}
- @result{} 15 ; 15 = @r{00 0000 0000 0000 0000 0000 0000 1111}
+(logior 12 5 7) ; 12 = @r{0000...001100}
+ ; 5 = @r{0000...000101}
+ ; 7 = @r{0000...000111}
+ @result{} 15 ; 15 = @r{0000...001111}
@end group
@end smallexample
@end defun
@smallexample
@group
- ; @r{ 30-bit binary values}
+ ; @r{ 30-bit binary values}
-(logxor 12 5) ; 12 = @r{00 0000 0000 0000 0000 0000 0000 1100}
- ; 5 = @r{00 0000 0000 0000 0000 0000 0000 0101}
- @result{} 9 ; 9 = @r{00 0000 0000 0000 0000 0000 0000 1001}
+(logxor 12 5) ; 12 = @r{0000...001100}
+ ; 5 = @r{0000...000101}
+ @result{} 9 ; 9 = @r{0000...001001}
@end group
@group
-(logxor 12 5 7) ; 12 = @r{00 0000 0000 0000 0000 0000 0000 1100}
- ; 5 = @r{00 0000 0000 0000 0000 0000 0000 0101}
- ; 7 = @r{00 0000 0000 0000 0000 0000 0000 0111}
- @result{} 14 ; 14 = @r{00 0000 0000 0000 0000 0000 0000 1110}
+(logxor 12 5 7) ; 12 = @r{0000...001100}
+ ; 5 = @r{0000...000101}
+ ; 7 = @r{0000...000111}
+ @result{} 14 ; 14 = @r{0000...001110}
@end group
@end smallexample
@end defun
@example
(lognot 5)
@result{} -6
-;; 5 = @r{00 0000 0000 0000 0000 0000 0000 0101}
+;; 5 = @r{0000...000101} (30 bits total)
;; @r{becomes}
-;; -6 = @r{11 1111 1111 1111 1111 1111 1111 1010}
+;; -6 = @r{1111...111010} (30 bits total)
@end example
@end defun
@end defun
@defun exp arg
-This is the exponential function; it returns
-@tex
-@math{e}
-@end tex
-@ifnottex
-@i{e}
-@end ifnottex
-to the power @var{arg}.
-@tex
-@math{e}
-@end tex
-@ifnottex
-@i{e}
-@end ifnottex
-is a fundamental mathematical constant also called the base of natural
-logarithms.
+This is the exponential function; it returns @math{e} to the power
+@var{arg}.
@end defun
@defun log arg &optional base
-This function returns the logarithm of @var{arg}, with base @var{base}.
-If you don't specify @var{base}, the base
-@tex
-@math{e}
-@end tex
-@ifnottex
-@i{e}
-@end ifnottex
-is used. If @var{arg} is negative, it signals a @code{domain-error}
-error.
+This function returns the logarithm of @var{arg}, with base
+@var{base}. If you don't specify @var{base}, the natural base
+@math{e} is used. If @var{arg} is negative, it signals a
+@code{domain-error} error.
@end defun
@ignore
it signals a @code{domain-error} error.
@end defun
+In addition, Emacs defines the following common mathematical
+constants:
+
+@defvar float-e
+The mathematical constant @math{e} (2.71828@dots{}).
+@end defvar
+
+@defvar float-pi
+The mathematical constant @math{pi} (3.14159@dots{}).
+@end defvar
+
@node Random Numbers
@section Random Numbers
@cindex random numbers
If @var{limit} is @code{t}, it means to choose a new seed based on the
current time of day and on Emacs's process @acronym{ID} number.
-@c "Emacs'" is incorrect usage!
On some machines, any integer representable in Lisp may be the result
of @code{random}. On other machines, the result can never be larger