summaryrefslogtreecommitdiff
path: root/devdocs/elisp/integer-basics.html
blob: 85ef6a385a7f160885c5dbaa16100729252ba627 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 <h3 class="section">Integer Basics</h3> <p>The Lisp reader reads an integer as a nonempty sequence of decimal digits with optional initial sign and optional final period. </p> <div class="example"> <pre class="example"> 1               ; <span class="roman">The integer 1.</span>
 1.              ; <span class="roman">The integer 1.</span>
+1               ; <span class="roman">Also the integer 1.</span>
-1               ; <span class="roman">The integer -1.</span>
 0               ; <span class="roman">The integer 0.</span>
-0               ; <span class="roman">The integer 0.</span>
</pre>
</div>       <p>The syntax for integers in bases other than 10 consists of ‘<samp>#</samp>’ followed by a radix indication followed by one or more digits. The radix indications are ‘<samp>b</samp>’ for binary, ‘<samp>o</samp>’ for octal, ‘<samp>x</samp>’ for hex, and ‘<samp><var>radix</var>r</samp>’ for radix <var>radix</var>. Thus, ‘<samp>#b<var>integer</var></samp>’ reads <var>integer</var> in binary, and ‘<samp>#<var>radix</var>r<var>integer</var></samp>’ reads <var>integer</var> in radix <var>radix</var>. Allowed values of <var>radix</var> run from 2 to 36, and allowed digits are the first <var>radix</var> characters taken from ‘<samp>0</samp>’–‘<samp>9</samp>’, ‘<samp>A</samp>’–‘<samp>Z</samp>’. Letter case is ignored and there is no initial sign or final period. For example: </p> <div class="example"> <pre class="example">#b101100 ⇒ 44
#o54 ⇒ 44
#x2c ⇒ 44
#24r1k ⇒ 44
</pre>
</div> <p>To understand how various functions work on integers, especially the bitwise operators (see <a href="bitwise-operations">Bitwise Operations</a>), it is often helpful to view the numbers in their binary form. </p> <p>In binary, the decimal integer 5 looks like this: </p> <div class="example"> <pre class="example">…000101
</pre>
</div> <p>(The ellipsis ‘<samp>…</samp>’ stands for a conceptually infinite number of bits that match the leading bit; here, an infinite number of 0 bits. Later examples also use this ‘<samp>…</samp>’ notation.) </p> <p>The integer -1 looks like this: </p> <div class="example"> <pre class="example">…111111
</pre>
</div> <p> -1 is represented as all ones. (This is called <em>two’s complement</em> notation.) </p> <p>Subtracting 4 from -1 returns the negative integer -5. In binary, the decimal integer 4 is 100. Consequently, -5 looks like this: </p> <div class="example"> <pre class="example">…111011
</pre>
</div> <p>Many of the functions described in this chapter accept markers for arguments in place of numbers. (See <a href="markers">Markers</a>.) Since the actual arguments to such functions may be either numbers or markers, we often give these arguments the name <var>number-or-marker</var>. When the argument value is a marker, its position value is used and its buffer is ignored. </p> <p>In Emacs Lisp, text characters are represented by integers. Any integer between zero and the value of <code>(max-char)</code>, inclusive, is considered to be valid as a character. See <a href="character-codes">Character Codes</a>. </p> <p>Integers in Emacs Lisp are not limited to the machine word size. Under the hood, though, there are two kinds of integers: smaller ones, called <em>fixnums</em>, and larger ones, called <em>bignums</em>. Although Emacs Lisp code ordinarily should not depend on whether an integer is a fixnum or a bignum, older Emacs versions support only fixnums, some functions in Emacs still accept only fixnums, and older Emacs Lisp code may have trouble when given bignums. For example, while older Emacs Lisp code could safely compare integers for numeric equality with <code>eq</code>, the presence of bignums means that equality predicates like <code>eql</code> and <code>=</code> should now be used to compare integers. </p> <p>The range of values for bignums is limited by the amount of main memory, by machine characteristics such as the size of the word used to represent a bignum’s exponent, and by the <code>integer-width</code> variable. These limits are typically much more generous than the limits for fixnums. A bignum is never numerically equal to a fixnum; Emacs always represents an integer in fixnum range as a fixnum, not a bignum. </p> <p>The range of values for a fixnum depends on the machine. The minimum range is -536,870,912 to 536,870,911 (30 bits; i.e., -2**29 to 2**29 - 1), but many machines provide a wider range. </p>   <dl> <dt id="most-positive-fixnum">Variable: <strong>most-positive-fixnum</strong>
</dt> <dd><p>The value of this variable is the greatest “small” integer that Emacs Lisp can handle. Typical values are 2**29 - 1 on 32-bit and 2**61 - 1 on 64-bit platforms. </p></dd>
</dl>   <dl> <dt id="most-negative-fixnum">Variable: <strong>most-negative-fixnum</strong>
</dt> <dd><p>The value of this variable is the numerically least “small” integer that Emacs Lisp can handle. It is negative. Typical values are -2**29 on 32-bit and -2**61 on 64-bit platforms. </p></dd>
</dl>    <dl> <dt id="integer-width">Variable: <strong>integer-width</strong>
</dt> <dd><p>The value of this variable is a nonnegative integer that controls whether Emacs signals a range error when a large integer would be calculated. Integers with absolute values less than 2**<var>n</var>, where <var>n</var> is this variable’s value, do not signal a range error. Attempts to create larger integers typically signal a range error, although there might be no signal if a larger integer can be created cheaply. Setting this variable to a large number can be costly if a computation creates huge integers. </p></dd>
</dl><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
    <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Integer-Basics.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Integer-Basics.html</a>
  </p>
</div>