integer division

Discussion in 'C++' started by Darius Fatakia, Jan 28, 2004.

  1. hi,
    i'm new to MS Visual Studio and C++ but not to C programming in general. i'm
    trying to divide two integers and get their actual quotient (eg 5/3 =
    1.666667 etc). i thought i had type cast correctly, but please let me know,
    because it appears to be rounding off. my code follows. Thanks!

    void DrawLine(GLint x1, GLint y1, GLint x2, GLint y2)

    {
    int i, pk, dy, dx, yc, xc, inc, temp;
    float m;
    /*
    x1 = 100;
    y1 = 200;
    x2 = 110;
    y2 = 206;
    */

    if((x1 >= x2) && (y1 >= y2)) {
    temp = x1;
    x1 = x2;
    x2 = temp;
    temp = y1;
    y1 = y2;
    y2 = temp;
    }

    dy = y2 - y1;
    dx = x2 - x1;
    if (dx != 0) m = (float)(dy/dx);
    else m = -999;

    // Debugging info
    printf("\nDrawLine called!\n");
    printf("x1 = %d\n", x1);
    printf("y1 = %d\n", y1);
    printf("x2 = %d\n", x2);
    printf("y2 = %d\n", y2);
    printf("dx = %d\n", dx);
    printf("dy = %d\n", dy);
    if (dx != 0) printf("dy/dx = %d\n", dy/dx);
    printf("m = %.2f\n", m);
    }

    >>>OUTPUT:


    DrawLine called!
    x1 = 286
    y1 = 204
    x2 = 309
    y2 = 187
    dx = 23
    dy = -17
    dy/dx = 0
    m = 0.00
     
    Darius Fatakia, Jan 28, 2004
    #1
    1. Advertising

  2. Darius Fatakia

    Mike Wahler Guest

    "Darius Fatakia" <> wrote in message
    news:bv736u$kh0$...

    > if (dx != 0) m = (float)(dy/dx);


    (float)dy / dx;

    -Mike
     
    Mike Wahler, Jan 28, 2004
    #2
    1. Advertising

  3. Darius Fatakia

    Howard Guest

    "Mike Wahler" <> wrote in message
    news:JwERb.29445$...
    >
    > "Darius Fatakia" <> wrote in message
    > news:bv736u$kh0$...
    >
    > > if (dx != 0) m = (float)(dy/dx);

    >
    > (float)dy / dx;
    >
    > -Mike
    >
    >


    Just to elaborate on WHY Mike's method is required: in the statement
    (float)(dy/dx), the results of (dy/dx) are calculated BEFORE the cast to
    float, and since dx and dy are integers, that division is done as an integer
    division, which drops the fractional part entirely. Casting either of the
    values themselves (dy or dy), the division then becomes a floating-point
    division because at least one of its parts is floating-point.

    -Howard
     
    Howard, Jan 28, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    12
    Views:
    1,947
  2. genlock
    Replies:
    22
    Views:
    26,437
    vipinlal
    Mar 17, 2010
  3. Sidney Cadot

    integer division towards -infinity

    Sidney Cadot, Jul 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    637
    Glen Herrmannsfeldt
    Jul 11, 2003
  4. Darius Fatakia

    integer division

    Darius Fatakia, Jan 28, 2004, in forum: C Programming
    Replies:
    9
    Views:
    496
    Richard Bos
    Jan 29, 2004
  5. Replies:
    94
    Views:
    4,689
    ┬Ča\\/b
    Feb 9, 2007
Loading...

Share This Page