Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!ima!mirror!cca!g-rh From: g-rh@cca.UUCP Newsgroups: comp.lang.c Subject: Re: C and Floating Point Message-ID: <14681@cca.CCA.COM> Date: Sat, 4-Apr-87 16:25:18 EST Article-I.D.: cca.14681 Posted: Sat Apr 4 16:25:18 1987 Date-Received: Mon, 6-Apr-87 23:41:14 EST References: <15958@sun.uucp> <5716@brl-smoke.ARPA> Reply-To: g-rh@CCA.CCA.COM.UUCP (Richard Harter) Organization: Computer Corp. of America, Cambridge, MA Lines: 55 Keywords: C Fortran Floating Point One class of usage for floating point equality has been suggested, detection of exact bit patterns. I suggest that this is not advisable, both in principle and in practice. I say 'in principle' because you are using an operation with one set of semantics to simulate an operation with a different set of semantics. I say 'in practice' because you can get unexpected results if the bit patterns are equivalent to unnormalized floating point numbers. However let us consider the following rather common situation: We have a iterative process which generates a floating point number, x, with the following iteration equation x <- x + delta where delta is a function of x and where it is expected that the iteration converges to some limit point x0. We have the generic problem of determining when the iterative sequence has converged, taking into account the nature of floating point arithmetic. Typically we will find the the calculation of the delta in question becomes numerically unstable when x is sufficiently close to x0. Let us suppose that we have addressed this problem, so that we are simply left with the convergence issue. The iteration will have converged if the iteration equation does not change the value of x; i.e. we will have found the limit point of the sequence, given that we are using floating point arithmetic of the machine in question. The question then is, how do we test for this condition? Is the following pseudo code acceptable? if ((x+delta)==x) then terminate_iteration else continue_iteration If not, why not? Alternatively should one use if (delta>0.) then if ((x+delta)<=x) then terminate_iteration else continue_iteration else if (delta<0.) then if ((x+delta)>=x) then terminate_iteration else continue_iteration else terminate_iteration or is this also unacceptable? If so, why? Is there a best method? What is it, and what are the issues involved? If it is not guaranteed the the calculation of delta is stable, what further issues are involved? You will be given a test on this at the end of the week. No grade will be given, but, if your answers are incorrect, World War III will happen. -- Richard Harter, SMDS Inc. [Disclaimers not permitted by company policy.]