[comp.sys.sgi] Passing floats - Is this a bug?

fjhenigman@watcgl.waterloo.edu (Frank J. Henigman) (10/06/89)

Take a look at the following program.

------------------------------------------------------------------
bar(x)
  float *x;
  {
  printf("bar:  %f\n", *x);
  }

foo(x)
  float x;
  {
  printf("foo:  %f\n", x);
  bar(&x);
  }

main()
  {
  float x = 1.0;
  foo(x);
  }
------------------------------------------------------------------

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@watcgl.uwaterloo.ca                       Computer Graphics Lab
fjhenigman@watcgl.waterloo.edu   Frank J. Henigman   University of Waterloo
 ...!watmath!watcgl!fjhenigman                       Waterloo, Ontario, Canada
-- 
fjhenigman@watcgl.uwaterloo.ca                       Computer Graphics Lab
fjhenigman@watcgl.waterloo.edu   Frank J. Henigman   University of Waterloo
 ...!watmath!watcgl!fjhenigman                       Waterloo, Ontario, Canada

dat@ORVILLE.NAS.NASA.GOV (Dave Tristram) (10/06/89)

  > Here is the output this program compiled and run on a 4D/120GTX with 3.1G
  > 
  > foo:  1.000000
  > bar:  1.875000
  > 
  > 

try declaring the parameters as doubles.  I've had problems using the 
ansi style declarations like

   foo( float )

has something to do with automatic promotion of arguments, etc...

goss@SNOW-WHITE.MERIT-TECH.COM (Mike Goss) (10/06/89)

In reply to:
> Date: 5 Oct 89 20:41:51 GMT
> From: "Frank J. Henigman" <watmath!watcgl!fjhenigman@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@watcgl.uwaterloo.ca                       Computer Graphics Lab
> fjhenigman@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@snow-white.merit-tech.com

mg@cidam.me.rmit.oz.AU ("Mike A. Gigante") (10/08/89)

yes, I have seen the same problem (in the middle of a large program)

The -float doesn't help as it doesn't prevent the promotion of arguments
to double, merely the promotion of expressions to double.

the way to *avoid* the bug is to

#define float double

horrid, but it works on my program. Haven't ttried it on your test case 
(this machine ain't an iris)

Mike Gigante, ACGL, RMIT Australia