aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorTom Tromey2018-07-08 23:10:53 -0600
committerTom Tromey2018-07-12 22:12:48 -0600
commitcc3d7580fc1cab3119e5e05c427575a2668cbb4f (patch)
tree55b0373e813f0f1d2530b23699f6c583892e8643 /doc
parente2a78b0d6d844f29acaaddd775c7b1cd6dec7af8 (diff)
downloademacs-cc3d7580fc1cab3119e5e05c427575a2668cbb4f.tar.gz
emacs-cc3d7580fc1cab3119e5e05c427575a2668cbb4f.zip
Document bignums
* doc/lispref/numbers.texi (Numbers, Integer Basics) (Predicates on Numbers, Comparison of Numbers) (Arithmetic Operations, Bitwise Operations): Update for bignums. * doc/lispref/objects.texi (Integer Type, Type Predicates): Update for bignums. * etc/NEWS: Update for bigums.
Diffstat (limited to 'doc')
-rw-r--r--doc/lispref/numbers.texi133
-rw-r--r--doc/lispref/objects.texi26
2 files changed, 59 insertions, 100 deletions
diff --git a/doc/lispref/numbers.texi b/doc/lispref/numbers.texi
index 2fed2b642fd..a95c31f4682 100644
--- a/doc/lispref/numbers.texi
+++ b/doc/lispref/numbers.texi
@@ -14,9 +14,9 @@
14fractional parts, such as @minus{}4.5, 0.0, and 2.71828. They can 14fractional parts, such as @minus{}4.5, 0.0, and 2.71828. They can
15also be expressed in exponential notation: @samp{1.5e2} is the same as 15also be expressed in exponential notation: @samp{1.5e2} is the same as
16@samp{150.0}; here, @samp{e2} stands for ten to the second power, and 16@samp{150.0}; here, @samp{e2} stands for ten to the second power, and
17that is multiplied by 1.5. Integer computations are exact, though 17that is multiplied by 1.5. Integer computations are exact.
18they may overflow. Floating-point computations often involve rounding 18Floating-point computations often involve rounding errors, as the
19errors, as the numbers have a fixed amount of precision. 19numbers have a fixed amount of precision.
20 20
21@menu 21@menu
22* Integer Basics:: Representation and range of integers. 22* Integer Basics:: Representation and range of integers.
@@ -34,7 +34,15 @@ errors, as the numbers have a fixed amount of precision.
34@node Integer Basics 34@node Integer Basics
35@section Integer Basics 35@section Integer Basics
36 36
37 The range of values for an integer depends on the machine. The 37 Integers in Emacs Lisp can have arbitrary precision.
38
39 Under the hood, though, there are two kinds of integers: smaller
40ones, called @dfn{fixnums}, and larger ones, called @dfn{bignums}
41Some functions in Emacs only accept fixnums. Also, while fixnums can
42always be compared for equality with @code{eq}, bignums require the
43use of @code{eql}.
44
45 The range of values for a fixnum depends on the machine. The
38minimum range is @minus{}536,870,912 to 536,870,911 (30 bits; i.e., 46minimum range is @minus{}536,870,912 to 536,870,911 (30 bits; i.e.,
39@ifnottex 47@ifnottex
40@minus{}2**29 48@minus{}2**29
@@ -49,9 +57,7 @@ to
49@tex 57@tex
50@math{2^{29}-1}), 58@math{2^{29}-1}),
51@end tex 59@end tex
52but many machines provide a wider range. Many examples in this 60but many machines provide a wider range.
53chapter assume the minimum integer width of 30 bits.
54@cindex overflow
55 61
56 The Lisp reader reads an integer as a nonempty sequence 62 The Lisp reader reads an integer as a nonempty sequence
57of decimal digits with optional initial sign and optional 63of decimal digits with optional initial sign and optional
@@ -91,14 +97,8 @@ For example:
91#24r1k @result{} 44 97#24r1k @result{} 44
92@end example 98@end example
93 99
94 If an integer is outside the Emacs range, the Lisp reader ordinarily 100 An integer is read as a fixnum if it is in the correct range.
95signals an overflow. However, if a too-large plain integer ends in a 101Otherwise, it will be read as a bignum.
96period, the Lisp reader treats it as a floating-point number instead.
97This lets an Emacs Lisp program specify a large integer that is
98quietly approximated by a floating-point number on machines with
99limited word width. For example, @samp{536870912.} is a
100floating-point number if Emacs integers are only 30 bits wide and is
101an integer otherwise.
102 102
103 To understand how various functions work on integers, especially the 103 To understand how various functions work on integers, especially the
104bitwise operators (@pxref{Bitwise Operations}), it is often helpful to 104bitwise operators (@pxref{Bitwise Operations}), it is often helpful to
@@ -141,16 +141,6 @@ In binary, the decimal integer 4 is 100. Consequently,
1410111...111111 (30 bits total) 1410111...111111 (30 bits total)
142@end example 142@end example
143 143
144 Since the arithmetic functions do not check whether integers go
145outside their range, when you add 1 to 536,870,911, the value is the
146negative integer @minus{}536,870,912:
147
148@example
149(+ 1 536870911)
150 @result{} -536870912
151 @result{} 1000...000000 (30 bits total)
152@end example
153
154 Many of the functions described in this chapter accept markers for 144 Many of the functions described in this chapter accept markers for
155arguments in place of numbers. (@xref{Markers}.) Since the actual 145arguments in place of numbers. (@xref{Markers}.) Since the actual
156arguments to such functions may be either numbers or markers, we often 146arguments to such functions may be either numbers or markers, we often
@@ -160,8 +150,8 @@ value is a marker, its position value is used and its buffer is ignored.
160@cindex largest Lisp integer 150@cindex largest Lisp integer
161@cindex maximum Lisp integer 151@cindex maximum Lisp integer
162@defvar most-positive-fixnum 152@defvar most-positive-fixnum
163The value of this variable is the largest integer that Emacs Lisp can 153The value of this variable is the largest ``small'' integer that Emacs
164handle. Typical values are 154Lisp can handle. Typical values are
165@ifnottex 155@ifnottex
1662**29 @minus{} 1 1562**29 @minus{} 1
167@end ifnottex 157@end ifnottex
@@ -181,8 +171,8 @@ on 64-bit platforms.
181@cindex smallest Lisp integer 171@cindex smallest Lisp integer
182@cindex minimum Lisp integer 172@cindex minimum Lisp integer
183@defvar most-negative-fixnum 173@defvar most-negative-fixnum
184The value of this variable is the smallest integer that Emacs Lisp can 174The value of this variable is the smallest small integer that Emacs
185handle. It is negative. Typical values are 175Lisp can handle. It is negative. Typical values are
186@ifnottex 176@ifnottex
187@minus{}2**29 177@minus{}2**29
188@end ifnottex 178@end ifnottex
@@ -315,6 +305,20 @@ use otherwise), but the @code{zerop} predicate requires a number as
315its argument. See also @code{integer-or-marker-p} and 305its argument. See also @code{integer-or-marker-p} and
316@code{number-or-marker-p}, in @ref{Predicates on Markers}. 306@code{number-or-marker-p}, in @ref{Predicates on Markers}.
317 307
308@defun bignump object
309This predicate tests whether its argument is a large integer, and
310returns @code{t} if so, @code{nil} otherwise. Large integers cannot
311be compared with @code{eq}, only with @code{=} or @code{eql}. Also,
312large integers are only available if Emacs was compiled with the GMP
313library.
314@end defun
315
316@defun fixnump object
317This predicate tests whether its argument is a small integer, and
318returns @code{t} if so, @code{nil} otherwise. Small integers can be
319compared with @code{eq}.
320@end defun
321
318@defun floatp object 322@defun floatp object
319This predicate tests whether its argument is floating point 323This predicate tests whether its argument is floating point
320and returns @code{t} if so, @code{nil} otherwise. 324and returns @code{t} if so, @code{nil} otherwise.
@@ -355,13 +359,13 @@ if so, @code{nil} otherwise. The argument must be a number.
355 359
356 To test numbers for numerical equality, you should normally use 360 To test numbers for numerical equality, you should normally use
357@code{=}, not @code{eq}. There can be many distinct floating-point 361@code{=}, not @code{eq}. There can be many distinct floating-point
358objects with the same numeric value. If you use @code{eq} to 362and large integer objects with the same numeric value. If you use
359compare them, then you test whether two values are the same 363@code{eq} to compare them, then you test whether two values are the
360@emph{object}. By contrast, @code{=} compares only the numeric values 364same @emph{object}. By contrast, @code{=} compares only the numeric
361of the objects. 365values of the objects.
362 366
363 In Emacs Lisp, each integer is a unique Lisp object. 367 In Emacs Lisp, each small integer is a unique Lisp object.
364Therefore, @code{eq} is equivalent to @code{=} where integers are 368Therefore, @code{eq} is equivalent to @code{=} where small integers are
365concerned. It is sometimes convenient to use @code{eq} for comparing 369concerned. It is sometimes convenient to use @code{eq} for comparing
366an unknown value with an integer, because @code{eq} does not report an 370an unknown value with an integer, because @code{eq} does not report an
367error if the unknown value is not a number---it accepts arguments of 371error if the unknown value is not a number---it accepts arguments of
@@ -389,15 +393,6 @@ Here's a function to do this:
389 fuzz-factor))) 393 fuzz-factor)))
390@end example 394@end example
391 395
392@cindex CL note---integers vrs @code{eq}
393@quotation
394@b{Common Lisp note:} Comparing numbers in Common Lisp always requires
395@code{=} because Common Lisp implements multi-word integers, and two
396distinct integer objects can have the same numeric value. Emacs Lisp
397can have just one integer object for any given value because it has a
398limited range of integers.
399@end quotation
400
401@defun = number-or-marker &rest number-or-markers 396@defun = number-or-marker &rest number-or-markers
402This function tests whether all its arguments are numerically equal, 397This function tests whether all its arguments are numerically equal,
403and returns @code{t} if so, @code{nil} otherwise. 398and returns @code{t} if so, @code{nil} otherwise.
@@ -407,7 +402,8 @@ and returns @code{t} if so, @code{nil} otherwise.
407This function acts like @code{eq} except when both arguments are 402This function acts like @code{eq} except when both arguments are
408numbers. It compares numbers by type and numeric value, so that 403numbers. It compares numbers by type and numeric value, so that
409@code{(eql 1.0 1)} returns @code{nil}, but @code{(eql 1.0 1.0)} and 404@code{(eql 1.0 1)} returns @code{nil}, but @code{(eql 1.0 1.0)} and
410@code{(eql 1 1)} both return @code{t}. 405@code{(eql 1 1)} both return @code{t}. This can be used to compare
406large integers as well as small ones.
411@end defun 407@end defun
412 408
413@defun /= number-or-marker1 number-or-marker2 409@defun /= number-or-marker1 number-or-marker2
@@ -567,10 +563,6 @@ Except for @code{%}, each of these functions accepts both integer and
567floating-point arguments, and returns a floating-point number if any 563floating-point arguments, and returns a floating-point number if any
568argument is floating point. 564argument is floating point.
569 565
570 Emacs Lisp arithmetic functions do not check for integer overflow.
571Thus @code{(1+ 536870911)} may evaluate to
572@minus{}536870912, depending on your hardware.
573
574@defun 1+ number-or-marker 566@defun 1+ number-or-marker
575This function returns @var{number-or-marker} plus 1. 567This function returns @var{number-or-marker} plus 1.
576For example, 568For example,
@@ -897,36 +889,6 @@ On the other hand, shifting one place to the right looks like this:
897As the example illustrates, shifting one place to the right divides the 889As the example illustrates, shifting one place to the right divides the
898value of a positive integer by two, rounding downward. 890value of a positive integer by two, rounding downward.
899 891
900The function @code{lsh}, like all Emacs Lisp arithmetic functions, does
901not check for overflow, so shifting left can discard significant bits
902and change the sign of the number. For example, left shifting
903536,870,911 produces @minus{}2 in the 30-bit implementation:
904
905@example
906(lsh 536870911 1) ; @r{left shift}
907 @result{} -2
908@end example
909
910In binary, the argument looks like this:
911
912@example
913@group
914;; @r{Decimal 536,870,911}
9150111...111111 (30 bits total)
916@end group
917@end example
918
919@noindent
920which becomes the following when left shifted:
921
922@example
923@group
924;; @r{Decimal @minus{}2}
9251111...111110 (30 bits total)
926@end group
927@end example
928@end defun
929
930@defun ash integer1 count 892@defun ash integer1 count
931@cindex arithmetic shift 893@cindex arithmetic shift
932@code{ash} (@dfn{arithmetic shift}) shifts the bits in @var{integer1} 894@code{ash} (@dfn{arithmetic shift}) shifts the bits in @var{integer1}
@@ -951,19 +913,6 @@ looks like this:
951@end group 913@end group
952@end example 914@end example
953 915
954In contrast, shifting the pattern of bits one place to the right with
955@code{lsh} looks like this:
956
957@example
958@group
959(lsh -6 -1) @result{} 536870909
960;; @r{Decimal @minus{}6 becomes decimal 536,870,909.}
9611111...111010 (30 bits total)
962 @result{}
9630111...111101 (30 bits total)
964@end group
965@end example
966
967Here are other examples: 916Here are other examples:
968 917
969@c !!! Check if lined up in smallbook format! XDVI shows problem 918@c !!! Check if lined up in smallbook format! XDVI shows problem
diff --git a/doc/lispref/objects.texi b/doc/lispref/objects.texi
index b94de80b658..8c92de123c2 100644
--- a/doc/lispref/objects.texi
+++ b/doc/lispref/objects.texi
@@ -166,7 +166,10 @@ latter are unique to Emacs Lisp.
166@node Integer Type 166@node Integer Type
167@subsection Integer Type 167@subsection Integer Type
168 168
169 The range of values for an integer depends on the machine. The 169 Under the hood, there are two kinds of integers---small integers,
170called @dfn{fixnums}, and large integers, called @dfn{bignums}.
171
172 The range of values for a fixnum depends on the machine. The
170minimum range is @minus{}536,870,912 to 536,870,911 (30 bits; i.e., 173minimum range is @minus{}536,870,912 to 536,870,911 (30 bits; i.e.,
171@ifnottex 174@ifnottex
172@minus{}2**29 175@minus{}2**29
@@ -182,8 +185,14 @@ to
182@math{2^{29}-1}) 185@math{2^{29}-1})
183@end tex 186@end tex
184but many machines provide a wider range. 187but many machines provide a wider range.
185Emacs Lisp arithmetic functions do not check for integer overflow. Thus 188
186@code{(1+ 536870911)} is @minus{}536,870,912 if Emacs integers are 30 bits. 189 Bignums can have arbitrary precision. Operations that overflow a
190fixnum will return a bignum instead.
191
192 Fixnums can be compared with @code{eq}, but bignums require
193@code{eql} or @code{=}. The @code{fixnump} predicate can be used to
194detect such small integers, and @code{bignump} can be used to detect
195large integers.
187 196
188 The read syntax for integers is a sequence of (base ten) digits with an 197 The read syntax for integers is a sequence of (base ten) digits with an
189optional sign at the beginning and an optional period at the end. The 198optional sign at the beginning and an optional period at the end. The
@@ -200,11 +209,6 @@ leading @samp{+} or a final @samp{.}.
200@end example 209@end example
201 210
202@noindent 211@noindent
203As a special exception, if a sequence of digits specifies an integer
204too large or too small to be a valid integer object, the Lisp reader
205reads it as a floating-point number (@pxref{Floating-Point Type}).
206For instance, if Emacs integers are 30 bits, @code{536870912} is read
207as the floating-point number @code{536870912.0}.
208 212
209 @xref{Numbers}, for more information. 213 @xref{Numbers}, for more information.
210 214
@@ -1895,6 +1899,9 @@ with references to further information.
1895@item arrayp 1899@item arrayp
1896@xref{Array Functions, arrayp}. 1900@xref{Array Functions, arrayp}.
1897 1901
1902@item bignump
1903@xref{Predicates on Numbers, floatp}.
1904
1898@item bool-vector-p 1905@item bool-vector-p
1899@xref{Bool-Vectors, bool-vector-p}. 1906@xref{Bool-Vectors, bool-vector-p}.
1900 1907
@@ -1928,6 +1935,9 @@ with references to further information.
1928@item custom-variable-p 1935@item custom-variable-p
1929@xref{Variable Definitions, custom-variable-p}. 1936@xref{Variable Definitions, custom-variable-p}.
1930 1937
1938@item fixnump
1939@xref{Predicates on Numbers, floatp}.
1940
1931@item floatp 1941@item floatp
1932@xref{Predicates on Numbers, floatp}. 1942@xref{Predicates on Numbers, floatp}.
1933 1943