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

R

raylopez99

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
 
A

Arne Vajhøj

raylopez99 said:
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
 

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

Top