double vs single precision

David Tribble tribble_acn%uta.csnet at csnet-relay.arpa
Wed Jan 8 20:47:32 AEST 1986


For the last few weeks there has been on-going discussion of
the merits and drawbacks (demerits?) of the precision the compiler
sees fit to use for statements like-
	float a, b;
	a = a + 1.0;	/* 1 */
	a = a + b;	/* 2 */
One argument that should be mentioned is that some compiler writers
choose the criteria-
	1. keep runtime code small.
	2. stay standard (K&R).
to design their C compiler.  For some machines, especially those that
so not have floating point instructions built in (eg, 808 and 68000)
it makes sense to convert everything to double (with a runtime called
something like $ftod), do the add operation (with a runtime called
$dadd), then convert the result back to a float (with a rtn called $dtof).
What's the advantage, you ask?  Well,-
	1. It keeps runtime code small, because only one floating
	   point routine ($dadd) is required; a single-precision
	   $fadd is not necessary.
	2. It agrees with K&R's definition of 'the usual arithmetic
	   conversions' for doing arithmetic operations.
True, it is more inefficient than calling a single-precision add ($fadd)
and sidestep the converions to and from double-precision. But if your
code uses ANY float-double conversions, your executable code will
require $ftod and $dtof calls, so you are incurring this overhead
already; why incur more overhead with $fadd?

Of course, if you are compiling code for a machine with built-in
floating point rtn's (or if your operating system supplies you
with them), or if you are not concerned with executable runtime code
size, then it would be advantageous to make the compiler smart enough
to choose the precision as it pleases.

My suggestion is to provide ANSI C with the standard pragmas
	#pragma float		/* force single precision */
	#pragma double		/* force double precision */
that the programmer could insert above the arithmetic statement
to specify the precision to use. These pragmas would be ignored
on compilers that choose not to give the programmer a choice.

	David R. Tribble,	Univ. Texas @ Arlington



More information about the Comp.lang.c mailing list