🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.next.programmer
Article #1572

cc problem and cure: comparison among doubles can produce wrong results

#1572
From: Ivo Welch
Date: Sat, 17 Oct 1992 00:21
33 lines
1492 bytes
There is a floating point comparison problem with the C compiler, resulting
from the  interaction of compiler switches  with the 68040 CPU. If  you run
this program through /NeXTStep_3.0/bin/cc (July 21, 1992):

	#include <stdio.h>
	int main() {
	  double omega;  double decay= 0.02, rev=0.05;
	
	  fprintf(stderr, "Code Starts");  omega = (rev)/(decay);

	  fprintf(stderr, "\n OMEGA = %f ==> %d\n", omega, omega!=omega);
	  fprintf(stderr, " OMEGA = %f ==> %d\n", omega, omega!=omega);
	  fprintf(stderr, " OMEGA = %f ==> %d\n", omega, omega!=omega);
	}

the output is

	Code Starts
	 OMEGA = 2.500000 ==> 1
	 OMEGA = 2.500000 ==> 0
	 OMEGA = 2.500000 ==> 0

If you use #define isNaN(x) ((x)!=(x)), chances  are your  code may produce
incorrect results.

The cure is to use the compiler switch -m68040-only (on  the C compiler) or
-ffloat-store (either on the C or the C++ compiler).   (I believe the cause
of  the    problem  is  how  differences in   internal  and external/stored
precisions interact with  compiler optimizations.)  Of course, your  MFLOPS
rating may go down...

/ivo welch
Technical Details
Message-ID: <9210170021.AA00604@next.agsm.ucla.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!agate!ucbvax!NEXT.AGSM.UCLA.EDU!ivo

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!agate!ucbvax!NEXT.AGSM.UCLA.EDU!ivo
From: ivo@NEXT.AGSM.UCLA.EDU (Ivo Welch)
Newsgroups: comp.sys.next.programmer
Subject: cc problem and cure: comparison among doubles can produce wrong results
Message-ID: <9210170021.AA00604@next.agsm.ucla.edu>
Date: 17 Oct 92 00:21:33 GMT
Sender: daemon@ucbvax.BERKELEY.EDU
Lines: 33