[net.lang.c] ANSI C Standard: Float/Double Handling

graham@orca.UUCP (Graham Bromley) (11/09/84)

	Having read some comments on float/double constant and
variable handling, I'd like to say (quite) a few words on the
subject.

	Recently I modified float/double handling in a 4.1 C
compiler. The goal was to seperate float and double into two 
completely distinct data types, for BOTH variables and constants.
Float -> double promotion in function arguments was also addressed.
The following scheme was used.

	* A numeric constant containing an exponent identifier 'e' 
		(e.g. 1.3e03) is of type float, regardless of the number 
		of significant digits. Excess significant digits are discarded.
	* A numeric constant containing an exponent identifer 'd' 
		(e.g. 1.3d03) is of type double, regardless of the number 
		of significant digits.
	* A constant containing a '.' but no 'e' or 'd' is of type
		float or double, as dictated by the number of significant
		digits and the host machine float representation.
	* In expressions of the type
				float OP float
		there is no float->double conversion. But there is in
		expressions of the type
				float OP double
		of course.
	* When a function is called, a float argument is not converted
		to double.

	The standard math library was also modified to include both
float and double versions of each function, e.g. float sin() and
double dsin(), float exp() and double dexp() etc. It took about
a week to do everything.

	Simple benchmarks showed a performance increase of about 2.0:1
for floating point arithmetic bound processes which used only
float operations (no double). Because a float uses only one
register on the VAX and a double needs two, these changes also
allowed more float arithmetic to be done in registers - in
particular, almost all of the arithmetic in the float math library
functions.

	It seems to be a poor idea to make all float constants
double, because a single constant in an expresion will promote
all the way through the expression, causing all those unwanted
conversions.

	Anyone considering this type of thing should beware of side
effects, e.g printf will treat a float argument as double (it
will remove 8 bytes from the stack), as will anything else which
hasn't been compiled with a modified compiler. One way to solve 
this is to cast all float arguments to standard functions with
(double), if you don't want to recompile all standard code with 
your modified compiler. This is harmless on an
unmodified compiler.

henry@utzoo.UUCP (Henry Spencer) (11/11/84)

> 	The standard math library was also modified to include both
> float and double versions of each function, e.g. float sin() and
> double dsin(), float exp() and double dexp() etc. It took about
> a week to do everything.
> ...
> 	It seems to be a poor idea to make all float constants
> double, because a single constant in an expresion will promote
> all the way through the expression, causing all those unwanted
> conversions.
> 
> 	Anyone considering this type of thing should beware of side
> effects, e.g printf will treat a float argument as double ...

Your approach can be summed up as "if you want double, you must say so".
The problem with this approach is that it breaks a large percentage
of the existing floating-point programs.  Avoiding breakage of existing
valid programs is a major item in the ANSI C committee's charter.  This
unfortunately means that places where things default to double right
now must generally stay that way.  Changing the types accepted by (e.g.)
sin(), changing the type of "1.23e5", and changing the type behavior at
the function-call interface are all changes capable of causing massive
breakage and unportability.

There is also the view that people who use floating point casually should
get the most accurate version, i.e. double.  If somebody is seriously
concerned about space or speed, then *he* should pay the price of having
to ask for float explicitly.  I concede that some of the techniques one
must use to ask for it are a bit clumsy.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (11/11/84)

> 	It seems to be a poor idea to make all float constants
> double, because a single constant in an expresion will promote
> all the way through the expression, causing all those unwanted
> conversions.

But the programmer has complete control over this:
	(float)3.14159...
is a float, not a double (under the proposed new rules).

chris@umcp-cs.UUCP (Chris Torek) (11/11/84)

Floating point constants should have the type ``fpconst'' rather than
``float'' or ``double''; this permits expansion if required, or none
if not required.  Figuring out when float is sufficient and when double
is required is nontrivial, but . . . .
-- 
(This line accidently left nonblank.)

In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (301) 454-7690
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@maryland