unmanaged float / double question

  • Thread starter Thread starter Bruno van Dooren
  • Start date Start date
B

Bruno van Dooren

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.
 
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
 
Back
Top