[Matlab] Order of precedence in computations

  • Context: MATLAB 
  • Thread starter Thread starter gfd43tg
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion centers around the order of precedence in computations within MATLAB programming. Participants explore the rules governing this order and express curiosity about the rationale behind it.

Discussion Character

  • Technical explanation, Conceptual clarification, Debate/contested

Main Points Raised

  • One participant lists the order of precedence for computations in MATLAB, including operations such as parentheses, NOT, arithmetic, relational, AND, and OR.
  • Another participant challenges the accuracy of the listed rules, specifically pointing out an error in the arithmetic operations order and emphasizing that multiplication precedes addition.
  • A third participant suggests using mnemonics as a memory aid for recalling the order of operations.
  • A fourth participant provides a link to the official MATLAB documentation for operator precedence.

Areas of Agreement / Disagreement

There is disagreement regarding the accuracy of the listed order of precedence, with at least one participant asserting that the rules need correction. The discussion remains unresolved as participants have differing views on the correctness of the initial list.

Contextual Notes

Some assumptions about the standard rules of precedence in programming and mathematics are present, but specific definitions and contexts may vary. The discussion does not resolve the mathematical steps or the implications of the proposed corrections.

gfd43tg
Gold Member
Messages
949
Reaction score
48
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
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.
 
Great. I forgot an old trick for memorizing things, use mnemonics! PNARAOSS!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
20K
  • · Replies 11 ·
Replies
11
Views
3K