Why Java floating-point division expression gives different result when numerator and denominator are calculated separately? [closed]

112
June 12, 2021, at 00:40 AM
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 9 hours ago.

Improve this question

I am using Android Studio 4.1.3 with JRE 1.8. In the following code, if numerator and denominator are calculated separately and then divided, looks like we get the right answer. But the combined division expression gives a different answer.

public class DesktopLauncher {
public static class Vector2  {
    public float x;
    public float y;
    public Vector2(float _x,float _y){x=_x; y=_y;}
}
private static float calcDist(Vector2 a, Vector2 b, Vector2 c) {
    double distNumerator =(a.x - b.x) * (a.y - c.y) - (a.y - b.y) * (a.x - c.x);
    double distDenominator =  Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
    double dist = distNumerator/distDenominator;
    // Using a single expression to calculate distance: dist2 not equal to dist.
    // There is a problem with this calculation - float division vs. double division issue ?
    double dist2 = (a.x - b.x) * (a.y - c.y) - (a.y - b.y) * (a.x - c.x)
            /
            Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
    System.out.println(" dist = "+dist + " dist2 = "+dist2);
    return (float) dist;
}
public static void main (String[] arg) {
    // reproduce problem with the numbers used when the problem was noticed
    calcDist(new Vector2(587.0f, 487.0f),
            new Vector2(809.15f, 223.27f),
            new Vector2(617.47f, 502.63f));
}

}

I expected dist and dist2 to have the same value. But the output is dist = 33.373617871708824 dist2 = 3495.510193353419

Why dist and dist2 do not have the same value? Is it a float vs. double issue?

Answer 1

No, this is not a case of the floating-point wackyiness. It's simply missing parenthesis:

double dist3 = ((a.x - b.x) * (a.y - c.y) - (a.y - b.y) * (a.x - c.x))
    / Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));

This calculation will produce the same result as dist.

You only devided the (a.y - b.y) * (a.x - c.x) part by the square root and subtracted the result of that division from (a.x - b.x) * (a.y - c.y).

Note that usually when the double/float difference or just general floating point imprecision is the problem then the difference will not be as big as this, it's usually an error a few digits after the most significant. While there are some operations where such small errors can grow quite rapidly and become huge, those are the exception and not the rule. So when you see such a big difference, you should first look at other causes for the difference before suspecting floating-point shenanigans.

Answer 2

These codes are not same.

double dist2 = (a.x - b.x) * (a.y - c.y) - (a.y - b.y) * (a.x - c.x)
        /
        Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));

You expect to run these parts first

(a.x - b.x) * (a.y - c.y) - (a.y - b.y) * (a.x - c.x)

Then you expect these part run

Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2))

But this is not the case.

If you run these code like that way you will run this part first

(a.x - b.x) * (a.y - c.y)

Then this code will run

(a.y - b.y) * (a.x - c.x) = Answer (We call these calculation as Answer)

Then these part

Answer/Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2))

So your expected result will not be match. You should run these code like that way.

double dist2 = (((a.x - b.x) * (a.y - c.y)) - ((a.y - b.y) * (a.x - c.x))) / (Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2)));
Rent Charter Buses Company
READ ALSO
laravel roles and permission with sanctum

laravel roles and permission with sanctum

i use this website https://wwwitsolutionstuff

199
Preventing auto input on form field Javascript

Preventing auto input on form field Javascript

I have noticed something on a number of websites now where auto filling form fields is not accepted by the form

100
How do i make a modal disappear after button has been clicked?

How do i make a modal disappear after button has been clicked?

I need to add a exit button to the top left corner of the modal, also I would like for the modal to disappear after the link has been copiedHow would i go about doing this ?

37