Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!ucsd!sdd.hp.com!samsung!uunet!mstan!amull From: amull@Morgan.COM (Andrew P. Mullhaupt) Newsgroups: comp.unix.wizards Subject: Re: Floating Point Expectations Summary: Don't do that. Keywords: Double Precision Floating Point Message-ID: <994@terminus.Morgan.COM> Date: 25 May 90 00:55:31 GMT References: <411@yonder.UUCP> Organization: Morgan Stanley & Co. NY, NY Lines: 34 In article <411@yonder.UUCP>, michael@yonder.UUCP (Michael E. Haws) writes: > Is it reasonable to expect that the following code should > work the same on all platforms, and produce "good" results? > > if ((45.0 / 100.0) == .45) > printf("good\n"); > else > printf("bad\n"); No. Thou shalt not compare floating point results for equality. They might depend on compiler options for optimization, etc. even on the same machine. > I remember seeing somewhere (possible a different group) an article which > made reference to some software which analyzed the accuracy of floating > point arithmetic on whatever host it was running. Anyone know where > this code can be found? You may be referring to paranoia, which I recommend for investigating your floating point reliability. However, it does not work by doing computations and then simply comparing results to fixed values. It usually computes various relationships which should be true among its results and reports on discrepancies. You might wonder if there are clever ways around this kind of problem, like subtracting and comparing to zero, but it if you are expecting to get the code to agree bit for bit across "all platforms", you're going to have to something like resorting to emulating the floating point arithmetic in integer C code. Consider that IBM 360 style floating point is not bit-normalized, and that Cray double precision is 64 bit, etc. Later, Andrew Mullhaupt