float vs double in function args

8 views
Skip to first unread message

Phil Howard

unread,
Aug 5, 1998, 3:00:00 AM8/5/98
to
I know that if I define a function with an argument type float, it is
really promoted to double. But what about an argument that is a struct
containing a float, or more than one float? For example:

struct complex_float {
float real,imaginary;
};
typedef struct complex_float complex_float;

complex_float complex_add(complex_float a, complex_float b)
{
complex_float s;
s.real = a.real+b.real;
s.imaginary = a.imaginary+b.imaginary;
return s;
}

--
-- *-----------------------------* Phil Howard KA9WGN * --
-- | Inturnet, Inc. | Director of Internet Services | --
-- | Business Internet Solutions | eng at intur.net | --
-- *-----------------------------* philh at intur.net * --

Ben Pfaff

unread,
Aug 5, 1998, 3:00:00 AM8/5/98
to
NOS...@intur.net (Phil Howard) writes:

I know that if I define a function with an argument type float, it is
really promoted to double.

Not if you use a function prototype.



But what about an argument that is a struct
containing a float, or more than one float? For example:

Float objects always stay floats. Float arguments without a function
prototype are promoted to double.
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
do not ask me C questions via email; post them instead

Richard Stamp

unread,
Aug 6, 1998, 3:00:00 AM8/6/98
to
In article <6qanl2$gej$1...@cezanne.intur.net>,

Phil Howard <NOS...@intur.net> wrote:
>I know that if I define a function with an argument type float, it is
>really promoted to double.

No, that's only true if
(1) you call the function without a prototype in scope, or
(2) it's a variable argument to a variadic function like 'printf'.

So if I prototype

void foo (float bar);

and then call 'foo', the argument is not promoted to 'double'. Notice
that it would be quite incorrect to call this function without a prototype
because then the promotion would take place and the function would receive
the "wrong" argument type (receives a double when expecting a float).

>But what about an argument that is a struct
>containing a float, or more than one float? For example:

These are never converted even in the cases mentioned above.

Cheers,
Richard
--
Richard Stamp
Churchill College, Cambridge

User923005

unread,
Aug 6, 1998, 3:00:00 AM8/6/98
to
Richard:
But it is very, _very_, *very* bad style to use a function without a prototype
in scope. So, for good programming, expect a promotion. Unless you use a
pointer to the object (in which case there is danger of modification in the
function) or you wrap it in a struct (which is ugly, don't you think?).

This is one of those unfortunate design decisions in the language. A good idea
at the time, but now looks a bit ugly. Personally, I don't use float at all,
unless I really, really have to. Memory is cheap, and accuracy from a larger
type is more than worth the cost in space.

Except when it's not. Never speak in absolutes. Never say never.

rats... I just did.

--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software? Algorithms? Pubs? http://www.infoseek.com

Nils Goesche

unread,
Aug 7, 1998, 3:00:00 AM8/7/98
to
>>>>> "User923005" == User923005 <user9...@aol.com> writes:

User923005> This is one of those unfortunate design decisions in the
User923005> language. A good idea at the time, but now looks a bit
User923005> ugly. Personally, I don't use float at all, unless I
User923005> really, really have to. Memory is cheap, and accuracy from
User923005> a larger type is more than worth the cost in space.

User923005> Except when it's not. Never speak in absolutes. Never say
User923005> never.

User923005> rats... I just did.

As far as I know, Quake2 uses floats instead of doubles. And I guess it's for
speed reasons...
--
Nils Goesche
My obscure opinions are my very own.


Reply all
Reply to author
Forward
0 new messages