469,953 Members | 2,284 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

no compiler warning for int to double conversion with nocast...strange

The below did not set off my compiler, perhaps because I set the
warnings to a higher (less nag) level.

Strange.

FYI, no question being asked.

RL

double d2030;
float f2030;

int i20 = 20;
int j30 = 30;

f2030 = i20 / j30;

d2030 = i20 / j30; //no cast needed? NO! need cast as shown below
(otherwise you get zero on LHS)

d2030 = (double) i20 / (double)j30; //cast needed as shown, to give
d2030 = 0.666

// same for f2030, cast needed as above

Oct 31 '08 #1
1 2553
raylopez99 wrote:
The below did not set off my compiler, perhaps because I set the
warnings to a higher (less nag) level.

Strange.
Not Strange.

Assigning an int to a double does not loose any precision.
>double d2030;
float f2030;

int i20 = 20;
int j30 = 30;

f2030 = i20 / j30;

d2030 = i20 / j30; //no cast needed? NO! need cast as shown below
(otherwise you get zero on LHS)

d2030 = (double) i20 / (double)j30; //cast needed as shown, to give
d2030 = 0.666
RHS is evaluated without considering.

Your problem is with integer division. It is completely
unrelated to the assignment of int to double.

And the compiler never gives warning about an integer division. It
assume that when you do an integer division that is what you meant
to do.

Arne
Nov 1 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Alf P. Steinbach | last post: by
1 post views Thread by Hafeez | last post: by
20 posts views Thread by Markus Sandheide | last post: by
8 posts views Thread by grundmann | last post: by
29 posts views Thread by junky_fellow | last post: by
3 posts views Thread by gil | last post: by
5 posts views Thread by happytoday | last post: by
2 posts views Thread by Chris Peters | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.