Lua 中括号的位置(运算顺序)
Placement of parentheses in Lua (Order of Operations)
我正在尝试找出下面公式中的运算顺序。
我正在使用此脚本计算并显示 1000 欧元在 5% 的利率下三年后增长了多少钱的结果。
p 是银行年利率百分比。
A是初始金额。
n是年数。
p = 5
A = 1000
n = 3
根据数学,这是最正确的做法吗?据我了解,首先它除以 p / 100,然后加 1,然后对 ^ n 求幂,最后将所有内容与 A * 相乘。
my_version = (A * ((1 + (p / 100)) ^ n))
print(my_version)
教师版:
teachers_version = A * ((1 + p / 100) ^ n)
print(teachers_version)
其他版本(我如何确定它不只是将 p 加 1 然后除以 100?好吧,我知道不是,因为我试过了,但在未来或与其他数学题,我只是想了解计算机是如何解决这个问题的):
other_version = A * (1 + p / 100) ^ n
print(other_version)
或者计算机只知道操作顺序?
每个脚本的输出都是一样的:
1157.625
确定的最佳方法是将 Lua 的 order of operator precedence 添加为书签,并在必要时进行检查。
Lua5.4参考手册3.4.8
章列出了precedence运算符
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.
左结合意味着A+B+C
被评估为(A+B)+C
,所以从左到右。
^
是右结合的,所以在 (A+B)^(C+D)
中 Lua 将首先评估 C+D
。
运算符的结合性可帮助您找出以何种顺序计算具有相同优先级的多个运算符的表达式。它们在优先级中位于同一行 table.
见https://en.wikipedia.org/wiki/Order_of_operations
Lua 表达式如 teachers_version = A * ((1 + p / 100) ^ n)
总是按照这些规则进行计算。括号将更改该顺序,因为它们将从内到外进行评估。
所以我们可以将其拆分为
local x = 1 + p / 100
local y = x^n
teachers_version = A * y
/
比 +
具有更高的优先级,因此我们将 p
除以 100
,然后再将其添加到“1”。
您的版本:
version = (A * ((1 + (p / 100)) ^ n))
外括号是多余的。它们没有任何作用,因此您可以将它们放在一边。
version = A * (1 + (p / 100)) ^ n)
p/100
两边的括号也是多余的。 /
比 +
具有更高的优先级,因此您不需要它们告诉计算机先执行此操作。
version = A * (1 + p / 100) ^ n
这就剩下第三个版本了。你需要那组括号来告诉计算机它应该用 n
对 1 + p / 100
求幂,否则它会计算 100^n
因为 ^
高于 [=21] =].
教师版多了一对括号。这里不需要外括号。
所以你用所有这些括号明确地告诉计算机运算的优先级。但它没有必要,因为它已经知道了。如果要更改默认优先级,请使用括号。就像你在数学中用笔和纸做的一样。
我正在尝试找出下面公式中的运算顺序。
我正在使用此脚本计算并显示 1000 欧元在 5% 的利率下三年后增长了多少钱的结果。
p 是银行年利率百分比。 A是初始金额。 n是年数。
p = 5
A = 1000
n = 3
根据数学,这是最正确的做法吗?据我了解,首先它除以 p / 100,然后加 1,然后对 ^ n 求幂,最后将所有内容与 A * 相乘。
my_version = (A * ((1 + (p / 100)) ^ n))
print(my_version)
教师版:
teachers_version = A * ((1 + p / 100) ^ n)
print(teachers_version)
其他版本(我如何确定它不只是将 p 加 1 然后除以 100?好吧,我知道不是,因为我试过了,但在未来或与其他数学题,我只是想了解计算机是如何解决这个问题的):
other_version = A * (1 + p / 100) ^ n
print(other_version)
或者计算机只知道操作顺序?
每个脚本的输出都是一样的: 1157.625
确定的最佳方法是将 Lua 的 order of operator precedence 添加为书签,并在必要时进行检查。
Lua5.4参考手册3.4.8
章列出了precedence运算符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.
左结合意味着A+B+C
被评估为(A+B)+C
,所以从左到右。
^
是右结合的,所以在 (A+B)^(C+D)
中 Lua 将首先评估 C+D
。
运算符的结合性可帮助您找出以何种顺序计算具有相同优先级的多个运算符的表达式。它们在优先级中位于同一行 table.
见https://en.wikipedia.org/wiki/Order_of_operations
Lua 表达式如 teachers_version = A * ((1 + p / 100) ^ n)
总是按照这些规则进行计算。括号将更改该顺序,因为它们将从内到外进行评估。
所以我们可以将其拆分为
local x = 1 + p / 100
local y = x^n
teachers_version = A * y
/
比 +
具有更高的优先级,因此我们将 p
除以 100
,然后再将其添加到“1”。
您的版本:
version = (A * ((1 + (p / 100)) ^ n))
外括号是多余的。它们没有任何作用,因此您可以将它们放在一边。
version = A * (1 + (p / 100)) ^ n)
p/100
两边的括号也是多余的。 /
比 +
具有更高的优先级,因此您不需要它们告诉计算机先执行此操作。
version = A * (1 + p / 100) ^ n
这就剩下第三个版本了。你需要那组括号来告诉计算机它应该用 n
对 1 + p / 100
求幂,否则它会计算 100^n
因为 ^
高于 [=21] =].
教师版多了一对括号。这里不需要外括号。
所以你用所有这些括号明确地告诉计算机运算的优先级。但它没有必要,因为它已经知道了。如果要更改默认优先级,请使用括号。就像你在数学中用笔和纸做的一样。