Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Matlab] Order of precedence in computations

Tags:
  1. Jul 30, 2014 #1

    Maylis

    User Avatar
    Gold Member

    Hello, here is a list of the order of precedence for computations when programming, at least in MATLAB

    1. Parentheses (starting with innermost pair)
    2. NOT (~) (left to right)
    3. Arithmetic operations (left to right)
    4. Relational operations (left to right)
    5. AND (&)
    6. OR (|)
    7. Short-circuit AND (&&)
    8. Short-circuit OR (||)

    I was wondering if there is any rhyme or reason for this order, or is it just something that I need to have committed to memory?
     
  2. jcsd
  3. Jul 30, 2014 #2

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    Double check those rules, correct them, and then memorize them. They are fairly standard in programming and in mathematics. Be sure to check the rules you posted. Line 3 is wrong. It would make 2 + 3 * 5 = (2 + 3) *5 = 25. That is wrong. Multiplication precedes addition so 2 + 3 * 5 = 2 + 15 = 17.

    Also, use parentheses liberally to force the calculation that you want.
     
  4. Jul 30, 2014 #3

    Maylis

    User Avatar
    Gold Member

    Great. I forgot an old trick for memorizing things, use mnemonics! PNARAOSS!!
     
  5. Jul 31, 2014 #4

    kreil

    User Avatar
    Gold Member

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Matlab] Order of precedence in computations
Loading...