🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3512

Re: Help, best way to compare doubles

#3512
From: Thomas Koenig
Date: Sat, 13 Jan 1996 17:53
33 lines
2141 bytes
[ Followup-To: comp.lang.c.moderated ]

In comp.std.c, gbacon@oreo (Greg Bacon) wrote:
>Jeff Tomich (jtomich@IntNet.net) wrote:
>: Having a hard time to figure out a function on how to compare doubles. 
>: Any ideas?

>No function necessary.. C has this really cool boolean equality operator
>that does this sort of thing for you.  Have a look:

[...]

It is unwise to compare floating point numbers for equality without
thinking hard first.  Roundoff error means that mathematically
equivalent expressions may yield different results for different
orderings; for example, a+b+c may very well compare unequal to
a+c+b (try this out with a=1.0, b=-1.0 and c=1.E-20, for example).
Roundoff error can also mean that algorithms which should converge
don't, so beware.  (I even know one system where a/2*2 != a for one
quarter of floating point numbers - no points for guessing it's
a /370 :-)

The correct way to compare two floating point numbers for "equality" to
use fabs(a-b) < eps.  The problem arises in determining what to use for
eps, of course :-).  What's reasonable depends very much on your problem
and on the floating point characteristics of your system (for example,
do your values span many orders of magnitude?  Are they close to unity?
Could they have opposing signs?), so it's really impossible to tell
without a fairly thorough analysis.
-- 
Thomas Koenig, Thomas.Koenig@ciw.uni-karlsruhe.de, ig25@dkauni2.bitnet.
The joy of engineering is to find a straight line on a double
logarithmic diagram.
Technical Details
Message-ID: <4d8rip$bk2@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4d1k09$aqq@mercury.IntNet.net> <4d8i3o$pjb@info.uah.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: ig25@mvmap66.ciw.uni-karlsruhe.de (Thomas Koenig)
Newsgroups: comp.std.c,comp.lang.c.moderated
Subject: Re: Help, best way to compare doubles
Followup-To: comp.lang.c.moderated
Date: 13 Jan 1996 11:53:29 -0600
Organization: Universität Karlsruhe (TH), Germany 
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4d8rip$bk2@solutions.solon.com>
References: <4d1k09$aqq@mercury.IntNet.net> <4d8i3o$pjb@info.uah.edu>
Reply-To: Thomas.Koenig@ciw.uni-karlsruhe.de
NNTP-Posting-Host: solutions.solon.com