Problem! (Easy for most of you i'm sure)

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

    Problem! (Easy for most of you i'm sure)

    I'm a C++ newbie, just been starting out today... I really can't get my head
    around this...


    *************** **

    int main()
    {
    int one = 1;
    int number = 43;

    double result = one / number;
    double result2 = static_cast<dou ble>(one / number);

    cout << result << endl << result2;

    return 0;
    }

    *************** **

    OUTPUT:

    0
    0

    *************** **


    I dont understand!!! :-( please help

    thanks!


  • Ron Natalie

    #2
    Re: Problem! (Easy for most of you i'm sure)


    "Eurig Jones" <eurig@one47.co m> wrote in message news:boeg1j$hve $1$830fa795@new s.demon.co.uk.. .
    [color=blue]
    > int one = 1;
    > int number = 43;[/color]

    These are two integers.
    [color=blue]
    >
    > double result = one / number;[/color]

    Ignore the stuff to the left of the = sign for now. The expression
    one/number
    has value zero, becaue integer divide throws away the fractional values.
    1/43 is 0, 42/43 is 0, 43/43 is 1.

    So in effect you have:
    double result = 0;

    The int zero is converted to double and initializers result. You have a double value of
    zero.
    [color=blue]
    > double result2 = static_cast<dou ble>(one / number);[/color]

    The same thing happens here. The sub expression one/number is still 0.
    double result2 = static_cast<dou ble>(0);
    The static cast converts it to double, but it's still zero.

    If you want to do a floating point divide, you must convert at least one of the
    operands to a floating point type (if you convert only one, the compiler will
    automatically convert the other to match).

    double result = static_cast<dou ble>(one) / number;

    for example. This convets the 1 to a double. The compiler now does the divide,
    converting number to double to match (there are a list of "usual artithmatic conversions"
    that gets applied to operators when the types of the operands don't match, generally
    the least precise of the two is converted to the more precise).

    The result of this expression is the 1.0/43 or about .023...


    Comment

    • Severin Ecker

      #3
      Re: Problem! (Easy for most of you i'm sure)

      hi!
      [color=blue]
      > double result = one / number;
      > double result2 = static_cast<dou ble>(one / number);[/color]

      if i'm not completely wrong, the divisions are calculated using integer
      division. both operands are integer so integer div. is used (and in the
      second case, the result!! is casted to double....)
      you must convert (at least) one operand to the precision you want the
      calculation to take place in, the the second operand is implicitly converted
      to the greater type.
      (though i think it's better and better style to cast both to double)

      double result = static_cast<dou ble>(one) / static_cast<dou ble>(number);

      should do the trick.

      regards,
      sev


      Comment

      • Ralf

        #4
        Re: Problem! (Easy for most of you i'm sure)

        > int main()[color=blue]
        > {
        > int one = 1;
        > int number = 43;
        >
        > double result = one / number;
        > double result2 = static_cast<dou ble>(one / number);
        >
        > cout << result << endl << result2;
        >
        > return 0;
        > }
        >
        > *************** **
        >
        > OUTPUT:
        >
        > 0
        > 0
        >
        > *************** **[/color]

        The output is correct! The two variables "one" and "number" are integer, so
        the result of "one/number" ist
        integer too (0). And this you are converting to double which ist remaining
        0.

        Try the following code:

        int main()
        {
        int one = 1;
        int number = 43;

        double result = (double)one / (double)number;

        cout << result << endl << result2;

        return 0;
        }

        If one of the operands is of the type "double", the operation is a floating
        point operation.



        Ralf

        Seminarbeschreibungen zu C und C++







        Comment

        • John Brown

          #5
          Re: Problem! (Easy for most of you i'm sure)

          > I'm a C++ newbie, just been starting out today... I really can't get my
          head[color=blue]
          > around this...[/color]
          [color=blue]
          > I dont understand!!! :-( please help
          >
          > thanks![/color]

          Also look up the "usual rules of arithmetic conversion" (in addition to what
          others have now said).


          Comment

          Working...