249 lines
11 KiB
HTML
249 lines
11 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>12.4. Arithmetic Operations</TITLE>
|
||
|
</HEAD>
|
||
|
<BODY>
|
||
|
<meta name="description" value=" Arithmetic Operations">
|
||
|
<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=tex2html3090 HREF="node126.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3088 HREF="node121.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3082 HREF="node124.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3092 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3093 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
||
|
<B> Next:</B> <A NAME=tex2html3091 HREF="node126.html"> Irrational and Transcendental </A>
|
||
|
<B>Up:</B> <A NAME=tex2html3089 HREF="node121.html"> Numbers</A>
|
||
|
<B> Previous:</B> <A NAME=tex2html3083 HREF="node124.html"> Comparisons on Numbers</A>
|
||
|
<HR> <P>
|
||
|
<H1><A NAME=SECTION001640000000000000000>12.4. Arithmetic Operations</A></H1>
|
||
|
<P>
|
||
|
Each of the functions in this section requires that its arguments all be
|
||
|
numbers; to call one with a non-number is an error. Unless otherwise
|
||
|
specified, each works on all types of numbers, automatically performing
|
||
|
any required coercions when arguments are of different types.
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>+ &rest <i>numbers</i></tt><P>This returns the sum of the arguments. If there are no arguments, the result
|
||
|
is <tt>0</tt>, which is an identity for this operation.
|
||
|
<P>
|
||
|
<hr>
|
||
|
<b>Compatibility note:</b> While <tt>+</tt> is compatible with its use in Lisp Machine Lisp,
|
||
|
it is incompatible with MacLisp, which uses <tt>+</tt> for fixnum-only
|
||
|
addition.
|
||
|
<hr>
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>- <i>number</i> &rest <i>more-numbers</i></tt><P>The function <tt>-</tt>, when given one argument, returns the negative
|
||
|
of that argument.
|
||
|
<P>
|
||
|
The function <tt>-</tt>, when given more than one argument, successively subtracts
|
||
|
from the first argument all the others, and returns the result.
|
||
|
For example, <tt>(- 3 4 5)</tt> => <tt>-6</tt>.
|
||
|
<P>
|
||
|
<hr>
|
||
|
<b>Compatibility note:</b> While <tt>-</tt> is compatible with its use in Lisp Machine Lisp,
|
||
|
it is incompatible with MacLisp, which uses <tt>-</tt> for fixnum-only
|
||
|
subtraction.
|
||
|
Also, <tt>-</tt> differs from <tt>difference</tt> as used in most Lisp
|
||
|
systems in the case of one argument.
|
||
|
<hr>
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>* &rest <i>numbers</i></tt><P>This returns the product of the arguments.
|
||
|
If there are no arguments, the result
|
||
|
is <tt>1</tt>, which is an identity for this operation.
|
||
|
<P>
|
||
|
<hr>
|
||
|
<b>Compatibility note:</b> While <tt>*</tt> is compatible with its use in Lisp Machine Lisp,
|
||
|
it is incompatible with MacLisp, which uses <tt>*</tt> for fixnum-only
|
||
|
multiplication.
|
||
|
<hr>
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>/ <i>number</i> &rest <i>more-numbers</i></tt><P>The function <tt>/</tt>, when given more than one argument, successively divides
|
||
|
the first argument by all the others and returns the result.
|
||
|
<P>
|
||
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
||
|
It is generally accepted that it is an error for any argument other than the
|
||
|
first to be zero.
|
||
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
||
|
<P>
|
||
|
With one argument, <tt>/</tt> reciprocates the argument.
|
||
|
<P>
|
||
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
||
|
It is generally accepted that it is an error in this case for the argument
|
||
|
to be zero.
|
||
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
||
|
<P>
|
||
|
<tt>/</tt> will produce a ratio if the mathematical quotient of two integers
|
||
|
is not an exact integer. For example:
|
||
|
<P><pre>
|
||
|
(/ 12 4) => 3
|
||
|
(/ 13 4) => 13/4
|
||
|
(/ -8) => -1/8
|
||
|
(/ 3 4 5) => 3/20
|
||
|
</pre><P>
|
||
|
To divide one integer by another producing an integer result,
|
||
|
use one of the functions <tt>floor</tt>, <tt>ceiling</tt>, <tt>truncate</tt>,
|
||
|
or <tt>round</tt>.
|
||
|
<P>
|
||
|
If any argument is a floating-point number,
|
||
|
then the rules of floating-point contagion apply.
|
||
|
<P>
|
||
|
<hr>
|
||
|
<b>Compatibility note:</b> What <tt>/</tt> does is totally unlike what the usual
|
||
|
<tt>//</tt> or <tt>quotient</tt> operator does. In most Lisp systems,
|
||
|
<tt>quotient</tt> behaves like <tt>/</tt> except when dividing integers,
|
||
|
in which case it behaves like <tt>truncate</tt> of two arguments;
|
||
|
this behavior is mathematically intractable, leading to such
|
||
|
anomalies as
|
||
|
<P><pre>
|
||
|
(quotient 1.0 2.0) => 0.5 but (quotient 1 2) => 0
|
||
|
</pre><P>
|
||
|
In contrast, the Common Lisp function <tt>/</tt> produces these results:
|
||
|
<P><pre>
|
||
|
(/ 1.0 2.0) => 0.5 and (/ 1 2) => 1/2
|
||
|
</pre><P>
|
||
|
In practice <tt>quotient</tt> is used only when one is sure that both arguments
|
||
|
are integers, <i>or</i> when one is sure that at least one argument
|
||
|
is a floating-point number. <tt>/</tt> is tractable for its purpose
|
||
|
and works for <i>any</i> numbers.
|
||
|
<hr>
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>1+ <i>number</i> <BR></tt><tt>1- <i>number</i></tt><P><tt>(1+ <i>x</i>)</tt> is the same as <tt>(+ <i>x</i> 1)</tt>.
|
||
|
<P>
|
||
|
<tt>(1- <i>x</i>)</tt> is the same as <tt>(- <i>x</i> 1)</tt>.
|
||
|
Note that the short name may be confusing: <tt>(1- <i>x</i>)</tt> does <i>not</i> mean
|
||
|
1-<i>x</i>; rather, it means <i>x</i>-1.
|
||
|
<P>
|
||
|
<hr>
|
||
|
<b>Rationale:</b> These are included primarily for compatibility with MacLisp
|
||
|
and Lisp Machine Lisp. Some programmers prefer always to write <tt>(+ x 1)</tt> and
|
||
|
<tt>(- x 1)</tt> instead of <tt>(1+ x)</tt> and <tt>(1- x)</tt>.
|
||
|
<hr>
|
||
|
<b>Implementation note:</b> Compiler writers are very strongly encouraged to ensure
|
||
|
that <tt>(1+ x)</tt> and <tt>(+ x 1)</tt> compile into identical code, and
|
||
|
similarly for <tt>(1- x)</tt> and <tt>(- x 1)</tt>, to avoid pressure on a Lisp
|
||
|
programmer to write possibly less clear code for the sake of efficiency.
|
||
|
This can easily be done as a source-language transformation.
|
||
|
<hr>
|
||
|
<P>
|
||
|
<BR><b>[Macro]</b><BR>
|
||
|
<tt>incf <i>place</i> [<i>delta</i>] <BR></tt><tt>decf <i>place</i> [<i>delta</i>]</tt><P>The number produced by the form <i>delta</i>
|
||
|
is added to (<tt>incf</tt>) or subtracted from (<tt>decf</tt>)
|
||
|
the number in the generalized variable named by <i>place</i>,
|
||
|
and the sum is stored back into <i>place</i> and returned.
|
||
|
The form <i>place</i> may be any form acceptable
|
||
|
as a generalized variable to <tt>setf</tt>.
|
||
|
If <i>delta</i> is not supplied, then the number in <i>place</i> is changed
|
||
|
by <tt>1</tt>.
|
||
|
For example:
|
||
|
<P><pre>
|
||
|
(setq n 0)
|
||
|
(incf n) => 1 and now n => 1
|
||
|
(decf n 3) => -2 and now n => -2
|
||
|
(decf n -5) => 3 and now n => 3
|
||
|
(decf n) => 2 and now n => 2
|
||
|
</pre><P>
|
||
|
The effect of <tt>(incf <i>place</i> <i>delta</i>)</tt>
|
||
|
is roughly equivalent to
|
||
|
<P><pre>
|
||
|
(setf <i>place</i> (+ <i>place</i> <i>delta</i>))
|
||
|
</pre><P>
|
||
|
except that the latter would evaluate any subforms of <i>place</i>
|
||
|
twice, whereas <tt>incf</tt> takes care to evaluate them only once.
|
||
|
Moreover, for certain <i>place</i> forms <tt>incf</tt> may be
|
||
|
significantly more efficient than the <tt>setf</tt> version.
|
||
|
<p>
|
||
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
||
|
X3J13 voted in March 1988 (PUSH-EVALUATION-ORDER) <A NAME=11688> </A>
|
||
|
to clarify order of evaluation (see section <A HREF="node80.html#SETFSECTION">7.2</A>).
|
||
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>conjugate <i>number</i></tt><P>This returns the complex conjugate of <i>number</i>. The conjugate
|
||
|
of a non-complex number is itself. For a complex number <tt>z</tt>,
|
||
|
<P><pre>
|
||
|
(conjugate z) == (complex (realpart z) (- (imagpart z)))
|
||
|
</pre><P>
|
||
|
For example:
|
||
|
<P><pre>
|
||
|
(conjugate #C(3/5 4/5)) => #C(3/5 -4/5)
|
||
|
(conjugate #C(0.0D0 -1.0D0)) => #C(0.0D0 1.0D0)
|
||
|
(conjugate 3.7) => 3.7
|
||
|
</pre><P>
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>gcd &rest <i>integers</i></tt><P>This returns the greatest common divisor of all the arguments,
|
||
|
which must be integers. The result of <tt>gcd</tt> is always a non-negative
|
||
|
integer.
|
||
|
If one argument is given, its absolute value is returned.
|
||
|
If no arguments are given, <tt>gcd</tt> returns <tt>0</tt>,
|
||
|
which is an identity for this operation.
|
||
|
For three or more arguments,
|
||
|
<P><pre>
|
||
|
(gcd <i>a</i> <i>b</i> <i>c</i> ... <i>z</i>) == (gcd (gcd <i>a</i> <i>b</i>) <i>c</i> ... <i>z</i>)
|
||
|
</pre><P>
|
||
|
<P>
|
||
|
Here are some examples of the use of <tt>gcd</tt>:
|
||
|
<P><pre>
|
||
|
(gcd 91 -49) => 7
|
||
|
(gcd 63 -42 35) => 7
|
||
|
(gcd 5) => 5
|
||
|
(gcd -4) => 4
|
||
|
(gcd) => 0
|
||
|
</pre><P>
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>lcm <i>integer</i> &rest <i>more-integers</i></tt><P>This returns the least common multiple of its arguments,
|
||
|
which must be integers.
|
||
|
The result of <tt>lcm</tt> is always a non-negative integer.
|
||
|
For two arguments that are not both zero,
|
||
|
<P><pre>
|
||
|
(lcm <i>a</i> <i>b</i>) == (/ (abs (* <i>a</i> <i>b</i>)) (gcd <i>a</i> <i>b</i>))
|
||
|
</pre><P>
|
||
|
If one or both arguments are zero,
|
||
|
<P><pre>
|
||
|
(lcm <i>a</i> 0) == (lcm 0 <i>a</i>) == 0
|
||
|
</pre><P>
|
||
|
<P>
|
||
|
For one argument, <tt>lcm</tt> returns the absolute value of that argument.
|
||
|
For three or more arguments,
|
||
|
<P><pre>
|
||
|
(lcm <i>a</i> <i>b</i> <i>c</i> ... <i>z</i>) == (lcm (lcm <i>a</i> <i>b</i>) <i>c</i> ... <i>z</i>)
|
||
|
</pre><P>
|
||
|
<P>
|
||
|
Some examples:
|
||
|
<P><pre>
|
||
|
(lcm 14 35) => 70
|
||
|
(lcm 0 5) => 0
|
||
|
(lcm 1 2 3 4 5 6) => 60
|
||
|
</pre><P>
|
||
|
<P>
|
||
|
Mathematically, <tt>(lcm)</tt> should return infinity. Because Common Lisp
|
||
|
does not have a representation for infinity, <tt>lcm</tt>, unlike <tt>gcd</tt>,
|
||
|
always requires at least one argument.
|
||
|
<P>
|
||
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
||
|
X3J13 voted in January 1989
|
||
|
(LCM-NO-ARGUMENTS) <A NAME=11749> </A>
|
||
|
to specify that <tt>(lcm) => 1</tt>.
|
||
|
<P>
|
||
|
This is one of my biggest boners. The identity for <tt>lcm</tt> is of course 1,
|
||
|
not infinity, and so <tt>(lcm)</tt> ought to have been defined to return 1.
|
||
|
Sorry about that, though in point of fact very few users have complained
|
||
|
to me that this mistake in the first edition has cramped their programming style.
|
||
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
||
|
<P>
|
||
|
<BR> <HR><A NAME=tex2html3090 HREF="node126.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3088 HREF="node121.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3082 HREF="node124.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3092 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3093 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
||
|
<B> Next:</B> <A NAME=tex2html3091 HREF="node126.html"> Irrational and Transcendental </A>
|
||
|
<B>Up:</B> <A NAME=tex2html3089 HREF="node121.html"> Numbers</A>
|
||
|
<B> Previous:</B> <A NAME=tex2html3083 HREF="node124.html"> Comparisons on Numbers</A>
|
||
|
<HR> <P>
|
||
|
<HR>
|
||
|
<P><ADDRESS>
|
||
|
AI.Repository@cs.cmu.edu
|
||
|
</ADDRESS>
|
||
|
</BODY>
|