Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!husc6!seismo!mcvax!dik From: dik@mcvax.UUCP Newsgroups: comp.lang.c Subject: Re: C, roundoff anomolies, and floating comparisions Message-ID: <345@zuring.mcvax.cwi.nl> Date: Sat, 11-Apr-87 20:06:57 EST Article-I.D.: zuring.345 Posted: Sat Apr 11 20:06:57 1987 Date-Received: Tue, 14-Apr-87 00:02:52 EST References: <15958@sun.uucp> <5716@brl-smoke.ARPA> <14680@cca.CCA.COM> <790@xanth.UUCP> <995@wanginst.EDU> <821@xanth.UUCP> Reply-To: dik@zuring.UUCP (Dik T. Winter) Distribution: world Organization: CWI, Amsterdam Lines: 46 Keywords: C, IEEE Floating Point, register arithmetic, relational operands. In article <821@xanth.UUCP> kent@xanth.UUCP (Kent Paul Dolan) writes: > In article <995@wanginst.EDU> mckeeman@wanginst.EDU (William McKeeman) writes: > > roundoff anomalies > Typically (?) the rounding takes place, or the guard bits are dropped (or > both), when the operand is returned from registers to main memory. This can > cause a problem with relational operations between floating point variables. > A particular example is that a computational quantity which is still in a > register may fail a comparision for equality with the same datum which has > been stored in memory and returned to a register. > In part. What if on your hardware A == B does not imply B == A? (Yes this occurs, for unnormalized numbers though.) > One solution to the latter problem is to have the compiler, any time it > detects a relational comparision between two floating point quantities, > force any values still in registers to memory and back, so that oranges are > being compared to oranges. This might be put under the control of a > compile time switch, for times when the performance penalty cannot be > afforded, or the comparision is know to be either exact or more than "fuzz" > different. But, as I understand the ANSI C proposal it is possible to force storing results in memory and using results from memory, i.e. when you use temporaries as in: temp = a + b; if (c == temp) ... in most languages you are not certain whether c is compared to the expression a + b, still in registers or already stored in memory. ANSI C provides the volatile attribute, and when you give temp this attribute, it is stored in the first statement and loaded again in the second (this is my understanding, please correct me if I am totally wrong). (It is strange to see that C in this respect outperforms numerical languages like Fortran and Algol. See for instance the well known algorithm to calculate floating point machine characteristics by Malcolm (CACM,15,1972), improved by Gentlemen & Marovitch (CACM,17,1974). It did not work on some machines, but would with the volatile attribute.) (Volatile, volatile? This number will not change!) However, this is a kludge (just like monadic +). What it boils down to is: If you are using floating point, know what you are doing, and know your compiler. If you want to do it portable, use defensive programming, i.e. assume nothing. (Do you assume 3.0 / 3.0 == 1.0? It is not true! Or A * 2.0 / 2.0 == A (assuming no overflow occurs). This is not true either!) -- dik t. winter, cwi, amsterdam, nederland INTERNET : dik@cwi.nl BITNET/EARN: dik@mcvax