234 lines
12 KiB
HTML
234 lines
12 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.3. Floating-Point Numbers</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<meta name="description" value=" Floating-Point Numbers">
|
|
<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=tex2html1745 HREF="node20.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1743 HREF="node16.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1737 HREF="node18.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1747 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1748 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html1746 HREF="node20.html"> Complex Numbers</A>
|
|
<B>Up:</B> <A NAME=tex2html1744 HREF="node16.html"> Numbers</A>
|
|
<B> Previous:</B> <A NAME=tex2html1738 HREF="node18.html"> Ratios</A>
|
|
<HR> <P>
|
|
<H2><A NAME=SECTION00613000000000000000>2.1.3. Floating-Point Numbers</A></H2>
|
|
<P>
|
|
Common Lisp allows an implementation to provide one or more kinds of
|
|
floating-point number, which collectively make up the type <tt>float</tt>.
|
|
Now a floating-point number is a (mathematical)
|
|
rational number of the form
|
|
<IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40217.gif">,
|
|
where <i>s</i> is <b>+1</b> or <b>-1</b>, the <i>sign</i>;
|
|
<i>b</i> is an integer greater than 1,
|
|
the <i>base</i> or <i>radix</i> of the representation;
|
|
<i>p</i> is a positive integer,
|
|
the <i>precision</i> (in base-<i>b</i> digits) of the floating-point number;
|
|
<i>f</i> is a positive integer between
|
|
<IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40223.gif"> and <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40223.gif"> (inclusive),
|
|
the <i>significand</i>;
|
|
and <i>e</i> is an integer, the <i>exponent</i>.
|
|
The value of <i>p</i> and the range of <i>e</i>
|
|
depends on the implementation and on the type of floating-point number
|
|
within that implementation.
|
|
In addition, there is a floating-point zero;
|
|
depending on the implementation, there may also be a ``minus zero.''
|
|
If there is no minus zero, then <tt>0.0</tt> and <tt>-0.0</tt> are
|
|
both interpreted as simply a floating-point zero.
|
|
<P>
|
|
<hr>
|
|
<b>Implementation note:</b> The form of the above description should not be construed
|
|
to require the internal representation to be in sign-magnitude form.
|
|
Two's-complement and other representations are also acceptable. Note
|
|
that the radix of the internal representation may be other than 2, as on
|
|
the IBM 360 and 370, which use radix 16; see
|
|
<tt>float-radix</tt>.
|
|
<hr>
|
|
<P>
|
|
Floating-point numbers may be provided in a variety of precisions and sizes,
|
|
depending on the implementation. High-quality floating-point
|
|
software tends to depend critically on the precise nature of the
|
|
floating-point arithmetic and so may not always be completely portable.
|
|
As an aid in writing programs that are
|
|
moderately portable, however, certain definitions are made here:
|
|
<UL><LI>
|
|
A <i>short</i> floating-point number (type <tt>short-float</tt>)
|
|
is of the representation of smallest
|
|
fixed precision provided by an implementation.
|
|
<P>
|
|
<LI>
|
|
A <i>long</i> floating-point number (type <tt>long-float</tt>)
|
|
is of the representation of the largest fixed
|
|
precision provided by an implementation.
|
|
<P>
|
|
<LI>
|
|
Intermediate between short and long formats are two others, arbitrarily
|
|
called <i>single</i> and <i>double</i> (types <tt>single-float</tt> and <tt>double-float</tt>).
|
|
</UL>
|
|
The precise definition of these categories is implementation-dependent.
|
|
However, the rough intent is that short floating-point numbers be
|
|
precise to at least four decimal places (but also have
|
|
a space-efficient representation);
|
|
single floating-point numbers, to at least seven decimal places;
|
|
and double floating-point numbers, to at least fourteen decimal places.
|
|
It is suggested that
|
|
the precision (measured in bits, computed as <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40227.gif">)
|
|
and the exponent size (also measured in bits, computed as the base-2
|
|
logarithm of 1 plus the maximum exponent value) be at least as great
|
|
as the values in table <A HREF="node19.html#FloatingFormatRequirementsTable">2-1</A>.
|
|
<P>
|
|
<A NAME=FloatingFormatRequirementsTable> </A><IMG ALIGN=BOTTOM ALT="" SRC="_24769_table670.gif">
|
|
<P>
|
|
Floating-point numbers are written in either decimal fraction
|
|
or computerized scientific notation: an optional sign,
|
|
then a non-empty sequence of digits with an embedded decimal point,
|
|
then an optional decimal exponent specification.
|
|
If there is no exponent specifier, then
|
|
the decimal point is required, and there must be digits
|
|
after it.
|
|
The exponent specifier consists of an exponent marker,
|
|
an optional sign, and a non-empty sequence of digits.
|
|
For preciseness, here is a modified-BNF description of floating-point
|
|
notation.
|
|
<PRE>
|
|
<i>floating-point-number</i> ::= [<i>sign</i>] {<i>digit</i>}* <i>decimal-point</i> {<i>digit</i>}* [<i>exponent</i>]
|
|
| [<i>sign</i>] {<i>digit</i>}+ [<i>decimal-point</i> {<i>digit</i>}*] <i>exponent</i>
|
|
<i>sign</i> ::= <tt>+</tt> | <tt>-</tt>
|
|
<i>decimal-point</i> ::= <tt>.</tt>
|
|
<i>digit</i> ::= <tt>0</tt> | <tt>1</tt> | <tt>2</tt> | <tt>3</tt> | <tt>4</tt> | <tt>5</tt> | <tt>6</tt> | <tt>7</tt> | <tt>8</tt> | <tt>9</tt>
|
|
<i>exponent</i> ::= <i>exponent-marker</i> [<i>sign</i>] {<i>digit</i>}+
|
|
<i>exponent-marker</i> ::= <tt>e</tt> | <tt>s</tt> | <tt>f</tt> | <tt>d</tt> | <tt>l</tt> | <tt>E</tt> | <tt>S</tt> | <tt>F</tt> | <tt>D</tt> | <tt>L</tt>
|
|
</PRE>
|
|
If no exponent specifier is present, or if the exponent marker <tt>e</tt>
|
|
(or <tt>E</tt>) is used, then the precise format to be used is not
|
|
specified. When such a representation is read and
|
|
converted to an internal floating-point data object, the format specified
|
|
by the variable <tt>*read-default-float-format*</tt> is used; the initial
|
|
value of this variable is <tt>single-float</tt>.
|
|
<P>
|
|
The letters <tt>s</tt>, <tt>f</tt>, <tt>d</tt>, and <tt>l</tt> (or their
|
|
respective uppercase equivalents) explicitly specify the
|
|
use of <i>short</i>, <i>single</i>, <i>double</i>, and <i>long</i> format, respectively.
|
|
<P>
|
|
Examples of floating-point numbers:
|
|
<P><pre>
|
|
0.0 ;Floating-point zero in default format
|
|
0E0 ;Also floating-point zero in default format
|
|
-.0 ;This may be a zero or a minus zero,
|
|
; depending on the implementation
|
|
0. ;The <i>integer</i> zero, not a floating-point zero!
|
|
0.0s0 ;A floating-point zero in <i>short</i> format
|
|
0s0 ;Also a floating-point zero in <i>short</i> format
|
|
3.1415926535897932384d0 ;A <i>double</i>-format approximation to <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40211.gif">
|
|
6.02E+23 ;Avogadro's number, in default format
|
|
602E+21 ;Also Avogadro's number, in default format
|
|
3.010299957f-1 ;<IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40212.gif">, in <i>single</i> format
|
|
-0.000000001s9 ;<IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40201.gif"> in <i>short</i> format, the hard way
|
|
</pre>
|
|
<P>
|
|
|
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
|
<i>Notice of correction.</i>
|
|
The first edition unfortunately listed an incorrect value <tt>(3.1010299957f-1)</tt>
|
|
for the base-10 logarithm of 2.
|
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
|
<P>
|
|
The internal format used for an external representation depends only
|
|
on the exponent marker and not on the number of decimal digits
|
|
in the external representation.
|
|
<P>
|
|
While Common Lisp provides terminology and notation sufficient
|
|
to accommodate four distinct floating-point formats,
|
|
not all implementations will have the means to support
|
|
that many distinct formats.
|
|
An implementation is therefore permitted to provide
|
|
fewer than four distinct internal floating-point formats,
|
|
in which case at least one of them will be ``shared''
|
|
by more than one of the external format names <i>short</i>, <i>single</i>,
|
|
<i>double</i>, and <i>long</i> according to the following rules:
|
|
<UL><LI>
|
|
If one internal format is provided, then it is considered to be
|
|
<i>single</i>, but serves also as <i>short</i>, <i>double</i>, and <i>long</i>.
|
|
The data types <tt>short-float</tt>,
|
|
<tt>single-float</tt>, <tt>double-float</tt>, and <tt>long-float</tt> are
|
|
considered to be identical. An expression such as <tt>(eql 1.0s0 1.0d0)</tt>
|
|
will be true in such an implementation
|
|
because the two numbers <tt>1.0s0</tt> and <tt>1.0d0</tt> will
|
|
be converted into the same internal format and therefore be considered
|
|
to have the same data type, despite the differing external syntax.
|
|
Similarly, <tt>(typep 1.0L0 'short-float)</tt> will be true in such
|
|
an implementation.
|
|
For output purposes all floating-point numbers are assumed to be
|
|
of <i>single</i> format and thus will print using the
|
|
exponent letter <tt>E</tt> or <tt>F</tt>.
|
|
<P>
|
|
<LI>
|
|
If two internal formats are provided, then either of two correspondences
|
|
may be used, depending on which is the more appropriate:<P>
|
|
<UL><LI>
|
|
One format is <i>short</i>; the other is <i>single</i> and serves also
|
|
as <i>double</i> and <i>long</i>.
|
|
The data types
|
|
<tt>single-float</tt>, <tt>double-float</tt>, and <tt>long-float</tt> are
|
|
considered to be identical, but <tt>short-float</tt> is distinct.
|
|
An expression such as <tt>(eql 1.0s0 1.0d0)</tt>
|
|
will be false, but <tt>(eql 1.0f0 1.0d0)</tt> will be true.
|
|
Similarly, <tt>(typep 1.0L0 'short-float)</tt> will be false,
|
|
but <tt>(typep 1.0L0 'single-float)</tt> will be true.
|
|
For output purposes all floating-point numbers are assumed to be
|
|
of <i>short</i> or <i>single</i> format.
|
|
<P>
|
|
<LI>
|
|
One format is <i>single</i> and serves also as <i>short</i>;
|
|
the other is <i>double</i> and serves also as <i>long</i>.
|
|
The data types <tt>short-float</tt> and <tt>single-float</tt> are considered to be
|
|
identical, and the data types <tt>double-float</tt> and <tt>long-float</tt> are
|
|
considered to be identical.
|
|
An expression such as <tt>(eql 1.0s0 1.0d0)</tt>
|
|
will be false, as will <tt>(eql 1.0f0 1.0d0)</tt>;
|
|
but <tt>(eql 1.0d0 1.0L0)</tt> will be true.
|
|
Similarly, <tt>(typep 1.0L0 'short-float)</tt> will be false,
|
|
but <tt>(typep 1.0L0 'double-float)</tt> will be true.
|
|
For output purposes all floating-point numbers are assumed to be
|
|
of <i>single</i> or <i>double</i> format.
|
|
</UL>
|
|
<P>
|
|
<LI>
|
|
If three internal formats are provided, then either of two correspondences
|
|
may be used, depending on which is the more appropriate:<p>
|
|
<UL><LI>
|
|
One format is <i>short</i>; another format is <i>single</i>; and the third format is
|
|
<i>double</i> and serves also as <i>long</i>. Similar constraints apply.
|
|
<P>
|
|
<LI>
|
|
One format is <i>single</i> and serves also as <i>short</i>;
|
|
another is <i>double</i>; and the third format is <i>long</i>.
|
|
</UL>
|
|
</UL>
|
|
<P>
|
|
<hr>
|
|
<b>Implementation note:</b> It is recommended that an implementation
|
|
provide as many distinct floating-point formats as feasible,
|
|
using table <A HREF="node19.html#FloatingFormatRequirementsTable">2-1</A> as a guideline.
|
|
Ideally, short-format floating-point numbers should have an
|
|
``immediate'' representation that does not require heap allocation;
|
|
single-format
|
|
floating-point numbers should approximate IEEE proposed standard
|
|
single-format floating-point numbers; and double-format floating-point
|
|
numbers should approximate IEEE proposed standard double-format
|
|
floating-point numbers
|
|
[<A HREF="node368.html#IEEEPROPOSEDFLOATINGPOINTSTANDARD">23</A>,<A HREF="node368.html#IEEEFLOATINGPOINTIMPLGUIDE">17</A>,<A HREF="node368.html#IEEEFLOATINGPOINTIMPLGUIDEERRATA">16</A>].
|
|
<hr>
|
|
<BR> <HR><A NAME=tex2html1745 HREF="node20.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1743 HREF="node16.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1737 HREF="node18.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1747 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1748 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html1746 HREF="node20.html"> Complex Numbers</A>
|
|
<B>Up:</B> <A NAME=tex2html1744 HREF="node16.html"> Numbers</A>
|
|
<B> Previous:</B> <A NAME=tex2html1738 HREF="node18.html"> Ratios</A>
|
|
<HR> <P>
|
|
<HR>
|
|
<P><ADDRESS>
|
|
AI.Repository@cs.cmu.edu
|
|
</ADDRESS>
|
|
</BODY>
|