Order of Precedence question

This is a discussion on Order of Precedence question within the C Programming forums, part of the General Programming Boards category; When i put this little equation through MinGW total = 100-25*50; The order of precedence states that it should do ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    7

    Question 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. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by CommonTater View Post
    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
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ssharish2005 View Post
    That still wouldn't solve the problem.

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

  6. #6
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    Quote Originally Posted by Jony View Post
    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
    Do not stare at my avatar.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    > 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
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    Quote Originally Posted by ssharish2005 View Post
    > 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;
    Do not stare at my avatar.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    7

    Lightbulb

    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. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by Jony View Post
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    7
    Quote Originally Posted by AndrewHunter View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. precedence question
    By nimitzhunter in forum C Programming
    Replies: 4
    Last Post: 08-10-2010, 11:57 PM
  2. Have a window take z-order precedence over - Games?
    By guitarist809 in forum Windows Programming
    Replies: 2
    Last Post: 07-19-2008, 08:11 PM
  3. order of precedence
    By the bassinvader in forum C Programming
    Replies: 6
    Last Post: 12-15-2006, 04:11 PM
  4. Replies: 15
    Last Post: 11-04-2006, 03:06 AM
  5. question about precedence (i think)
    By rootnix in forum C++ Programming
    Replies: 4
    Last Post: 09-25-2001, 11:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21