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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raylopez99

    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

  • =?windows-1252?Q?Arne_Vajh=F8j?=

    #2
    Re: no compiler warning for int to double conversion with no cast...strange

    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

    Comment

    Working...