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

  • Thread starter Thread starter raylopez99
  • Start date Start date
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
 
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
 
Back
Top