From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 107079,c7637cfdf68e766 X-Google-Attributes: gid107079,public X-Google-Thread: f43e6,c7637cfdf68e766 X-Google-Attributes: gidf43e6,public X-Google-Thread: 109d8a,c7637cfdf68e766 X-Google-Attributes: gid109d8a,public X-Google-Thread: f8362,c7637cfdf68e766 X-Google-Attributes: gidf8362,public X-Google-Thread: 103376,c7637cfdf68e766 X-Google-Attributes: gid103376,public From: Gerhard Heinzel Subject: Re: floating point comparison Date: 1997/08/08 Message-ID: #1/1 X-Deja-AN: 262832308 References: <33E61497.33E2@pseserv3.fw.hac.com> <5sar4r$t7m$1@cnn.nas.nasa.gov> <5sbb90$qsc@redtail.cruzio.com> Organization: Rechenzentrum der Max-Planck-Gesellschaft in Garching Newsgroups: comp.lang.ada,sci.math.num-analysis,comp.software-eng,comp.theory,sci.math Date: 1997-08-08T00:00:00+00:00 List-Id: On 7 Aug 1997 schlafly@bbs.cruzio.com wrote: > In article <5sar4r$t7m$1@cnn.nas.nasa.gov>, fyodor@sally.nas.nasa.gov (Chris L. Kuszmaul) writes: > > Fair enough. There are two basic guidlines I follow: > > > > 1: Do not test for equality between floating point values. If you do, then > > you better get a numerical analysit, or figure out how to avoid using floating > > point, or see if you can use a 'close to equal' check. > > There is nothing inherently wrong with testing for equality, > if that is what you really want. It is a very easily > understandable and reliable operation, except for some funny > business involving NANs. > Maybe it should be clarified WHY one would want to test for equality. I can imagine two distinct situations: First, to test whether two numbers that have not been computed, but only entered and maybe stored back and forth, are the same. I rarely have that case (It is usually much better to mark things with logical or integer variables in that case, and test the integers). But I assume that testing for equality may be ok in that case. Second, if you want to compare two numbers that come from some algorithm (i.e. is an eigenvalue zero? is a pivot zero? is a correlation coefficient unity?) In these cases one must NEVER test for exact equality, but always take into account rounding errors and devise some test such as fabs(x-y)