Implicit conversion from int to double

A

amphetaman

I know this might seem kind of silly, but is it necessary to cast int
to double?

Do I have to do

int i = 42;
double d = static_cast<double>(i);

or can I just do d = i?
 
J

Jerry Coffin

I know this might seem kind of silly, but is it necessary to cast int
to double?

Do I have to do

int i = 42;
double d = static_cast<double>(i);

or can I just do d = i?

The language allows you to do the assignment without an explicit type
cast. Somewhere, I suppose there might be a compiler that warns you
about doing it, but that's a bit hard to guess -- someday I might create
a web site of "inane compiler warnings", but I haven't yet...
 
S

Sjouke Burry

Jerry said:
The language allows you to do the assignment without an explicit type
cast. Somewhere, I suppose there might be a compiler that warns you
about doing it, but that's a bit hard to guess -- someday I might create
a web site of "inane compiler warnings", but I haven't yet...
My compileer warns, when losing precision,
so d=i is oke, i=d gives warning(warning level set high).
 
I

Ivan Novick

My compileer warns, when losing precision,
so d=i is oke, i=d gives warning(warning level set high).

This is covered in the C++ standard 4.9 "floating-integral
conversions".

Converting from an int to double should be pretty intuitive. The rule
when converting from double to int is to truncate the value.
Technically, the compiler should not have to warn in this case, but i
guess the compiler team felt normally this was an error in coding when
someone truncates by assigning a floating point to an integer.

Ivan Novick
http://www.mycppquiz.com
 
G

Greg Herlihy

I know this might seem kind of silly, but is it necessary to cast int
to double?

Do I have to do

int i = 42;
double d = static_cast<double>(i);

or can I just do d = i?

The static_cast<> is not needed to assign an int to a double. If you
still wish to make the int-to-double conversion explicit, then, I
would use the more compact, functional notation instead of a
static_cast<>:

int i = 42;
double d = double(i);

Greg
 
I

Ivan Novick

The static_cast<> is not needed to assign an int to a double. If you
still wish to make the int-to-double conversion explicit, then, I
would use the more compact, functional notation instead of a
static_cast<>:

    int i = 42;
    double d = double(i);

Greg

That being said, its still functionally identical to d=i; correct?

Ivan Novick
http://www.mycppquiz.com
 
G

Greg Herlihy

That being said, its still functionally identical to d=i; correct?

Yes. About the only conceivable reason for using the cast in this case
would be to document that the int-to-double conversion is intentional.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top