Path: utzoo!attcan!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!att!cbnewsj!asd From: asd@cbnewsj.ATT.COM (Adam S. Denton) Newsgroups: comp.lang.c Subject: Re: log10(8) Message-ID: <4033@cbnewsj.ATT.COM> Date: 28 Feb 90 16:06:39 GMT References: <90058.153054CMH117@psuvm.psu.edu> Reply-To: asd@cbnewsj.ATT.COM (Adam S. Denton) Organization: AT&T Bell Laboratories, Middletown, NJ Lines: 83 In article <90058.153054CMH117@psuvm.psu.edu> CMH117@psuvm.psu.edu (Charles Hannum) writes: > >In article , meissner@osf.org (Michael >Meissner) says: >> >>| Try printf"%lf",l); after all l is declared to be a double!!!!!!!! > >>Sigh. Printf is a varargs function. This means that there is no way >>a 'float' can be passed to it. Thus %f and %lf are synomous..... > >As I don't have access to my copy of K&R2 until Monday, could you please point >me to a specific reference? > >BTW: I know of at least one compiler that *does* pass floats as floats, not > doubles. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I question this. IMHO there is no way you could ever find this out, unless you have looked at the assembly-language code emitted by your compiler. If your compiler is non-ANSI (i.e. K&R1/UNIX vintage), then floats can NEVER be passed to functions, though they might appear to. For example, void floatfun(arg) float arg; { ... } main() { float value; value = 1.0; floatfun(value); ... } What happens in main() is that value is converted (as if by cast) to type 'double', then transmitted as a double to floatfun(). The code for floatfun will contain a routine to convert its argument from 'double' to 'float' as soon as floatfun() is entered. Although this might seem silly, it is in fact what happens. If you dig up a K&R1, you will find that this behavior is explicitly blessed. If you have an ANSI compiler, then the same thing happens (floats are automatically converted to double when passed to functions), EXCEPT that it is possible to circumvent this conversion under two conditions: 1) A *complete* prototype of the function is in effect when the call to the function is compiled; and 2) The function is declared explicitly with a 'float' type argument in its argument list (between the parentheses). For example, void floatfun(float arg); main() { floatfun(2.0); } will pass 2.0 to floatfun as a float. (Incidentally, one could write 2.0F to explicitly show that 2.0 is a float constant, not double...) ...printf(), however, does not meet condition (2) above. The declaration for printf() is void printf(char *s, ...) (^^^^ I think) which does *not* contain the explicit type `float' in the argument list; therefore, all floats will be converted to double when it is called. It is possible, also, that float and double have EXACTLY THE SAME PRECISION on your machine/compiler. Never was it said (in K&R) or standardized that `double' must have greater precision than `float.' Maybe that's what you're observing... Also, I don't believe that "%lf" is blessed by the ANSI standard in ...printf(), although the Turbo-C guide does mention it, interestingly. The correct way to output floats (actually, doubles) is "%f". However, "%lf" is of course necessary for scanf()... Hope this cleared things up a little. Adam asd@mtqua.att.com