[Matlab] Order of precedence in computations

  • MATLAB
  • Thread starter gfd43tg
  • Start date
  • Tags
    Matlab
In summary, the order of precedence for computations in MATLAB is as follows: parentheses, NOT (~), arithmetic operations, relational operations, AND (&), OR (|), short-circuit AND (&&), and short-circuit OR (||). These rules are standard in programming and mathematics, and it is important to double check them and use parentheses to force the desired calculation. Mnemonics, such as PNARAOSS, can be helpful for memorizing the order of precedence.
  • #1
gfd43tg
Gold Member
950
50
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?
 
Physics news on Phys.org
  • #2
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.
 
  • #3
Great. I forgot an old trick for memorizing things, use mnemonics! PNARAOSS!
 
  • #5


I can say that there is indeed a rationale behind this order of precedence in computations. The purpose of this order is to ensure that mathematical expressions are evaluated in a consistent and predictable manner. By following this order, we can avoid ambiguity and potential errors in our calculations.

For example, if we did not have parentheses as the first priority, the expression (3+4)*5 could be interpreted as 3+(4*5) or (3+4)*5, resulting in two different outcomes. By giving parentheses the highest priority, we ensure that the innermost operations are evaluated first, leading to the correct result.

Similarly, the order of precedence for logical operations is designed to prioritize the evaluation of expressions in a way that makes sense and is consistent with mathematical principles. This allows us to write complex expressions without having to use excessive parentheses, making our code more readable and efficient.

In summary, the order of precedence in computations is not something you need to memorize, but rather something you should understand and apply in your coding. Knowing the rationale behind it can help you write more efficient and error-free code.
 

What is the order of precedence in computations in Matlab?

The order of precedence in computations in Matlab follows the standard rules of arithmetic in mathematics. This means that multiplication and division are performed before addition and subtraction. In addition, parentheses can be used to change the order of operations.

How does Matlab handle operators with the same level of precedence?

When operators have the same level of precedence, Matlab follows the left-to-right rule, meaning that the operations are performed in the order they appear in the expression. For example, in the expression 4*3/2, the multiplication is performed first, followed by the division.

Can the order of precedence be changed in Matlab?

The order of precedence in Matlab cannot be changed. However, the use of parentheses can be used to control the order of operations and change the overall outcome of a computation.

How does Matlab handle expressions with multiple sets of parentheses?

When there are multiple sets of parentheses in an expression, Matlab evaluates the innermost parentheses first and then works its way outwards. This means that the order of operations is applied within each set of parentheses first, before moving on to the next set.

Are there any exceptions to the order of precedence in Matlab?

There are no exceptions to the order of precedence in Matlab. However, it is important to note that functions and operators in Matlab have different levels of precedence, so it is important to pay attention to this when writing complex expressions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Calculus and Beyond Homework Help
Replies
8
Views
208
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Replies
5
Views
2K
  • Quantum Physics
Replies
3
Views
1K
  • Programming and Computer Science
Replies
29
Views
3K
Back
Top