aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorTom Tromey2018-08-11 13:34:17 -0600
committerTom Tromey2018-08-11 13:34:17 -0600
commit78ec68e18f07a90a9ad400683b973ff51baa80e1 (patch)
tree638c986bf753e3ddab9992ba1ef0a10a3d4891f0 /doc
parentba1c4f63e3d2adbe9b590a3c51c2a0808c84723f (diff)
parent79f59d41a3d2ef3b4a9a87265bf517206a5837ad (diff)
downloademacs-78ec68e18f07a90a9ad400683b973ff51baa80e1.tar.gz
emacs-78ec68e18f07a90a9ad400683b973ff51baa80e1.zip
Merge branch 'feature/bignum'
Diffstat (limited to 'doc')
-rw-r--r--doc/lispref/numbers.texi132
-rw-r--r--doc/lispref/objects.texi26
2 files changed, 59 insertions, 99 deletions
diff --git a/doc/lispref/numbers.texi b/doc/lispref/numbers.texi
index a3317c9a260..89205f9df39 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
@@ -327,6 +317,20 @@ use otherwise), but the @code{zerop} predicate requires a number as
327its argument. See also @code{integer-or-marker-p} and 317its argument. See also @code{integer-or-marker-p} and
328@code{number-or-marker-p}, in @ref{Predicates on Markers}. 318@code{number-or-marker-p}, in @ref{Predicates on Markers}.
329 319
320@defun bignump object
321This predicate tests whether its argument is a large integer, and
322returns @code{t} if so, @code{nil} otherwise. Large integers cannot
323be compared with @code{eq}, only with @code{=} or @code{eql}. Also,
324large integers are only available if Emacs was compiled with the GMP
325library.
326@end defun
327
328@defun fixnump object
329This predicate tests whether its argument is a small integer, and
330returns @code{t} if so, @code{nil} otherwise. Small integers can be
331compared with @code{eq}.
332@end defun
333
330@defun floatp object 334@defun floatp object
331This predicate tests whether its argument is floating point 335This predicate tests whether its argument is floating point
332and returns @code{t} if so, @code{nil} otherwise. 336and returns @code{t} if so, @code{nil} otherwise.
@@ -367,17 +371,17 @@ if so, @code{nil} otherwise. The argument must be a number.
367 371
368 To test numbers for numerical equality, you should normally use 372 To test numbers for numerical equality, you should normally use
369@code{=} instead of non-numeric comparison predicates like @code{eq}, 373@code{=} instead of non-numeric comparison predicates like @code{eq},
370@code{eql} and @code{equal}. Distinct floating-point objects can be 374@code{eql} and @code{equal}. Distinct floating-point and large
371numerically equal. If you use @code{eq} to compare them, you test 375integer objects can be numerically equal. If you use @code{eq} to
372whether they are the same @emph{object}; if you use @code{eql} or 376compare them, you test whether they are the same @emph{object}; if you
373@code{equal}, you test whether their values are 377use @code{eql} or @code{equal}, you test whether their values are
374@emph{indistinguishable}. In contrast, @code{=} uses numeric 378@emph{indistinguishable}. In contrast, @code{=} uses numeric
375comparison, and sometimes returns @code{t} when a non-numeric 379comparison, and sometimes returns @code{t} when a non-numeric
376comparison would return @code{nil} and vice versa. @xref{Float 380comparison would return @code{nil} and vice versa. @xref{Float
377Basics}. 381Basics}.
378 382
379 In Emacs Lisp, each integer is a unique Lisp object. 383 In Emacs Lisp, each small integer is a unique Lisp object.
380Therefore, @code{eq} is equivalent to @code{=} where integers are 384Therefore, @code{eq} is equivalent to @code{=} where small integers are
381concerned. It is sometimes convenient to use @code{eq} for comparing 385concerned. It is sometimes convenient to use @code{eq} for comparing
382an unknown value with an integer, because @code{eq} does not report an 386an unknown value with an integer, because @code{eq} does not report an
383error if the unknown value is not a number---it accepts arguments of 387error if the unknown value is not a number---it accepts arguments of
@@ -405,15 +409,6 @@ Here's a function to do this:
405 fuzz-factor))) 409 fuzz-factor)))
406@end example 410@end example
407 411
408@cindex CL note---integers vrs @code{eq}
409@quotation
410@b{Common Lisp note:} Comparing numbers in Common Lisp always requires
411@code{=} because Common Lisp implements multi-word integers, and two
412distinct integer objects can have the same numeric value. Emacs Lisp
413can have just one integer object for any given value because it has a
414limited range of integers.
415@end quotation
416
417@defun = number-or-marker &rest number-or-markers 412@defun = number-or-marker &rest number-or-markers
418This function tests whether all its arguments are numerically equal, 413This function tests whether all its arguments are numerically equal,
419and returns @code{t} if so, @code{nil} otherwise. 414and returns @code{t} if so, @code{nil} otherwise.
@@ -423,7 +418,8 @@ and returns @code{t} if so, @code{nil} otherwise.
423This function acts like @code{eq} except when both arguments are 418This function acts like @code{eq} except when both arguments are
424numbers. It compares numbers by type and numeric value, so that 419numbers. It compares numbers by type and numeric value, so that
425@code{(eql 1.0 1)} returns @code{nil}, but @code{(eql 1.0 1.0)} and 420@code{(eql 1.0 1)} returns @code{nil}, but @code{(eql 1.0 1.0)} and
426@code{(eql 1 1)} both return @code{t}. 421@code{(eql 1 1)} both return @code{t}. This can be used to compare
422large integers as well as small ones.
427@end defun 423@end defun
428 424
429@defun /= number-or-marker1 number-or-marker2 425@defun /= number-or-marker1 number-or-marker2
@@ -583,10 +579,6 @@ Except for @code{%}, each of these functions accepts both integer and
583floating-point arguments, and returns a floating-point number if any 579floating-point arguments, and returns a floating-point number if any
584argument is floating point. 580argument is floating point.
585 581
586 Emacs Lisp arithmetic functions do not check for integer overflow.
587Thus @code{(1+ 536870911)} may evaluate to
588@minus{}536870912, depending on your hardware.
589
590@defun 1+ number-or-marker 582@defun 1+ number-or-marker
591This function returns @var{number-or-marker} plus 1. 583This function returns @var{number-or-marker} plus 1.
592For example, 584For example,
@@ -912,35 +904,6 @@ On the other hand, shifting one place to the right looks like this:
912@noindent 904@noindent
913As the example illustrates, shifting one place to the right divides the 905As the example illustrates, shifting one place to the right divides the
914value of a positive integer by two, rounding downward. 906value of a positive integer by two, rounding downward.
915
916The function @code{lsh}, like all Emacs Lisp arithmetic functions, does
917not check for overflow, so shifting left can discard significant bits
918and change the sign of the number. For example, left shifting
919536,870,911 produces @minus{}2 in the 30-bit implementation:
920
921@example
922(lsh 536870911 1) ; @r{left shift}
923 @result{} -2
924@end example
925
926In binary, the argument looks like this:
927
928@example
929@group
930;; @r{Decimal 536,870,911}
9310111...111111 (30 bits total)
932@end group
933@end example
934
935@noindent
936which becomes the following when left shifted:
937
938@example
939@group
940;; @r{Decimal @minus{}2}
9411111...111110 (30 bits total)
942@end group
943@end example
944@end defun 907@end defun
945 908
946@defun ash integer1 count 909@defun ash integer1 count
@@ -967,19 +930,6 @@ looks like this:
967@end group 930@end group
968@end example 931@end example
969 932
970In contrast, shifting the pattern of bits one place to the right with
971@code{lsh} looks like this:
972
973@example
974@group
975(lsh -6 -1) @result{} 536870909
976;; @r{Decimal @minus{}6 becomes decimal 536,870,909.}
9771111...111010 (30 bits total)
978 @result{}
9790111...111101 (30 bits total)
980@end group
981@end example
982
983Here are other examples: 933Here are other examples:
984 934
985@c !!! Check if lined up in smallbook format! XDVI shows problem 935@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