emacs.d/clones/lisp/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node17.html

134 lines
6.7 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>2.1.1. Integers</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Integers">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html1721 HREF="node18.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1719 HREF="node16.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1713 HREF="node16.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1723 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1724 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html1722 HREF="node18.html"> Ratios</A>
<B>Up:</B> <A NAME=tex2html1720 HREF="node16.html"> Numbers</A>
<B> Previous:</B> <A NAME=tex2html1714 HREF="node16.html"> Numbers</A>
<HR> <P>
<H2><A NAME=SECTION00611000000000000000>2.1.1. Integers</A></H2>
<P>
<A NAME=INTEGERSSECTION>The</A>
<A NAME=522><tt>integer</tt></A>
data type is intended to represent mathematical integers.
Unlike most programming languages, Common Lisp in principle imposes no limit on
the magnitude of an integer; storage
is automatically allocated as necessary to represent large integers.
<P>
In every Common Lisp implementation there is a range of integers that are
represented more efficiently than others; each such integer is called a
<i>fixnum</i>, and an integer that is not a fixnum is called a
<i>bignum</i>.
Common Lisp is designed to hide this distinction as much as possible;
the distinction between fixnums and bignums is visible to
the user in only a few places where the efficiency of representation is
important. Exactly which integers are
fixnums is implementation-dependent; typically they will be those
integers in the range <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40205.gif"> to <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40207.gif">,
inclusive, for some <i>n</i> not less than 15.
See <tt>most-positive-fixnum</tt> and <tt>most-negative-fixnum</tt>.
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in January 1989
(FIXNUM-NON-PORTABLE) <A NAME=532>&#160;</A>
to specify that <tt>fixnum</tt> must be a supertype
of the type <tt>(signed-byte 16)</tt>, and additionally that the value
of <tt>array-dimension-limit</tt> must be a fixnum (implying that the implementor
should choose the range of fixnums to be large enough to accommodate the
largest size of array to be supported).
<P>
<hr>
<b>Rationale:</b> This specification allows programmers to declare variables in portable code
to be of type <tt>fixnum</tt> for efficiency. Fixnums are guaranteed to
encompass at least the set of 16-bit signed integers
(compare this to the data type <tt>short int</tt> in the C programming language).
In addition, any valid array index must be a fixnum, and therefore variables
used to hold array indices (such as a <tt>dotimes</tt> variable)
may be declared <tt>fixnum</tt> in portable code.
<hr>
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
Integers are ordinarily written in decimal notation, as a sequence
of decimal digits, optionally preceded by a sign and optionally followed
by a decimal point.
For example:
<P>
<pre>
0 ;Zero
-0 ;This <i>always</i> means the same as <tt>0</tt>
+6 ;The first perfect number
28 ;The second perfect number
1024. ;Two to the tenth power
-1 ;<IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40201.gif">
15511210043330985984000000. ;25 factorial (25!), probably a bignum
</pre><P>
<P>
<hr>
<b>Compatibility note:</b>
MacLisp and Lisp Machine Lisp normally assume that integers
are written in octal (radix-8) notation unless a decimal
point is present.
Interlisp assumes integers are written in decimal notation and uses a
trailing <tt>Q</tt> to indicate octal radix; however, a decimal point,
even in trailing position, <i>always</i> indicates a floating-point number.
This is of course consistent with Fortran. Ada does not permit
trailing decimal points but instead requires them to be embedded.
In Common Lisp, integers written as described
above are always construed to be
in decimal notation, whether or not the decimal point is present;
allowing the decimal point to be present permits compatibility with
MacLisp.
<hr>
<P>
Integers may be notated in radices other than ten.
The notation
<P><pre>
#<i>nn</i>r<i>ddddd</i> or #<i>nn</i>R<i>ddddd</i>
</pre><P>
means the integer in radix-<i>nn</i> notation denoted by the digits
<i>ddddd</i>. More precisely, one may write <tt>#</tt>, a non-empty sequence
of decimal digits representing an unsigned decimal integer <i>n</i>,
<tt>r</tt> (or <tt>R</tt>), an optional sign, and a sequence of radix-<i>n</i>
digits, to indicate an integer written in radix <i>n</i> (which must be
between 2 and 36, inclusive). Only legal digits
for the specified radix may be used; for example, an octal number may
contain only the digits 0 through 7. For digits above 9,
letters of the alphabet of either
case may be used in order. Binary, octal, and
hexadecimal radices are useful enough to warrant the special
abbreviations <tt>#b</tt> for <tt>#2r</tt>, <tt>#o</tt> for <tt>#8r</tt>, and
<tt>#x</tt> for <tt>#16r</tt>.
For example:
<P><pre>
#2r11010101 ;Another way of writing <tt>213</tt> decimal
#b11010101 ;Ditto
#b+11010101 ;Ditto
#o325 ;Ditto, in octal radix
#xD5 ;Ditto, in hexadecimal radix
#16r+D5 ;Ditto
#o-300 ;Decimal -192, written in base 8
#3r-21010 ;Same thing in base 3
#25R-7H ;Same thing in base 25
#xACCEDED ;181202413, in hexadecimal radix
</pre><P>
<P>
<BR> <HR><A NAME=tex2html1721 HREF="node18.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1719 HREF="node16.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1713 HREF="node16.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1723 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1724 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html1722 HREF="node18.html"> Ratios</A>
<B>Up:</B> <A NAME=tex2html1720 HREF="node16.html"> Numbers</A>
<B> Previous:</B> <A NAME=tex2html1714 HREF="node16.html"> Numbers</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>