Passing floats - Is this a bug?
Mike Goss
goss at SNOW-WHITE.MERIT-TECH.COM
Fri Oct 6 23:09:24 AEST 1989
In reply to:
> Date: 5 Oct 89 20:41:51 GMT
> From: "Frank J. Henigman" <watmath!watcgl!fjhenigman at iuvax.cs.indiana.edu>
> Organization: U of Waterloo, Ontario
> Subject: Passing floats - Is this a bug?
>
> Take a look at the following program.
>
.
.
.
>
> Here is the output of this program compiled and run on a 4D/120GTX with 3.1G
>
> foo: 1.000000
> bar: 1.875000
>
> You get the same thing EVEN IF COMPILED WITH -float.
>
>
> Compile it with gcc and you get:
>
> foo: 1.000000
> bar: 1.000000
>
> --
> fjhenigman at watcgl.uwaterloo.ca Computer Graphics Lab
> fjhenigman at watcgl.waterloo.edu Frank J. Henigman University of Waterloo
> ...!watmath!watcgl!fjhenigman Waterloo, Ontario, Canada
This is due to an ambiguity in the C language regarding passing parameters
of type "float". If you don't use an ANSI style function prototype, C
promotes all floating point function arguments to type "double". Therefore,
you can never really get a function argument of type "float". There are
two schools of thought among compiler writers on handling this problem.
Some (shown by your first example on the IRIX C compiler) assume that
if you declare an argument as "float" that you really meant "double".
Others (such as gcc) assume that since you want a "float" argument,
they should convert the "double" argument to "float".
The best way around this would be to use ANSI style function prototypes
for all functions; this causes the compiler to generate code to pass
the argument exactly as it is declared in the prototype. The compiler
does type checking of arguments and performs type conversion (for example,
int to float or float to double) where possible, and issues an error
message otherwise (for example, passing a pointer to a float parameter).
Unfortunately, the ANSI C standard has not been finalized yet, and not
all C compilers implement even the draft standard yet. The IRIX C
compiler implements some of the ANSI features, including function
prototypes, but not others (such as "void" pointers).
------------------------------
Mike Goss
Merit Technology Inc.
(214)733-7018
goss at snow-white.merit-tech.com
More information about the Comp.sys.sgi
mailing list