[comp.lang.c] C and Floating Point and The Elements of Programming Style

flaps@utcsri.UUCP (Alan J Rosenthal) (04/08/87)

First, the recap:

Someone wrote about how fortran was created by and used by people who were
primarily mathematicians rather than computer scientists, who wouldn't DREAM
of doing things like disallowing comparing floating-point numbers for equality
(among other things that were under discussion too).  (I hope I've summarized
fairly here; I think I have.)

I wrote that, aside from the fact that it is a simple fact of life that
floating point numbers can't (in general) be compared for equality, fortran
was no superhero in this respect, and in particular:

>> 2.  Kernighan and Plauger, in _The_Elements_of_Programming_Style_, note that
>> the version of fortran at their local installation fails on an equality test
>> when one number is on an input punch-card and the other, identically typed
>> and also a constant, is in a program.  This is because different scanning
>> routines were used in the i/o library compiled with the fortran program and
>> the i/o routines that were part of the fortran compiler!  So much for
>> floating-point equality in fortran.

Mark Brader <msb@sq.uucp> wrote:

>This is misremembered.  The reference is to the fact that in a Fortran
>PROGRAM, blanks are ignored (except in quoted or counted character strings);
>whereas in Fortran formatted DATA, blanks are taken as zeros.
[ thus if you read "1 024" (as an integer) and compare "if (i .eq. 1 024)" you
get false because you compare 10024 for 'i' with 1024 for '1 024' in the
program.  furthermore this has nothing to do with floating point
specifically. ]


Now, my article:

Mark Brader is correctly reporting another complaint about fortran presented
in this book; however, I was not mistaken.  I just got this book out from the
library again today, and to quote from page 95 (McGraw-Hill, 1974):

    We observed that one should never test floating point numbers for
    exact equality.  But here is one case where any rational person would
    believe that the comparison would work:

	95 N = N+1
	   READ (5,100) DATA(N)
       100 FORMAT(F10.3)
	   IF(DATA(N).NE.999.999) GO TO 95
    
    If the input card contains 999.999 in the proper field, the program
    will stop reading, will it not?  ...

    On some systems, the routines in the compiler that convert "999.999"
    into its internal representation were written by different people
    than those who wrote the routines that convert "999.999" when a READ
    is executed.

Of course they then go on to explain (briefly) why this is not a good idea,
but I think we all know that here.

-- 

Alan J Rosenthal

flaps@csri.toronto.edu, {seismo!utai or utzoo}!utcsri!flaps,
flaps@toronto on csnet, flaps at utorgpu on bitnet.

"Probably the best operating system in the world is the [operating system]
made for the PDP-11 by Bell Laboratories." - Ted Nelson, October 1977