diff options
| author | Tom Tromey | 2018-07-08 23:10:53 -0600 |
|---|---|---|
| committer | Tom Tromey | 2018-07-12 22:12:48 -0600 |
| commit | cc3d7580fc1cab3119e5e05c427575a2668cbb4f (patch) | |
| tree | 55b0373e813f0f1d2530b23699f6c583892e8643 /doc | |
| parent | e2a78b0d6d844f29acaaddd775c7b1cd6dec7af8 (diff) | |
| download | emacs-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.texi | 133 | ||||
| -rw-r--r-- | doc/lispref/objects.texi | 26 |
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 @@ | |||
| 14 | fractional parts, such as @minus{}4.5, 0.0, and 2.71828. They can | 14 | fractional parts, such as @minus{}4.5, 0.0, and 2.71828. They can |
| 15 | also be expressed in exponential notation: @samp{1.5e2} is the same as | 15 | also 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 |
| 17 | that is multiplied by 1.5. Integer computations are exact, though | 17 | that is multiplied by 1.5. Integer computations are exact. |
| 18 | they may overflow. Floating-point computations often involve rounding | 18 | Floating-point computations often involve rounding errors, as the |
| 19 | errors, as the numbers have a fixed amount of precision. | 19 | numbers 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 | ||
| 40 | ones, called @dfn{fixnums}, and larger ones, called @dfn{bignums} | ||
| 41 | Some functions in Emacs only accept fixnums. Also, while fixnums can | ||
| 42 | always be compared for equality with @code{eq}, bignums require the | ||
| 43 | use of @code{eql}. | ||
| 44 | |||
| 45 | The range of values for a fixnum depends on the machine. The | ||
| 38 | minimum range is @minus{}536,870,912 to 536,870,911 (30 bits; i.e., | 46 | minimum 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 |
| 52 | but many machines provide a wider range. Many examples in this | 60 | but many machines provide a wider range. |
| 53 | chapter 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 |
| 57 | of decimal digits with optional initial sign and optional | 63 | of 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. |
| 95 | signals an overflow. However, if a too-large plain integer ends in a | 101 | Otherwise, it will be read as a bignum. |
| 96 | period, the Lisp reader treats it as a floating-point number instead. | ||
| 97 | This lets an Emacs Lisp program specify a large integer that is | ||
| 98 | quietly approximated by a floating-point number on machines with | ||
| 99 | limited word width. For example, @samp{536870912.} is a | ||
| 100 | floating-point number if Emacs integers are only 30 bits wide and is | ||
| 101 | an 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 |
| 104 | bitwise operators (@pxref{Bitwise Operations}), it is often helpful to | 104 | bitwise operators (@pxref{Bitwise Operations}), it is often helpful to |
| @@ -141,16 +141,6 @@ In binary, the decimal integer 4 is 100. Consequently, | |||
| 141 | 0111...111111 (30 bits total) | 141 | 0111...111111 (30 bits total) |
| 142 | @end example | 142 | @end example |
| 143 | 143 | ||
| 144 | Since the arithmetic functions do not check whether integers go | ||
| 145 | outside their range, when you add 1 to 536,870,911, the value is the | ||
| 146 | negative 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 |
| 155 | arguments in place of numbers. (@xref{Markers}.) Since the actual | 145 | arguments in place of numbers. (@xref{Markers}.) Since the actual |
| 156 | arguments to such functions may be either numbers or markers, we often | 146 | arguments 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 |
| 163 | The value of this variable is the largest integer that Emacs Lisp can | 153 | The value of this variable is the largest ``small'' integer that Emacs |
| 164 | handle. Typical values are | 154 | Lisp can handle. Typical values are |
| 165 | @ifnottex | 155 | @ifnottex |
| 166 | 2**29 @minus{} 1 | 156 | 2**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 |
| 184 | The value of this variable is the smallest integer that Emacs Lisp can | 174 | The value of this variable is the smallest small integer that Emacs |
| 185 | handle. It is negative. Typical values are | 175 | Lisp 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 | |||
| 315 | its argument. See also @code{integer-or-marker-p} and | 305 | its 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 | ||
| 309 | This predicate tests whether its argument is a large integer, and | ||
| 310 | returns @code{t} if so, @code{nil} otherwise. Large integers cannot | ||
| 311 | be compared with @code{eq}, only with @code{=} or @code{eql}. Also, | ||
| 312 | large integers are only available if Emacs was compiled with the GMP | ||
| 313 | library. | ||
| 314 | @end defun | ||
| 315 | |||
| 316 | @defun fixnump object | ||
| 317 | This predicate tests whether its argument is a small integer, and | ||
| 318 | returns @code{t} if so, @code{nil} otherwise. Small integers can be | ||
| 319 | compared with @code{eq}. | ||
| 320 | @end defun | ||
| 321 | |||
| 318 | @defun floatp object | 322 | @defun floatp object |
| 319 | This predicate tests whether its argument is floating point | 323 | This predicate tests whether its argument is floating point |
| 320 | and returns @code{t} if so, @code{nil} otherwise. | 324 | and 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 |
| 358 | objects with the same numeric value. If you use @code{eq} to | 362 | and large integer objects with the same numeric value. If you use |
| 359 | compare 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 | 364 | same @emph{object}. By contrast, @code{=} compares only the numeric |
| 361 | of the objects. | 365 | values 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. |
| 364 | Therefore, @code{eq} is equivalent to @code{=} where integers are | 368 | Therefore, @code{eq} is equivalent to @code{=} where small integers are |
| 365 | concerned. It is sometimes convenient to use @code{eq} for comparing | 369 | concerned. It is sometimes convenient to use @code{eq} for comparing |
| 366 | an unknown value with an integer, because @code{eq} does not report an | 370 | an unknown value with an integer, because @code{eq} does not report an |
| 367 | error if the unknown value is not a number---it accepts arguments of | 371 | error 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 | ||
| 396 | distinct integer objects can have the same numeric value. Emacs Lisp | ||
| 397 | can have just one integer object for any given value because it has a | ||
| 398 | limited 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 |
| 402 | This function tests whether all its arguments are numerically equal, | 397 | This function tests whether all its arguments are numerically equal, |
| 403 | and returns @code{t} if so, @code{nil} otherwise. | 398 | and returns @code{t} if so, @code{nil} otherwise. |
| @@ -407,7 +402,8 @@ and returns @code{t} if so, @code{nil} otherwise. | |||
| 407 | This function acts like @code{eq} except when both arguments are | 402 | This function acts like @code{eq} except when both arguments are |
| 408 | numbers. It compares numbers by type and numeric value, so that | 403 | numbers. 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 |
| 406 | large 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 | |||
| 567 | floating-point arguments, and returns a floating-point number if any | 563 | floating-point arguments, and returns a floating-point number if any |
| 568 | argument is floating point. | 564 | argument is floating point. |
| 569 | 565 | ||
| 570 | Emacs Lisp arithmetic functions do not check for integer overflow. | ||
| 571 | Thus @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 |
| 575 | This function returns @var{number-or-marker} plus 1. | 567 | This function returns @var{number-or-marker} plus 1. |
| 576 | For example, | 568 | For example, |
| @@ -897,36 +889,6 @@ On the other hand, shifting one place to the right looks like this: | |||
| 897 | As the example illustrates, shifting one place to the right divides the | 889 | As the example illustrates, shifting one place to the right divides the |
| 898 | value of a positive integer by two, rounding downward. | 890 | value of a positive integer by two, rounding downward. |
| 899 | 891 | ||
| 900 | The function @code{lsh}, like all Emacs Lisp arithmetic functions, does | ||
| 901 | not check for overflow, so shifting left can discard significant bits | ||
| 902 | and change the sign of the number. For example, left shifting | ||
| 903 | 536,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 | |||
| 910 | In binary, the argument looks like this: | ||
| 911 | |||
| 912 | @example | ||
| 913 | @group | ||
| 914 | ;; @r{Decimal 536,870,911} | ||
| 915 | 0111...111111 (30 bits total) | ||
| 916 | @end group | ||
| 917 | @end example | ||
| 918 | |||
| 919 | @noindent | ||
| 920 | which becomes the following when left shifted: | ||
| 921 | |||
| 922 | @example | ||
| 923 | @group | ||
| 924 | ;; @r{Decimal @minus{}2} | ||
| 925 | 1111...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 | ||
| 954 | In 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.} | ||
| 961 | 1111...111010 (30 bits total) | ||
| 962 | @result{} | ||
| 963 | 0111...111101 (30 bits total) | ||
| 964 | @end group | ||
| 965 | @end example | ||
| 966 | |||
| 967 | Here are other examples: | 916 | Here 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, |
| 170 | called @dfn{fixnums}, and large integers, called @dfn{bignums}. | ||
| 171 | |||
| 172 | The range of values for a fixnum depends on the machine. The | ||
| 170 | minimum range is @minus{}536,870,912 to 536,870,911 (30 bits; i.e., | 173 | minimum 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 |
| 184 | but many machines provide a wider range. | 187 | but many machines provide a wider range. |
| 185 | Emacs 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 |
| 190 | fixnum 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 | ||
| 194 | detect such small integers, and @code{bignump} can be used to detect | ||
| 195 | large 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 |
| 189 | optional sign at the beginning and an optional period at the end. The | 198 | optional 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 |
| 203 | As a special exception, if a sequence of digits specifies an integer | ||
| 204 | too large or too small to be a valid integer object, the Lisp reader | ||
| 205 | reads it as a floating-point number (@pxref{Floating-Point Type}). | ||
| 206 | For instance, if Emacs integers are 30 bits, @code{536870912} is read | ||
| 207 | as 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 | ||