Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!att!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c Subject: Re: Floating point non-exactness Message-ID: <11117@alice.UUCP> Date: 1 Aug 90 12:46:24 GMT References: <622@.tetrauk.UUCP> <5467@quanta.eng.ohio-state.edu> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 23 In article <5467@quanta.eng.ohio-state.edu>, rob@baloo.eng.ohio-state.edu (Rob Carriere) writes: > In other words, usually what you want is something like > int fcmp( double a, double b, double eps ) > { > if ( fabs( a-b ) < eps ) return 0; > if ( a > b ) return 1; > return -1; > } Probably not. The trouble is that if a and b are big enough, then a-b will either be exactly 0 or will be larger than eps. (Here, `big enough' is approximately eps * 2^n, where n is the number of significant bits in a floating-point freaction.) There is also the possibility that a-b might overflow, in which case the comparison would surely have been valid if done directly. Floating point arithmetic is *hard*. -- --Andrew Koenig ark@europa.att.com