[comp.lang.misc] Floating point equality

firth@sei.cmu.edu (Robert Firth) (05/11/89)

In article <452@sdti.SDTI.COM> turner@sdti.UUCP (0006-Prescott K. Turner, Jr.) writes:

>I don't buy that.  Every language with a successful floating point type
>has comparison for equality.

Well, I recall one compiler that implemented fuzzy equality... but
seriously, when I wrote a Coral-66 compiler, I deliberately removed
floating-point equality.  If the user wrote

	IF x = y THEN ...

the compiler generated an error message. In the instruction manual,
I explained that floating test for equality was permitted only
against the singular points of the domain, and gave the following
very broad hint:

"The test should be rewritten as

	IF x-y = 0 THEN ...

 and read as 'if the subtraction of y from x yields underflow then...'"

The actual codegeneration for floating-point expressions was reviewed
very carefully, until I was convinced it was close to impossible to
generate anomalies. Thus, things like

	z := x+y;
	IF z - (x+y) = 0 THEN ...	-- always TRUE
	IF (x+y) - z = 0 THEN ...	-- always TRUE

	pi := 3.141592686;
	IF pi - 3.141592686 = 0 THEN ... -- always TRUE

could be relied upon.  The codegenerator performed no code rearrangement
that could not be guaranteed to yield the bitwise identical result (and
so for instance would not change "z-(x+y)" into "z-x-y").

As far as I know, nobody got bitten by a running program.

jwb@LINDENTHAL.CAE.RI.CMU.EDU (John Baugh) (05/11/89)

In article <3349@bd.sei.cmu.edu> firth@sei.cmu.edu (Robert Firth) writes:
>Well, I recall one compiler that implemented fuzzy equality... but

Didn't Dijkstra mention his regret in allowing floating point
equality if two numbers differed only in the last bit?  (I don't
remember the language.)

John Baugh
--