Search code examples
lua

Placement of parentheses in Lua (Order of Operations)


I'm trying to figure out the order of operations in the formulas down below.

I'm using this script to compute and display the result of how much money 1000 euros have grown after three years with a 5 percent interest rate.

p is the bank interest rate in percent per year. A is the initial amount. n is the number of years.

p = 5
A = 1000
n = 3

Is this the most correct way to do it according to math? As I understand it, first it divides p / 100, then adds 1, then it exponentiates ^ n and finally multiplies everything with A *.

my_version = (A * ((1 + (p / 100)) ^ n))
print(my_version)

Teacher's version:

teachers_version = A * ((1 + p / 100) ^ n)
print(teachers_version)

Other version (How can I be sure that it doesn't just add 1 to p and then divide it with 100? Well I know it doesn't because I've tried it, but in the future or with other math problems, I just want to understand how the computer solves this problem):

other_version = A * (1 + p / 100) ^ n
print(other_version)

Or does the computer just know the order of operations?

Output of every script is the same: 1157.625


Solution

  • The Lua 5.4 Reference Manual lists the precedence of operators in chapter 3.4.8

    Operator precedence in Lua follows the table below, from lower to higher priority:

     or
     and
     <     >     <=    >=    ~=    ==
     |
     ~
     &
     <<    >>
     ..
     +     -
     *     /     //    %
     unary operators (not   #     -     ~)
     ^
    

    As usual, you can use parentheses to change the precedences of an expression. The concatenation ('..') and exponentiation ('^') operators are right associative. All other binary operators are left associative.

    Left associative means that A+B+C is evaluated as (A+B)+C, so from left to right. ^ is right associative so in (A+B)^(C+D) Lua will first evaluate C+D. The associativeness of an operator helps you to find out in which order to evaluate expressions with multiple operators of the same precedence. They are in the same line in the precedence table.

    See https://en.wikipedia.org/wiki/Order_of_operations

    Lua expressions like teachers_version = A * ((1 + p / 100) ^ n) are always evaluated following those rules. Parentheses will change that order as they will be evaluated from the inside out.

    So we can split this into

    local x = 1 + p / 100
    local y = x^n
    teachers_version = A * y
    

    / has higher precedence than + so we divide p by 100 befor we add it to `1'.

    Your version:

    version = (A * ((1 + (p / 100)) ^ n))
    

    The outer parenthesis are superfluous. They have no effect so you may leave them away.

    version = A * (1 + (p / 100)) ^ n)
    

    The parenthesis around p/100 are superfluous as well. / has higher precedence than + so you don't need them to tell the computer to do this first.

    version = A * (1 + p / 100) ^ n
    

    This leaves you with your third version. You need that set of parenthesis to tell the computer that it should exponentiate 1 + p / 100 with n as it would otherwise evaluate 100^n due to the higher precedence of ^ over /.

    The teachers version has a superfluous pair of parenthesis. The outer parenthesis are not necessary here.

    So you explicitly told the computer the precedence of operations with all those parentheses. But it was not necessary as it already knows it. Use parenthesis if you want to change the default precedence. The same way as you would do it in maths using pen and paper.