Path: utzoo!attcan!uunet!fernwood!decwrl!ucbvax!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!brutus.cs.uiuc.edu!psuvax1!rutgers!att!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: float parameters in C++; type rewriting? Message-ID: <10163@alice.UUCP> Date: 19 Nov 89 06:36:47 GMT References: <1000037@hpclisp.HP.COM> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 63 In article <1000037@hpclisp.HP.COM>, shankar@hpclisp.HP.COM (Shankar Unni) writes: > This is about the handling of formal parameters of type "float" in C++. > The 2.0 language spec seems to imply that these floats should be treated as > floats, and that any parameter passed to such functions should be passed as > a float. Correct. > In actuality, what seems to be happening is that in the generated K&R C > code (the default), an old-style definition is put out (causing the "float" > parameter to be treated by the compiler as a double), and, in the absence > of prototypes, all actual parameters are also promoted to double. Yup. If you ask cfront to generate ANSI C and you have a C compiler that accepts it, it should be better about it. If your C compiler doesn't provide first-class support for `float' and your C++ compiler generates C, what can one do? > In addition, the translator is really spotty about casting the actual > parameters to type "float" at the point of call. It sometimes does the > cast, and then again, often it does not. Examples, please? > The reason this is an issue is for C++ *compiler* writers (those that > generate code without going through a C compiler). > What does the language spec say about this type-rewriting? Should it > happen? It is unspecified, or does the language want to commit one way or > another (say, do what ANSI C does)? It should behave like ANSI C. > If C++ is supposed to behave like C in this regard, where does that leave > the translator? The best one can do, faced with the task of generating C for a C compiler that only knows how to pass double arguments, is to pass floats as doubles with appropriate casts in the right places to ensure the correct values are actually passed. Cfront does a pretty good job of this. For example: extern void f(float); main() { double d = 3; f(d); f(3); f(3.0); } In each of these cases, the code generated by cfront (2.0) casts the argument to float to pass it to f. If you know of cases where this is not being done and should be, please let me know. -- --Andrew Koenig ark@europa.att.com