C Floating point arithmetic

jimc at ucla-cs.UUCP jimc at ucla-cs.UUCP
Wed Dec 4 06:09:52 AEST 1985


In article <706 at lasspvax.UUCP> garry%geology at cu-arpa.cornell.edu.arpa writes:
>In an inauspicious moment, K&R specified "All floating-point arithmetic
>in C is done in double precision" (pg 41).
>	1) floats, as opposed to doubles, are costly and useless (except 
>	   when memory space is critical), and
>	2) I have to advise people with CPU-intense problems not to use C.
>Comments? Does everybody agree?
>(I know what my compiler does, but are char->int and short->int
>required in the same way? K&R seems fuzzy to me...)

On machines with the 8087 (IBM PC,...) where the hardware has to convert
both float and double to an internal format, the cost of float variables is
very slightly cheaper than doubles, as 4 bytes less have to be loaded.  But
on mainframes or, particularly, when floating point is emulated, the extra
cost of double precision is outrageous: more than 4X, since an IEEE float
has 23+1 bits of precision while a double has 52+1.  Certainly the float
variables should be doubled only when necessary, just as is done in integer-
type expressions which may have 16-bit int and 32-bit long.
    As for short-int-long conversions, I don't know of any machines where
loading 16 bits into a 32-bit register is any slower than loading 32 bits.
As I understand it, the reason for converting everything to int is, that
K+R says you can convert any pointer to an int and back again without
wrecking it.  Therefore an int has to match the addressing size.  The moti-
vation, I'm sure, was so undeclared functions could be declared "extern int"
even though they return pointers.  BOO!  
    I personally would like to see a data type for 8-bit (signed?) integers.
How about short = 8 bits, int = 16 bits, long = 32 bits?  (Yes, I know, every-
one's pet code expects 16-bit shorts...)  I very much dislike using char
type for arithmetic, because type char is for CHARACTERS.  I have enough
trouble explaining K+R's example "++ndigits[c-'0'];" without the possibility
that "char c" is a variable of integer type!



More information about the Comp.lang.c mailing list