147 lines
7.7 KiB
HTML
147 lines
7.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>12.3. Comparisons on Numbers</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<meta name="description" value=" Comparisons on 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=tex2html3078 HREF="node125.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3076 HREF="node121.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3070 HREF="node123.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3080 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3081 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html3079 HREF="node125.html"> Arithmetic Operations</A>
|
|
<B>Up:</B> <A NAME=tex2html3077 HREF="node121.html"> Numbers</A>
|
|
<B> Previous:</B> <A NAME=tex2html3071 HREF="node123.html"> Predicates on Numbers</A>
|
|
<HR> <P>
|
|
<H1><A NAME=SECTION001630000000000000000>12.3. Comparisons on Numbers</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>= <i>number</i> &rest <i>more-numbers</i> <BR>
|
|
</tt><tt>/= <i>number</i> &rest <i>more-numbers</i> <BR>
|
|
</tt><tt>< <i>number</i> &rest <i>more-numbers</i> <BR>
|
|
</tt><tt>> <i>number</i> &rest <i>more-numbers</i> <BR>
|
|
</tt><tt><= <i>number</i> &rest <i>more-numbers</i> <BR>
|
|
</tt><tt>>= <i>number</i> &rest <i>more-numbers</i></tt><P>These functions each take one or more arguments. If the sequence
|
|
of arguments satisfies a certain condition:
|
|
<PRE>
|
|
= all the same
|
|
/= all different
|
|
< monotonically increasing
|
|
> monotonically decreasing
|
|
<= monotonically nondecreasing
|
|
>= monotonically nonincreasing
|
|
</PRE>
|
|
then the predicate is true, and otherwise is false.
|
|
Complex numbers may be compared using <tt>=</tt> and <tt>/=</tt>,
|
|
but the others require non-complex arguments.
|
|
Two complex numbers are considered equal by <tt>=</tt>
|
|
if their real parts are equal and their imaginary parts are equal
|
|
according to <tt>=</tt>.
|
|
A complex number may be compared with a non-complex number with <tt>=</tt> or <tt>/=</tt>.
|
|
For example:
|
|
<P><pre>
|
|
(= 3 3) is true. (/= 3 3) is false.
|
|
(= 3 5) is false. (/= 3 5) is true.
|
|
(= 3 3 3 3) is true. (/= 3 3 3 3) is false.
|
|
(= 3 3 5 3) is false. (/= 3 3 5 3) is false.
|
|
(= 3 6 5 2) is false. (/= 3 6 5 2) is true.
|
|
(= 3 2 3) is false. (/= 3 2 3) is false.
|
|
(< 3 5) is true. (<= 3 5) is true.
|
|
(< 3 -5) is false. (<= 3 -5) is false.
|
|
(< 3 3) is false. (<= 3 3) is true.
|
|
(< 0 3 4 6 7) is true. (<= 0 3 4 6 7) is true.
|
|
(< 0 3 4 4 6) is false. (<= 0 3 4 4 6) is true.
|
|
(> 4 3) is true. (>= 4 3) is true.
|
|
(> 4 3 2 1 0) is true. (>= 4 3 2 1 0) is true.
|
|
(> 4 3 3 2 0) is false. (>= 4 3 3 2 0) is true.
|
|
(> 4 3 1 2 0) is false. (>= 4 3 1 2 0) is false.
|
|
(= 3) is true. (/= 3) is true.
|
|
(< 3) is true. (<= 3) is true.
|
|
(= 3.0 #C(3.0 0.0)) is true. (/= 3.0 #C(3.0 1.0)) is true.
|
|
(= 3 3.0) is true. (= 3.0s0 3.0d0) is true.
|
|
(= 0.0 -0.0) is true. (= 5/2 2.5) is true.
|
|
(> 0.0 -0.0) is false. (= 0 -0.0) is true.
|
|
</pre><P>
|
|
With two arguments, these functions perform the usual arithmetic
|
|
comparison tests.
|
|
With three or more arguments, they are useful for range checks,
|
|
as shown in the following example:
|
|
<P><pre>
|
|
(<= 0 x 9) ;true if <tt>x</tt> is between 0 and 9, inclusive
|
|
(< 0.0 x 1.0) ;true if <tt>x</tt> is between 0.0 and 1.0, exclusive
|
|
(< -1 j (length s)) ;true if <tt>j</tt> is a valid index for <tt>s</tt>
|
|
(<= 0 j k (- (length s) 1)) ;true if <tt>j</tt> and <tt>k</tt> are each valid
|
|
; indices for <tt>s</tt> and <b><tt>j</tt><tt>k</tt></b>
|
|
</pre><P>
|
|
<P>
|
|
<hr>
|
|
<b>Rationale:</b> The ``unequality'' relation is called <tt>/=</tt> rather than
|
|
<tt><></tt>
|
|
(the name used in Pascal) for two reasons. First, <tt>/=</tt> of more than two
|
|
arguments is not the same as the <tt>or</tt> of <tt><</tt> and <tt>></tt> of those same
|
|
arguments. Second, unequality is meaningful for complex numbers even though
|
|
<tt><</tt> and <tt>></tt> are not. For both reasons it would be misleading to
|
|
associate unequality with the names of <tt><</tt> and <tt>></tt>.
|
|
<hr>
|
|
<b>Compatibility note:</b> In Common Lisp, the comparison operations
|
|
perform ``mixed-mode'' comparisons: <tt>(= 3 3.0)</tt> is true. In MacLisp,
|
|
there must be exactly two arguments, and they must be either both fixnums
|
|
or both floating-point numbers. To compare two numbers for numerical
|
|
equality and type equality, use <tt>eql</tt>.
|
|
<hr>
|
|
<P>
|
|
<BR><b>[Function]</b><BR>
|
|
<tt>max <i>number</i> &rest <i>more-numbers</i> <BR></tt><tt>min <i>number</i> &rest <i>more-numbers</i></tt><P>The arguments may be any non-complex numbers.
|
|
<tt>max</tt> returns the argument that is greatest (closest
|
|
to positive infinity).
|
|
<tt>min</tt> returns the argument that is least (closest to
|
|
negative infinity).
|
|
<P>
|
|
For <tt>max</tt>,
|
|
if the arguments are a mixture of rationals and floating-point
|
|
numbers, and the largest argument
|
|
is a rational, then the implementation is free to
|
|
produce either that rational or its floating-point approximation;
|
|
if the largest argument is a floating-point number of a smaller format
|
|
than the largest format of any floating-point argument,
|
|
then the implementation is free to
|
|
return the argument in its given format or expanded to the larger format.
|
|
More concisely, the implementation has the choice of returning the largest
|
|
argument as is or applying the rules of floating-point contagion,
|
|
taking all the arguments into consideration for contagion purposes.
|
|
Also, if two or more of the arguments are equal, then any one
|
|
of them may be chosen as the value to return.
|
|
Similar remarks apply to <tt>min</tt> (replacing ``largest argument'' by
|
|
``smallest argument'').
|
|
<P>
|
|
<P><pre>
|
|
(max 6 12) => 12 (min 6 12) => 6
|
|
(max -6 -12) => -6 (min -6 -12) => -12
|
|
(max 1 3 2 -7) => 3 (min 1 3 2 -7) => -7
|
|
(max -2 3 0 7) => 7 (min -2 3 0 7) => -2
|
|
(max 3) => 3 (min 3) => 3
|
|
(max 5.0 2) => 5.0 (min 5.0 2) => 2 <i>or</i> 2.0
|
|
(max 3.0 7 1) => 7 <i>or</i> 7.0 (min 3.0 7 1) => 1 <i>or</i> 1.0
|
|
(max 1.0s0 7.0d0) => 7.0d0
|
|
(min 1.0s0 7.0d0) => 1.0s0 <i>or</i> 1.0d0
|
|
(max 3 1 1.0s0 1.0d0) => 3 <i>or</i> 3.0d0
|
|
(min 3 1 1.0s0 1.0d0) => 1 <i>or</i> 1.0s0 <i>or</i> 1.0d0
|
|
</pre><P>
|
|
<P>
|
|
<BR> <HR><A NAME=tex2html3078 HREF="node125.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3076 HREF="node121.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3070 HREF="node123.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3080 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3081 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html3079 HREF="node125.html"> Arithmetic Operations</A>
|
|
<B>Up:</B> <A NAME=tex2html3077 HREF="node121.html"> Numbers</A>
|
|
<B> Previous:</B> <A NAME=tex2html3071 HREF="node123.html"> Predicates on Numbers</A>
|
|
<HR> <P>
|
|
<HR>
|
|
<P><ADDRESS>
|
|
AI.Repository@cs.cmu.edu
|
|
</ADDRESS>
|
|
</BODY>
|