views:

75

answers:

6

Hello!

I have this operation:

t = (x - (y * (z + w)) - w) / 2;

where:

x = 268; y = 4; z = 20; w = 30;

As far as I know the result will be 49, but I getting 19.

Where is my error? (In using this code on a .Net Compact Framework 2.0 SP2 WinForm app).

Thank you.

A: 
>>> x = 268
>>> y = 4
>>> z = 20
>>> w = 30
>>> (x - (y * (z + w)) - w) / 2
19

Python likes 19 too! I think the problem is with your expectations ;)

Christopher Monsanto
+1  A: 

You should get 19...

t = (x - (y * (z + w)) - w) / 2;
t = (268 - (4 * (20 + 30)) - 30) / 2;  
t = (268 - (4 * (50)) - 30) / 2;  
t = (268 - (200) - 30) / 2;  
t = (68 - 30) / 2;  
t = (38) / 2;  
t = 19;

I suspect your error was at this step:

t = (268 - (200) - 30) / 2;

If you did 200 - 30 in your head you would get 170. And then 268 - 170 = 98 and 98 / 2 = 49. Because you have -200 and -30, you need to combine those to -230 not -170.

Brian R. Bondy
A: 

t = (268 - (200) - 30) / 2
t = 38 / 2
t = 19

19 is correct.

Jammin
A: 

Check your math again - either you've made a mistake in a calculation or your understanding of operator precedence is incorrect.

ShZ
+5  A: 

You probably want

 t = (x -(y * (z+w) - w))/2

which is 49

mjv
Beat me to it, but I like that you removed the unnecessary parentheses.
Tony van der Peet
A: 

Try

t = (x - ((y * (z + w)) - w)) / 2;
Tony van der Peet