Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!mintaka!bloom-beacon!eru!luth!sunic!mcsun!cernvax!chx400!urz.unibas.ch!carlos From: carlos@urz.unibas.ch Newsgroups: comp.lang.c Subject: Re: Floating point non-exactness Message-ID: <1990Jul31.101803.852@urz.unibas.ch> Date: 31 Jul 90 09:18:03 GMT References: <622@.tetrauk.UUCP> Organization: University of Basel, Switzerland Lines: 38 Oooops, sorry for message nr. 2653, it was our first try ever.... Hope this time works...... In article <622@.tetrauk.UUCP>, rick@.tetrauk.UUCP (Rick Jones) writes: > fpcmp (double a, double b) > returns 0 if a equals b within the reliable precision, > else returns 1 if a > b, or -1 if a < b > > Real problem: how do you write fpcmp() ? Since there is only a finite quantity of fp numbers on the computer, an Increase(x) resp. Decrease(x) (to get the next resp. previous fp of x) procedure for fp numbers could do it. The tolerance that you build in your function fpcmp() depends on the fp system you use. You can implement the above functions very easily for IEEE fp formats: Just regard the bit-sequence representing your fp number as an integer, and increase resp. decrease it by 1. The new integer (interpreting it as double) is the next resp. previous possible fp number. This holds automatically in case of changes to other exponents and changes between normalized and denormalized numbers. You must only watch out for a change in sign and that decreasing negative numbers means increasing the mantissa (and exponent). > Can you ensure that "fpcmp (a, b)" and "fpcmp (a-b, 0.0)" yield the same resul t > when a and b are very close? Yes, if your fp system conforms IEEE Std. 754 due to the existence of denormalized numbers. Good luck, Stefan & Carlos