diff options
| author | Tom Tromey | 2018-08-11 13:34:17 -0600 |
|---|---|---|
| committer | Tom Tromey | 2018-08-11 13:34:17 -0600 |
| commit | 78ec68e18f07a90a9ad400683b973ff51baa80e1 (patch) | |
| tree | 638c986bf753e3ddab9992ba1ef0a10a3d4891f0 /doc | |
| parent | ba1c4f63e3d2adbe9b590a3c51c2a0808c84723f (diff) | |
| parent | 79f59d41a3d2ef3b4a9a87265bf517206a5837ad (diff) | |
| download | emacs-78ec68e18f07a90a9ad400683b973ff51baa80e1.tar.gz emacs-78ec68e18f07a90a9ad400683b973ff51baa80e1.zip | |
Merge branch 'feature/bignum'
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/lispref/numbers.texi | 132 | ||||
| -rw-r--r-- | doc/lispref/objects.texi | 26 |
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 @@ | |||
| 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 |
| @@ -327,6 +317,20 @@ use otherwise), but the @code{zerop} predicate requires a number as | |||
| 327 | its argument. See also @code{integer-or-marker-p} and | 317 | its 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 | ||
| 321 | This predicate tests whether its argument is a large integer, and | ||
| 322 | returns @code{t} if so, @code{nil} otherwise. Large integers cannot | ||
| 323 | be compared with @code{eq}, only with @code{=} or @code{eql}. Also, | ||
| 324 | large integers are only available if Emacs was compiled with the GMP | ||
| 325 | library. | ||
| 326 | @end defun | ||
| 327 | |||
| 328 | @defun fixnump object | ||
| 329 | This predicate tests whether its argument is a small integer, and | ||
| 330 | returns @code{t} if so, @code{nil} otherwise. Small integers can be | ||
| 331 | compared with @code{eq}. | ||
| 332 | @end defun | ||
| 333 | |||
| 330 | @defun floatp object | 334 | @defun floatp object |
| 331 | This predicate tests whether its argument is floating point | 335 | This predicate tests whether its argument is floating point |
| 332 | and returns @code{t} if so, @code{nil} otherwise. | 336 | and 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 |
| 371 | numerically equal. If you use @code{eq} to compare them, you test | 375 | integer objects can be numerically equal. If you use @code{eq} to |
| 372 | whether they are the same @emph{object}; if you use @code{eql} or | 376 | compare them, you test whether they are the same @emph{object}; if you |
| 373 | @code{equal}, you test whether their values are | 377 | use @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 |
| 375 | comparison, and sometimes returns @code{t} when a non-numeric | 379 | comparison, and sometimes returns @code{t} when a non-numeric |
| 376 | comparison would return @code{nil} and vice versa. @xref{Float | 380 | comparison would return @code{nil} and vice versa. @xref{Float |
| 377 | Basics}. | 381 | Basics}. |
| 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. |
| 380 | Therefore, @code{eq} is equivalent to @code{=} where integers are | 384 | Therefore, @code{eq} is equivalent to @code{=} where small integers are |
| 381 | concerned. It is sometimes convenient to use @code{eq} for comparing | 385 | concerned. It is sometimes convenient to use @code{eq} for comparing |
| 382 | an unknown value with an integer, because @code{eq} does not report an | 386 | an unknown value with an integer, because @code{eq} does not report an |
| 383 | error if the unknown value is not a number---it accepts arguments of | 387 | error 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 | ||
| 412 | distinct integer objects can have the same numeric value. Emacs Lisp | ||
| 413 | can have just one integer object for any given value because it has a | ||
| 414 | limited 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 |
| 418 | This function tests whether all its arguments are numerically equal, | 413 | This function tests whether all its arguments are numerically equal, |
| 419 | and returns @code{t} if so, @code{nil} otherwise. | 414 | and returns @code{t} if so, @code{nil} otherwise. |
| @@ -423,7 +418,8 @@ and returns @code{t} if so, @code{nil} otherwise. | |||
| 423 | This function acts like @code{eq} except when both arguments are | 418 | This function acts like @code{eq} except when both arguments are |
| 424 | numbers. It compares numbers by type and numeric value, so that | 419 | numbers. 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 |
| 422 | large 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 | |||
| 583 | floating-point arguments, and returns a floating-point number if any | 579 | floating-point arguments, and returns a floating-point number if any |
| 584 | argument is floating point. | 580 | argument is floating point. |
| 585 | 581 | ||
| 586 | Emacs Lisp arithmetic functions do not check for integer overflow. | ||
| 587 | Thus @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 |
| 591 | This function returns @var{number-or-marker} plus 1. | 583 | This function returns @var{number-or-marker} plus 1. |
| 592 | For example, | 584 | For example, |
| @@ -912,35 +904,6 @@ On the other hand, shifting one place to the right looks like this: | |||
| 912 | @noindent | 904 | @noindent |
| 913 | As the example illustrates, shifting one place to the right divides the | 905 | As the example illustrates, shifting one place to the right divides the |
| 914 | value of a positive integer by two, rounding downward. | 906 | value of a positive integer by two, rounding downward. |
| 915 | |||
| 916 | The function @code{lsh}, like all Emacs Lisp arithmetic functions, does | ||
| 917 | not check for overflow, so shifting left can discard significant bits | ||
| 918 | and change the sign of the number. For example, left shifting | ||
| 919 | 536,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 | |||
| 926 | In binary, the argument looks like this: | ||
| 927 | |||
| 928 | @example | ||
| 929 | @group | ||
| 930 | ;; @r{Decimal 536,870,911} | ||
| 931 | 0111...111111 (30 bits total) | ||
| 932 | @end group | ||
| 933 | @end example | ||
| 934 | |||
| 935 | @noindent | ||
| 936 | which becomes the following when left shifted: | ||
| 937 | |||
| 938 | @example | ||
| 939 | @group | ||
| 940 | ;; @r{Decimal @minus{}2} | ||
| 941 | 1111...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 | ||
| 970 | In 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.} | ||
| 977 | 1111...111010 (30 bits total) | ||
| 978 | @result{} | ||
| 979 | 0111...111101 (30 bits total) | ||
| 980 | @end group | ||
| 981 | @end example | ||
| 982 | |||
| 983 | Here are other examples: | 933 | Here 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, |
| 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 | ||