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

184 lines
9.3 KiB
HTML
Raw Normal View History

2022-08-26 19:11:35 +02:00
<!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>12.5.1. Exponential and Logarithmic Functions</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Exponential and Logarithmic Functions">
<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=tex2html3117 HREF="node128.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3115 HREF="node126.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3109 HREF="node126.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3119 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3120 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3118 HREF="node128.html"> Trigonometric and Related </A>
<B>Up:</B> <A NAME=tex2html3116 HREF="node126.html"> Irrational and Transcendental </A>
<B> Previous:</B> <A NAME=tex2html3110 HREF="node126.html"> Irrational and Transcendental </A>
<HR> <P>
<H2><A NAME=SECTION001651000000000000000>12.5.1. Exponential and Logarithmic Functions</A></H2>
<P>
Along with the usual one-argument and two-argument exponential and
logarithm functions, <tt>sqrt</tt> is considered to be an exponential
function, because it raises a number to the power 1/2.
<P>
<BR><b>[Function]</b><BR>
<tt>exp <i>number</i></tt><P>Returns <i>e</i> raised to the power <i>number</i>,
where <i>e</i> is the base of the natural logarithms.
<P>
<BR><b>[Function]</b><BR>
<tt>expt <i>base-number</i> <i>power-number</i></tt><P>Returns <i>base-number</i> raised to the power <i>power-number</i>.
If the <i>base-number</i> is of type <tt>rational</tt> and the <i>power-number</i> is
an <tt>integer</tt>,
the calculation will be exact and the result will be of type <tt>rational</tt>;
otherwise a floating-point approximation may result.
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in March 1989
(COMPLEX-RATIONAL-RESULT) <A NAME=11788>&#160;</A>
to clarify that provisions similar to those of the previous paragraph apply to complex
numbers. If the <i>base-number</i> is of type <tt>(complex rational)</tt>
and the <i>power-number</i> is
an <tt>integer</tt>,
the calculation will also be exact and the result will be of type
<tt>(or rational (complex rational))</tt>;
otherwise a floating-point or complex floating-point approximation may result.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
When <i>power-number</i> is <tt>0</tt> (a zero of type integer),
then the result is always the value 1 in the type of <i>base-number</i>,
even if the <i>base-number</i> is zero (of any type). That is:
<P><pre>
(expt <i>x</i> 0) == (coerce 1 (type-of <i>x</i>))
</pre><P>
If the <i>power-number</i> is a zero of any other data type,
then the result is also the value 1, in the type of the arguments
after the application of the contagion rules, with one exception:
it is an error if <i>base-number</i> is zero when the <i>power-number</i>
is a zero not of type integer.
<P>
Implementations of <tt>expt</tt> are permitted to use different algorithms
for the cases of a rational <i>power-number</i> and a floating-point
<i>power-number</i>; the motivation is that in many cases greater accuracy
can be achieved for the case of a rational <i>power-number</i>.
For example, <tt>(expt pi 16)</tt> and <tt>(expt pi 16.0)</tt> may yield
slightly different results if the first case is computed by repeated squaring
and the second by the use of logarithms. Similarly, an implementation
might choose to compute <tt>(expt x 3/2)</tt> as if it had
been written <tt>(sqrt (expt x 3))</tt>, perhaps producing a more accurate
result than would <tt>(expt x 1.5)</tt>. It is left to the implementor
to determine the best strategies.
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in January 1989
(EXPT-RATIO) <A NAME=11816>&#160;</A>
to clarify that the preceding remark is in
error, because <tt>(sqrt (expt x 3))</tt> does not produce the same value
as <tt>(expt x 3/2)</tt> in most cases, and to specify that the
specification of the principal value of <tt>expt</tt> as given in section <A HREF="node129.html#BRANCHCUTSSECTION">12.5.3</A>
should be regarded as definitive.
<P>
As an example of the difficulty, let
<IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40793.gif">.
Then <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40794.gif">, but
<IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40795.gif">.
Another example is <b><i>x</i>=-1</b>; then <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40796.gif">, but
<IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40797.gif">.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
The result of <tt>expt</tt> can be a complex number, even when neither argument
is complex, if <i>base-number</i> is negative and <i>power-number</i>
is not an integer. The result is always the principal complex value.
Note that <tt>(expt -8 1/3)</tt> is not permitted to return <tt>-2</tt>;
while <tt>-2</tt> is indeed one of the cube roots of <tt>-8</tt>, it is
not the principal cube root, which is a complex number
approximately equal to <tt>#C(1.0 1.73205)</tt>.
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
<i>Notice of correction.</i> The first edition gave the incorrect value
<tt>#C(0.5 1.73205)</tt> for the principal cube root of <tt>-8</tt>. The correct
value is <tt>#C(1.0 1.73205)</tt>, that is, 1+SQRT(3)<i>i</i>. I simply don't know what
I was thinking of!
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR><b>[Function]</b><BR>
<tt>log <i>number</i> &amp;optional <i>base</i></tt><P>Returns the logarithm of <i>number</i> in the base <i>base</i>,
which defaults to <i>e</i>, the base of the natural logarithms.
For example:
<P><pre>
(log 8.0 2) => 3.0
(log 100.0 10) => 2.0
</pre><P>
The result of <tt>(log 8 2)</tt> may be either <tt>3</tt> or <tt>3.0</tt>, depending on the
implementation.
<P>
Note that <tt>log</tt> may return a complex result when given a non-complex
argument if the argument is negative. For example:
<P><pre>
(log -1.0) == (complex 0.0 (float pi 0.0))
</pre><P>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in January 1989
(IEEE-ATAN-BRANCH-CUT) <A NAME=11878>&#160;</A>
to specify certain floating-point behavior when minus zero is supported.
As a part of that vote it approved a mathematical definition of complex logarithm
in terms of real logarithm, absolute value,
arc tangent of two real arguments, and the phase function as
<PRE><TT>
Logarithm log|<i>z</i>| + <i>i</i> phase <i>z</i>
<P>
</TT></PRE>
This specifies the branch cuts precisely whether minus zero is supported or not;
see <tt>phase</tt> and <tt>atan</tt>.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR><b>[Function]</b><BR>
<tt>sqrt <i>number</i></tt><P>Returns the principal square root of <i>number</i>.
If the <i>number</i> is not complex but is negative, then the result
will be a complex number.
For example:
<P><pre>
(sqrt 9.0) => 3.0
(sqrt -9.0) => #c(0.0 3.0)
</pre><P>
The result of <tt>(sqrt 9)</tt> may be either <tt>3</tt> or <tt>3.0</tt>, depending on the
implementation. The result of <tt>(sqrt -9)</tt> may be either <tt>#c(0 3)</tt>
or <tt>#c(0.0 3.0)</tt>.
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in January 1989
(IEEE-ATAN-BRANCH-CUT) <A NAME=11900>&#160;</A>
to specify certain floating-point behavior when minus zero is supported.
As a part of that vote it approved a mathematical definition of complex square root
in terms of complex logarithm and exponential functions as
<PRE><TT>
Square root <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap40799.gif">
<P>
</TT></PRE>
This specifies the branch cuts precisely whether minus zero is supported or not;
see <tt>phase</tt> and <tt>atan</tt>.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR><b>[Function]</b><BR>
<tt>isqrt <i>integer</i></tt><P>Integer square root: the argument must be a non-negative integer, and the
result is the greatest integer less than or equal to the exact positive
square root of the argument.
For example:
<P><pre>
(isqrt 9) => 3
(isqrt 12) => 3
(isqrt 300) => 17
(isqrt 325) => 18
</pre><P>
<P>
<BR> <HR><A NAME=tex2html3117 HREF="node128.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3115 HREF="node126.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3109 HREF="node126.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3119 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3120 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3118 HREF="node128.html"> Trigonometric and Related </A>
<B>Up:</B> <A NAME=tex2html3116 HREF="node126.html"> Irrational and Transcendental </A>
<B> Previous:</B> <A NAME=tex2html3110 HREF="node126.html"> Irrational and Transcendental </A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>