Can anyone explain the output process in java - java

int p=15, q=12, s;
s=p/q*++p%(++p%--q);
System.out.print(s);
I am trying it according to the precedence of operators and associativity but I think it should give me the output 2 but it is giving me 4 as output.

Precedence
p/q*++p%(++p%--q)
is computed as
((p / q) * (++p)) % ((++p) % (--q))
using the operator precendece rules.
Note that ++x/--x increment/decrement the variable by one and then return the result after the operation. Since they change the variable itself, the effect obviously carries over to other usages of the variable.
Evaluation
It is then evaluated left to right, the math is
((15 / 12) * (16)) % ((17) % (11))
which results in 4:
((15 / 12) * (16)) % ((17) % (11))
= (1 * 16) % 6
= 16 % 6
= 4

Related

Why does result of Math.pow() and * differ while calculating aswers in java ? and how do java calculate math in run-time memory?

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 ?
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

How to evaluate the java operators evaluation

I am java newbie and trying to understand how calculation steps are performed to achieve final result. The final answer is coming as 49. Looking at precedence operators hierarchy my calculation is not coming to 49.
Following is my code with expression:
class Test
{
public static void main(String args[])
{
int a = 6, b = 5;
a = a + a++ % b++ *a + b++ * --b;
System.out.print(a)
}
}
Modulo has same precedence as division and multiplication. The next precedence goes to addition and subtraction.Operations happen left to right.
Adding brackets for better clarity:
( a ) + ( a++ % b++ *a ) + ( b++ * --b )
group I II III
a++ % b++ *a evaluated as :a. (6 % 5) = 1
b. 1 * 6 till now, a = 6, b = 5
Having completed this operation, the value of a & b are incremented to 7 & 6 respectively. i.e the post fix increment happens only after the modulo/multiplication/division during a step or group as shown above.
b++ * --b => 6 * 6. gives value of 36
Having completed this operation the ++ operation is performed but -- is also performed, effectively leaving the value of b at 6.
next operation is addition. i.e 7 + 6 + 36 = 49
Reference: https://www.programiz.com/java-programming/operator-precedence
Operators have priorities. There is a table of these here.
Also, you can use round brackets (parentheses) for forcing the right sequence of calculations.

Java Arithmetic expression

The following expression evaluates to 14.
int a=4;
int b=6;
int c=1;
int ans= ++c + b % a - (c - b * c);
System.out.print(ans);
This is how i calculate this
1. (c - b * c) // since bracket has highest preference
ans : -5
2. ++c //since unary operator has next highest preference
ans : 2
3. b%a // % has higher preference than + and -
ans : 2
Therefore, 2 + 2 - (-5) = 9
As you can see I'm getting 9 as the value. Not sure what's wrong in my way of calculation (pretty sure I'm gonna end up looking stupid)
Edit : I refered to the below link for precedence and association.
https://introcs.cs.princeton.edu/java/11precedence/
Can someone explain the difference between level 16 and level 13 parentheses? I thought level 13 parentheses is used only for typecasting. That is why i considered level 16 parenthesis for evaluating the expression.
Evaluation order is not the same as precedence. Java always evaluates left-to-right (with a minor caveat around array accesses).
Effectively, you are evaluating the following expression, because the very first thing that happens is the pre-increment of c:
2 + 6 % 4 - (2 - 6 * 2)
Precedence then describes how the values are combined.
As you are using pre-increment operator on c. So, after applying increment on c the value of c will be 2. Now :
(c - b * c) will be evaluated to (2 - 6 * 2)= -10
So, the final expression will be 2 + 2 - (-10) = 14

Java/Pawn Maths formula returns 0

could someone explain me what am I doing wrong here, and how can I make this formula to be working? right now it returns 0 in Java and Pawn. But it works in PHP, which isnt making much sense for me.
int test = (65 / 65 / (1 + 25) * 10000);
In Java, integer division will truncate the results. For example, 5/2 will truncate 2.5 to 2.
To ensure you're using float with numeric constants, add a .0 on the end, as in:
int test = (65.0 / 65.0 / (1.0 + 25.0) * 10000.0);
Java operations for multiplication and division is left to right, so your expression is really:
(((65 / 65) / 26) * 1000) // Clarifying parenthesis
((1 / 26) * 1000)
(0 * 1000) // integer division!
0
To avoid this, you just need to ensure the operations are casted to a double.
The simple way would be just changing the first value to a double, either by:
int test = (65D / 65 / (1 + 25) * 10000);
Or
int test = (65.0 / 65 / (1 + 25) * 10000);
However, if you are refactoring your code later, you might want to change more than just the first value.

Arithmetic operator confusion

Why I'm getting two different values while using the arithmetic operators for the same value of variables. I've just altered little bit my second program, which is resulted in giving me the different output. Could anyone please tell me why?
int number=113;
int rot=0;
rot=number%10;
rot*=100+number/10;
System.out.println(rot);//333
int number=113;
int rot=0;
rot=number%10;
rot=rot*100+number/10;
System.out.println(rot);//311
In the first part you compute
rot *= 100 + number/10
which is
rot = rot * (100 + number/10)
And in the second part:
rot = rot*100 + number/10
Note that multiplication and division goes before addition and substraction.
the problem is that *= has different (lower) precedence than * and +
rot *= 100 + number/10;
is equavalent to
rot = rot * (100 + number /10);
operator precdence can be found here
It seems like the problem is operator precedence.
What this means is that num * 10 + 13 is treated like (num * 10) + 13, i.e. the () are automatically added according to the rules of the language.
The difference then, in your example, is that the first one means the following:
rot*=100+number/10;
// Is the same as this:
rot = rot * (100 + (number / 10));
Whereas the second one means the following:
rot=rot*100+number/10;
// Is the same as this:
rot = (rot * 100) + (number / 10);
Since the parenthesis are in different places, these probably evaluate to different numbers.
in the second code because of the high precedence of *. rot*100 will be calculated and to that (number/10) will be added so its (300 + 11 = 311).

Categories

Resources