[comp.lang.c] floating point ==

PEPRBV%CFAAMP.BITNET@wiscvm.wisc.edu (04/01/87)

Vincent Manis <manis%ubc-cs.uucp@BRL.ARPA> writes about an old Univac
Fortran compiler:
  >>This compiler would generate a warning when you compared
  >>floating point numbers for equality (or non-equality).
and suggests that C compilers could do the same thing.

I used the same compiler, and the option to turn off this warning
was only added when users complained about it.  It's a good idea
to have such an option, but in some environments you need to be
able to turn it off so that other error messages don't get lost
in the midst of ignorable warnings.  I also don't think that any
optional warning a compiler can produce should be considered a
violation of the standard.

steve@warwick.UUCP (04/06/87)

OK, now that we have decided that floating point == doesn't make a lot of sense
and that feq(x, y, fuzz) is the way to go, does anybody have an *efficient*
implementation of feq. I have an application spending between 10% and 15% of
its time doing feq, and I would really like to speed up this routine. Any
volunteers?

					Thanks,
					  Steve.

-- 
_______________________________________________________________________________
|UUCP:	 ...!ukc!warwick!steve			| Steve Rumsby		      |
|JANET:	 steve@uk.ac.warwick.maths		| Maths Institute	      |
|ARPA:	 steve%uk.ac.warwick.maths@ucl-cs.ARPA	| University of Warwick	      |
|BITNET: steve%uk.ac.warwick.maths@UK.AC	| Coventry		      |
|						| CV4 7AL		      |
|PHONE:	 +44 203 523523 x2657			| ENGLAND		      |
-------------------------------------------------------------------------------

"For every problem there is one solution which is simple, neat, and wrong."
								-- H. L. Menken

gwyn@brl-smoke.ARPA (Doug Gwyn ) (04/09/87)

In article <429@euclid.warwick.UUCP> steve@warwick.UUCP (Steve Rumsby) writes:
>OK, now that we have decided that floating point == doesn't make a lot of sense
>and that feq(x, y, fuzz) is the way to go...

Have we decided that?  feq() has many of the same problems as ==, just
in a different regime.

firth@sei.cmu.edu.UUCP (04/09/87)

In article <429@euclid.warwick.UUCP> steve@warwick.UUCP (Steve Rumsby) writes:
>OK, now that we have decided that floating point == doesn't make a lot of sense
>and that feq(x, y, fuzz) is the way to go, does anybody have an *efficient*
>implementation of feq. I have an application spending between 10% and 15% of
>its time doing feq, and I would really like to speed up this routine. Any
>volunteers?

Allow me to pass on this disgusting hack with no recommendation
except that, when one of my students found it, it spread like wildfire:

. floating subtract y from x
. extract the exponent field of the result
. integer compare it to "small", where small = log2(eps)
  (don't forget the bias in the representation)

And you don't even have to worry about the sign of (x-y).  To make this
a closed subroutine, replace that last compare by a subtract.  The sign
of the result tells all.  Four instructions on a Vax.