# (Error in documentation?) Math operator precedence problem

**967605**Oct 11, 2012 2:49 AM

Hello,

I apologize in advance, while I do know programming (I'm a PHP and Perl programmer) I'm fairly new to Java so this may well be a silly question, but that's why I am here. I hope you'll show me where my error is so I can finally set this to rest before it drives me mad :)

(Also, I hope I'm posting this in the right forum?)

So, I am taking a Java class, and the question in the homework related to operand precendence. When dealing with multiplication, division and addition, things were fine, the documentation is clear and it also makes sense math-wise (multiplication comes before addition, etc etc).

However, we got this exercise to solve:

If u=2, v=3, w=5, x=7 and y=11, find the value assuming int variables:

u++ / v+u++ *w

Now, according to the operator precedence table (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html) the unary operator u++ comes first, before multiplication, division and addition.

This would mean I could rewrite the exercise as:

((u+1)/v) + ((u+1)*w) = (3/3) + (4*5) = 1+20 = 21

However, if I run this in Java, the result I get is 15.

I tried breaking up the result for the two values in the Java code, so I could see where the problem is with my calculation.

For

System.out.println(u++ /v);

I get 0

For

System.out.println("u++ *w");

I get 15

My professor suggested I attempt to change the values from int to float, so I can see if the division came out to be something illogical. I did so, and now I get:

For

System.out.println(u++ /v);

I get 0.66667

For

System.out.println("u++ *w");

I get 15.0000

Which means that for the first operation (the division) the division happens on 2/3 (which is 0.6667) and only afterwards the u value is increased. That is, the u++ operation is done

That is entirely against the documentation.

This is the script I wrote to test the issue:

public class MathTest {

public static void main (String [] args) {

float u=2, v=3, w=5, x=7, y=11;

System.out.println("Initial value for 'u': " + u);

float tmp1 = u++ /v;

System.out.println("u++ /v = " + tmp1);

System.out.println("First ++ value for 'u': " + u);

float tmp2 = u++ *w;

System.out.println("u++ *w= " + tmp2);

System.out.println("Second ++ value for 'u': " + u);

System.out.println(tmp1+tmp2);

}

}

The output:

Initial value for 'u': 2.0

u++ /v = 1.0

First ++ value for 'u': 3.0

u++ *w= 20.0

Second ++ value for 'u': 4.0

21.0

Clearly, the ++ operation is done after the division and after the multiplication.

Am I missing something here? Is the documentation wrong, or have I missed something obvious? This is driving me crazy!

Thanks in advance,

Mori

I apologize in advance, while I do know programming (I'm a PHP and Perl programmer) I'm fairly new to Java so this may well be a silly question, but that's why I am here. I hope you'll show me where my error is so I can finally set this to rest before it drives me mad :)

(Also, I hope I'm posting this in the right forum?)

So, I am taking a Java class, and the question in the homework related to operand precendence. When dealing with multiplication, division and addition, things were fine, the documentation is clear and it also makes sense math-wise (multiplication comes before addition, etc etc).

However, we got this exercise to solve:

If u=2, v=3, w=5, x=7 and y=11, find the value assuming int variables:

u++ / v+u++ *w

Now, according to the operator precedence table (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html) the unary operator u++ comes first, before multiplication, division and addition.

This would mean I could rewrite the exercise as:

((u+1)/v) + ((u+1)*w) = (3/3) + (4*5) = 1+20 = 21

However, if I run this in Java, the result I get is 15.

I tried breaking up the result for the two values in the Java code, so I could see where the problem is with my calculation.

For

System.out.println(u++ /v);

I get 0

For

System.out.println("u++ *w");

I get 15

My professor suggested I attempt to change the values from int to float, so I can see if the division came out to be something illogical. I did so, and now I get:

For

System.out.println(u++ /v);

I get 0.66667

For

System.out.println("u++ *w");

I get 15.0000

Which means that for the first operation (the division) the division happens on 2/3 (which is 0.6667) and only afterwards the u value is increased. That is, the u++ operation is done

**after**the division, not before. The same happens with the multiplication; when that is carried out, the value of u is now 3 (after it was raised by one in the previous operation) and the first to be carried out is the multiplication (3*5=15) and only then the u++.That is entirely against the documentation.

This is the script I wrote to test the issue:

public class MathTest {

public static void main (String [] args) {

float u=2, v=3, w=5, x=7, y=11;

System.out.println("Initial value for 'u': " + u);

float tmp1 = u++ /v;

System.out.println("u++ /v = " + tmp1);

System.out.println("First ++ value for 'u': " + u);

float tmp2 = u++ *w;

System.out.println("u++ *w= " + tmp2);

System.out.println("Second ++ value for 'u': " + u);

System.out.println(tmp1+tmp2);

}

}

The output:

Initial value for 'u': 2.0

u++ /v = 1.0

First ++ value for 'u': 3.0

u++ *w= 20.0

Second ++ value for 'u': 4.0

21.0

Clearly, the ++ operation is done after the division and after the multiplication.

Am I missing something here? Is the documentation wrong, or have I missed something obvious? This is driving me crazy!

Thanks in advance,

Mori

- 133 Views
- Tags: none (add)