1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Issues/iss086_w.htm
2024-04-01 10:24:07 +02:00

142 lines
8 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Issue CONTAGION-ON-NUMERICAL-COMPARISONS Writeup</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="../Issues/iss085_w.htm">
<LINK REL=UP HREF="../Issues/iss086.htm">
<LINK REL=NEXT HREF="../Issues/iss087_w.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="../Issues/iss085_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="../Issues/iss086.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="../Issues/iss087_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>Issue CONTAGION-ON-NUMERICAL-COMPARISONS Writeup</H2>
<PRE><B>Issue:</B> <A HREF="iss086.htm">CONTAGION-ON-NUMERICAL-COMPARISONS</A><P>
<P>
<B>References:</B> CLtL p.194<P>
<P>
<B>Category:</B> CHANGE<P>
<P>
<B>Edit history:</B> Version 1, 14-Sep-88, Moon<P>
<P>
<B>Problem description:<P>
</B><P>
The numerical contagion rules specified on CLtL p.194 make it impossible<P>
for the numerical comparison functions to be transitive when given<P>
arguments of mixed floating and rational types (see example below).<P>
<P>
<B>Proposal (CONTAGION-ON-NUMERICAL-COMPARISONS:TRANSITIVE):<P>
</B> <P>
Instead of using the same contagion rule both for combining functions<P>
and for comparing functions, make the present rule apply only to<P>
combining functions and add the following rule: When rational and<P>
floating-point numbers are compared by a numerical function, the<P>
function <A REL=DEFINITION HREF="../Body/a_ration.htm#rational"><B>RATIONAL</B></A> is called to convert the floating-point number to a<P>
rational and then an exact comparison is performed. In the case of<P>
complex numbers (<A REL=DEFINITION HREF="../Body/a_ration.htm#rational"><B>RATIONAL</B></A> is for some unknown reason only allowed on<P>
reals), the real and imaginary parts are handled individually.<P>
<P>
It is of course valid to use a more efficient implementation than<P>
actually calling <A REL=DEFINITION HREF="../Body/a_ration.htm#rational"><B>RATIONAL</B></A>, as long as the result of the comparison is<P>
the same.<P>
<P>
<B>Test Cases/Examples:<P>
</B><P>
(<A REL=DEFINITION HREF="../Body/m_defpar.htm#defvar"><B>defvar</B></A> a (/ 10.0 <A REL=DEFINITION HREF="../Body/v_short_.htm#single-float-epsilon"><B>single-float-epsilon</B></A>))<P>
<P>
(= a (<A REL=DEFINITION HREF="../Body/f_1pl_1_.htm#1PL"><B>1+</B></A> (<A REL=DEFINITION HREF="../Body/f_floorc.htm#floor"><B>floor</B></A> a)))<P>
should be <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, since <P>
(= a (<A REL=DEFINITION HREF="../Body/f_floorc.htm#floor"><B>floor</B></A> a))<P>
is certainly T and<P>
(= (<A REL=DEFINITION HREF="../Body/f_floorc.htm#floor"><B>floor</B></A> a) (<A REL=DEFINITION HREF="../Body/f_1pl_1_.htm#1PL"><B>1+</B></A> (<A REL=DEFINITION HREF="../Body/f_floorc.htm#floor"><B>floor</B></A> a)))<P>
is certainly <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>. However, by the rule of floating-point contagion,<P>
(= a (<A REL=DEFINITION HREF="../Body/f_1pl_1_.htm#1PL"><B>1+</B></A> (<A REL=DEFINITION HREF="../Body/f_floorc.htm#floor"><B>floor</B></A> a)))<P>
is the same as <P>
(= a (<A REL=DEFINITION HREF="../Body/a_float.htm#float"><B>float</B></A> (<A REL=DEFINITION HREF="../Body/f_1pl_1_.htm#1PL"><B>1+</B></A> (<A REL=DEFINITION HREF="../Body/f_floorc.htm#floor"><B>floor</B></A> a)) a))<P>
and the latter form is certainly T.<P>
<P>
To understand this example better, it helps to realize that<P>
(= a (+ a 1.0))<P>
is always true, by the definition of <A REL=DEFINITION HREF="../Body/v_short_.htm#single-float-epsilon"><B>single-float-epsilon</B></A>.<P>
<P>
Here is a second example:<P>
<P>
(<A REL=DEFINITION HREF="../Body/m_defpar.htm#defvar"><B>defvar</B></A> i (<A REL=DEFINITION HREF="../Body/f_floorc.htm#floor"><B>floor</B></A> a))<P>
<P>
(<A REL=DEFINITION HREF="../Body/f_eq_sle.htm#LTEQ"><B>&lt;=</B></A> a (+ i 1)) is T.<P>
(&lt; (+ i 1) (+ i 2)) is T.<P>
(<A REL=DEFINITION HREF="../Body/f_eq_sle.htm#LTEQ"><B>&lt;=</B></A> (+ i 2) a) is T by CLtL, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A> by this proposal.<P>
Thus CLtL requires<P>
a <A REL=DEFINITION HREF="../Body/f_eq_sle.htm#LTEQ"><B>&lt;=</B></A> i+1 &lt; i+2 <A REL=DEFINITION HREF="../Body/f_eq_sle.htm#LTEQ"><B>&lt;=</B></A> a<P>
which ought to imply<P>
a &lt; a<P>
which is absurd.<P>
<P>
<B>Rationale:<P>
</B><P>
Transitivity of = and of &lt; are important to many algorithms. What CLtL<P>
says now was probably not intentional, but just the result of thinking<P>
that comparing and combining could be lumped together without really<P>
thinking about it.<P>
<P>
Without this change, it is impossible to extend the :TEST argument to<P>
<A REL=DEFINITION HREF="../Body/f_mk_has.htm#make-hash-table"><B>MAKE-HASH-TABLE</B></A> to allow = or <A REL=DEFINITION HREF="../Body/f_equalp.htm#equalp"><B>EQUALP</B></A>, since there could be two table<P>
entries with rational keys that are not =, then <A REL=DEFINITION HREF="../Body/f_gethas.htm#gethash"><B>GETHASH</B></A> could be<P>
presented with a floating-point argument that is = to both keys.<P>
<P>
<B>Current practice:<P>
</B><P>
Lucid is said to implement the proposal. As far as I know all other<P>
implementations do what CLtL currently says.<P>
<P>
<B>Cost to Implementors:<P>
</B><P>
This requires a change in what is likely to be intricate and<P>
implementation-dependent code. However, the total effort should<P>
be small compared to the cost of writing that code originally.<P>
<P>
<B>Cost to Users:<P>
</B><P>
This is an incompatible change. It's difficult to know if any users<P>
are depending on the current behavior. It seems likely that most users<P>
would expect the proposed behavior, and may be wondering why their<P>
programs don't quite work when the numbers get large.<P>
<P>
<B>Cost of non-adoption:<P>
</B><P>
Comparison functions in Common Lisp will be non-transitive.<P>
<P>
<B>Benefits:<P>
</B><P>
Comparison functions in Common Lisp will be transitive.<P>
<P>
<B>Esthetics:<P>
</B><P>
Having two rules of floating-point contagion may seem less esthetic,<P>
but I'm certain that having the comparison functions behave in a more<P>
mathematically correct fashion outweighs that esthetically.<P>
<P>
<B>Discussion:<P>
</B><P>
Everyone who has expressed an opinion has thought this was the right<P>
thing for years, but we never got around to writing it up as a cleanup<P>
proposal.<P>
<P>
</PRE>
<HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>