Nasty bug

Chris Torek chris at mimsy.umd.edu
Wed Sep 5 02:05:33 AEST 1990


In article <RICHARD.90Sep3115406 at orange.iesd.auc.dk> richard at iesd.auc.dk
(Richard Flamsholt S0rensen) writes:
>  Nope. ANSI states, that floats remain floats unless the expression
>contains more precise types, that is long double or double, in which
>case the float in converted.

This is *almost* right.  The ANSI C standard does not say that a
compiler cannot do `float f = 0.1; f += f;' as `convert f to double;
add double to double; convert result to float', but it does give
the compiler the option of doing it as `add float to float'.

(This is similar to the fact that the standard does not require a
compiler to, say, not insert delay loops after every few instructions
---except that the delay loops would be better proof of a stupid
compiler!  In other words, the standard does not, and cannot, outlaw
bad code generation.)

>Therefore, if the float is the only parameter to a function it is
>*not* converted to a double in the expression, regardless of how the
>function was declared.

This, however, is almost completely wrong.

When an actual parameter has type float, the value is not altered
*only* when a function prototype is in scope and the corresponding
formal parameter also has type float.  If no prototype is in scope,
or if the prototype has run into a `...', the value is widened.

This is easily demonstrated by noting that

	float f; double d; ...
	(void) printf("%f %f\n", f, d);

is correct: we do not need `%lf' to print d.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list