# Thread: Order of Precedence question

1. ## Order of Precedence question

When i put this little equation through MinGW

total = 100-25*50;

The order of precedence states that it should do 25*50(1250) first and then -100
the result of that being 1150...

However... when i put this through MinGW it returns -1150 which implies it's doing the subtraction first and then multiplying it by 50 (on my calculator i managed to get the same result by doing that)

So, what's going on here? :S

2. Try this

total = 25 * 50 - 100;

Do you still dont get why the negative value is? then perhaps you should look into basics of maths from your school book.

ssharish

3. This is why the C gods gave us brackets. Try... total = (25 * 50) - 100; ... it will do the part in brackets first.
It is always better to be more specific... a lot fewer surprises that way.

4. Originally Posted by CommonTater
This is why the C gods gave us brackets ... try total = 100 - (25 * 50); ... it will do the part in brackets first.
That still wouldn't solve the problem.

ssharish

5. Originally Posted by ssharish2005
That still wouldn't solve the problem.

ssharish
I know... look again, I fixed it.

6. Originally Posted by Jony
When i put this little equation through MinGW

total = 100-25*50;

The order of precedence states that it should do 25*50(1250) first and then -100
the result of that being 1150...

However... when i put this through MinGW it returns -1150 which implies it's doing the subtraction first and then multiplying it by 50 (on my calculator i managed to get the same result by doing that)

So, what's going on here? :S
total = 100 - 25 * 50

and the answer will be -1150. what's wrong with that?
Code:
```total = 100 - 25 * 50

total = 100 - 1250

total = -1150```

7. > what's wrong with that?
Look back and read the OP question again. lol.
His question was why it is negative. And he expecting a positive number.

ssharish

8. Originally Posted by ssharish2005
> what's wrong with that?
Look back and read the OP question again. lol.
His question was why it is negative. And he expecting a positive number.

ssharish
ah...okay

dear OP,
if you want to get a positive result (instead of negative), then you should do this (just like what ssharish2005 did)
Code:
`int total = 25 * 50 - 100;`

9. Ahhh... i see...

It takes 100 first and then subtracts it by the result of (25*50) resulting in -1150...
*facepalm* xD
I haven't been sleeping much so sorry for the dumb mistake... :P

But thanks for the help

10. Originally Posted by Jony
When i put this little equation through MinGW

total = 100-25*50;

The order of precedence states that it should do 25*50(1250) first and then -100
the result of that being 1150...

However... when i put this through MinGW it returns -1150 which implies it's doing the subtraction first and then multiplying it by 50 (on my calculator i managed to get the same result by doing that)

So, what's going on here? :S
Hmm....maybe you would find this helpful.

11. Originally Posted by AndrewHunter
Hmm....maybe you would find this helpful.
I already knew about the order itself " P E M D A S " my confusion stemmed from the positioning of the 100...

Since Multiplication comes before Subtraction in the Order of Precedence, i foolishly thought that it would disregard the 100, do
the multiplication and only THEN subtract 100 from the result of the multiplication, which would've resulted in a positive number,
but, of course, that's not how it works, it still reads 100 first and subtracts the result of the division from 100 resulting a negative number :P