Q:

Wikipedia's line intersection formula can't handle large numbers?

I'm working on a little game that needed a function to calculate the point of intersection of two lines. So I got this formula off Wikipedia (http://en.wikipedia.org/wiki/Line-line_intersection) and made it into a function.
bool lineline(int L1X1, int L1Y1, int L1X2, int L1Y2, int L2X1, int L2Y1, int L2X2, int L2Y2, int* X, int* Y) { // Returns the point of intersection of two lines
    int D = (L1X1 - L1X2) * (L2Y1 - L2Y2) - (L1Y1 - L1Y2) * (L2X1 - L2X2); // Denominator. If zero then no intersection

    if (D == 0) { // Parallel and possibly overlapping
        return false;
    } else {
        *X = ( (L1X1 * L1Y2 - L1Y1 * L1X2) * (L2X1 - L2X2) - (L1X1 - L1X2) * (L2X1 * L2Y2 - L2Y1 * L2X2) ) / D; // Calculate x
        *Y = ( (L1X1 * L1Y2 - L1Y1 * L1X2) * (L2Y1 - L2Y2) - (L1Y1 - L1Y2) * (L2X1 * L2Y2 - L2Y1 * L2X2) ) / D; // Calculate y

        std::cout << D << " | " << *X << " | " << *Y << "\n";

        if (*X >= Bmin(L1X1, L1X2) && *X <= Bmax(L1X1, L1X2) && *Y >= Bmin(L1Y1, L1Y2) && *Y <= Bmax(L1Y1, L1Y2)) {
            // Intersection is on first line
            if (*X >= Bmin(L2X1, L2X2) && *X <= Bmax(L2X1, L2X2) && *Y >= Bmin(L2Y1, L2Y2) && *Y <= Bmax(L2Y1, L2Y2)) {
                // Intersection is on second line
                return true;
            } else {
                // Intersection is on first, but not on second line
                return false;
            }
        } else {
            // Intersection is not on first line.
            return false;
        }

        return true;
    }
}

It works pretty well, for example when I call it with these arguments it returns true
lineline(400, 0, 400, 2000, 0, 400, 2000, 400, &x, &y);

However, when I move the second line up 1300 units...
lineline(400, 0, 400, 2000, 0, 1700, 2000, 1700, &x, &y) == false;

It returns false. While the two lines from the second function call should intersect, right? The values it calculates with these parameters are:
D = -4000000
*X = 400;
*Y = -447;

Can anyone help me out with this? I've been stuck on this for a day now and I'm probably just missing something simple, just like last time, but I can't see it. Thanks in advance!

A:

The formula raises the differences between the input numbers to the third degree, so if the differences have roughly three digits, you should be careful about overflowing an int, which has nine digits, plus the first digit can go up to 2. once you overflow an int, you start seeing negative numbers as a result of multiplying positive ones, so the rest of your calculations becomes incorrect.
To improve the range, use 64-bit integers (i.e. long long) for your intermediate results.

