470,599 Members | 1,445 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,599 developers. It's quick & easy.

unmanaged float / double question

Hi,

i have a simple question that has bothered me for some time now.

if i have a function foo( double a)
and i do something like

float b = 2.0;
foo(b);

* can i safely assume that the compiler will convert b to a double?

and if i do foo(20) //notice that i coded an integer number

* will vc do the correct conversion?

i ask this because recently i had to work with an archaic HP-UX compiler
that tried to use a binary represtation of the integer '20' as a floating
point. i was wondering if vc is smarter. if it is not, where should i look
for documentation about this?
i used to think that this was a non-issue, but apparently this is not always
true.

* and while i am on that subject, is it correct that there is no way for
format specifiers to work with double precision floats?

kind regards,
Bruno.
Nov 17 '05 #1
1 1500
Hi,

C++ does this for you - you'll get a warning about type conversion from the
double -> float conversion in
float b = 2.0
and the integer -> double conversion should go OK without any warnings.
However, as a rule, it's best not to rely on it - you'd be better checking
your types yourself since type conversion can lead to trouble (most
noticeably with unsigned / signed conversion). There's a compiler option to
turn off implicit conversion (treat it as an error).

HTH,

Steve

"Bruno van Dooren" <mi******@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

i have a simple question that has bothered me for some time now.

if i have a function foo( double a)
and i do something like

float b = 2.0;
foo(b);

* can i safely assume that the compiler will convert b to a double?

and if i do foo(20) //notice that i coded an integer number

* will vc do the correct conversion?

i ask this because recently i had to work with an archaic HP-UX compiler
that tried to use a binary represtation of the integer '20' as a floating
point. i was wondering if vc is smarter. if it is not, where should i look
for documentation about this?
i used to think that this was a non-issue, but apparently this is not always true.

* and while i am on that subject, is it correct that there is no way for
format specifiers to work with double precision floats?

kind regards,
Bruno.

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Christian Christmann | last post: by
2 posts views Thread by JohnO | last post: by
1 post views Thread by Amir Kolsky via .NET 247 | last post: by
2 posts views Thread by The unProfessional | last post: by
4 posts views Thread by William F. Kinsley | last post: by
8 posts views Thread by abdul_n_khan | last post: by
13 posts views Thread by Shirsoft | last post: by
reply views Thread by MottetCCSF | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.