0

I was trying to execute simple java program to calculate result with expression as: v^2 - u^2 / 2as

that is v*v - u*u / 2*a*s

code is in java 11

int v=16;
int u =5;
int a = 7;
int s = 9;
int res1 = v*v;
int res2 = u*u;
double FunRes1 =  Math.pow(v, 2);
double FunRes2 =  Math.pow(u, 2);
int part1 = res1 - res2;
int part2 = 2 *a*s;
int result = part1/part2;                             // = All 4
int AllResult = (v*v-u*u)/2*a*s;                     // == results 
double doubleResult = FunRes1-FunRes2 / 2*a*s;      // === have different
double doubleResult2 = (FunRes1-FunRes2) / 2*a*s;  // ==== answers (see ss above)

the asnwers of all 4 variable ( result , AllResult , doubleResult1 , doubleResult2 ) are different . Anyone explain why this happen ? and What is the correct answers mathematically ?

shmosel
  • 49,289
  • 6
  • 73
  • 138
  • 1
    You should read up on operator precedence. Why the results are different should be obvious: operators are applied in a different order, e.g. by putting parentheses around expressions. – Thomas Nov 03 '22 at 06:49
  • 1
    all are doing different calculations e.g `a - b / c * d` or `a-b / c-d` are the same as `a - ( (b / c) * d )`; also `a / b * c` or `a / b*c` are same as `(a / b) * c` (spaces do not change the *precedence/associativity* of operators) (also integer calculation is not same as floating point one) – user16320675 Nov 03 '22 at 07:07
  • 1
    BTW please read [Why should I not upload images of code/DATA/errors?](https://meta.stackoverflow.com/a/285557/16320675) – user16320675 Nov 03 '22 at 07:18

1 Answers1

1

This is because of operator precedence. If I write something like 2 * 8 / 8 - 6, without further context, it is ambiguous how it should be evaluated. This can lead to different results. For example (2 * 8) / (8 - 6) == 8 but ((2 * 8) / 8) - 6 == -4. To disambiguate, Java uses a list of precedence rules that are common throughout most languages. You can find the complete list here.

For your case the important part is that multiplication and division are applied before addition and subtraction.

Also of note is what happens in the case of operators having equal precedence, which also appears in your example.

When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

Going back to our example of 2 * 8 / 8 - 6. We can see (from here) that java will evaluate multiplication and division before subtraction. * and / have the same precedence and are both binary operators so we evaluate them left to right. This leaves us with ((2 * 8) / 8) - 6 which is why java evaluates this as -4

System.out.println(2 * 8 / 8 - 6);

-4

Marcus Dunn
  • 487
  • 3
  • 7
  • 2
    Since `2 * 8 / 8` would yield the same example no matter the execution order (16/8 = 2 and 2/1 = 2) I'd like to add another to make the left to right application clear: `4 / 2 * 2` would yield `4` (`(4/2) * 2 = 2 * 2 = 2`) instead of `1` (which would mean `4 / (2 * 2) = 4 / 4 = 1`). This is why `AllResult` is different from `result`. – Thomas Nov 03 '22 at 06:54
  • 1
    actually `2 * 8 / 8 - 6` is not ambiguous according the Java Language Specification. – user16320675 Nov 03 '22 at 07:11