This 博客文章 https://codeplea.com/exponentiation-associativity-options by 刘易斯·范·温克尔 https://github.com/codeplea(参考here https://en.wikipedia.org/wiki/Order_of_operations#Serial_exponentiation以及)给出了不同应用程序/语言如何处理左关联性与右关联性问题的有用细分;其中,对电力塔进行右关联处理是正确的方法。他的表是:
What |
Code Tested |
Result |
Associativity |
Bash |
2**2**3 |
256 |
right-associative |
C++ |
a^b^c |
64 |
left-associative |
DuckDuckGo |
2^2^3 |
256 |
right-associative |
Excel |
2^2^3 |
64 |
left-associative |
EtherCalc |
2^2^3 |
64 |
left-associative |
Fortran |
2**2**3 |
256 |
right-associative |
Google |
2^2^3 |
256 |
right-associative |
Google Sheets |
2^2^3 |
256 |
right-associative |
Hand-held Calculators |
|
|
Varies |
Lua |
2^2^3 |
256 |
right-associative |
Matlab |
2^2^3 |
64 |
left-associative |
Octave |
2^2^3 |
64 |
left-associative |
Perl |
2**2**3 |
256 |
right-associative |
PostgreSQL |
2^2^3 |
64 |
left-associative |
Python |
2**2**3 |
256 |
right-associative |
Ruby |
2**2**3 |
256 |
right-associative |
Tcl |
2**2**3 |
256 |
right-associative |
WolframAlpha |
2^2^3 |
256 |
right-associative |
我们可以添加r /questions/tagged/r在右联想阵营。
显然,右关联是此示例的主要方法,它排除了没有类似幂运算符的其他应用程序和语言(C#、SQL Server、Powershell 等)。我大胆猜测,由于向后兼容性问题,这个问题在 Excel(和其他)中并未“修复”。
这可能与这个问题无关(而且这超出了我的理解范围),但是 Math Exchange 上有一个人说左结合是解决问题的方法序数四联 https://math.stackexchange.com/q/2313576/341419(见评论)。
无论如何,我的答案是在 VBA 中使用用户定义的函数,例如从一个范围:
Function POWTOWR(rng As Range) As Double
On Error GoTo ErrHandler
Dim var As Variant
Dim dbl As Double
Dim lng As Long
var = Application.Transpose(Application.Transpose(rng.Value))
dbl = 1
For lng = UBound(var) To LBound(var) Step -1
dbl = var(lng) ^ dbl
Next lng
POWTOWR = dbl
Exit Function
ErrHandler:
POWTOWR = CVErr(xlErrNum)
End Function
并从数组中:
Function POWTOWA(ParamArray vals() As Variant) As Double
On Error GoTo ErrHandler
Dim dbl As Double
Dim lng As Long
Debug.Print TypeName(vals)
dbl = 1
For lng = UBound(vals) To LBound(vals) Step -1
dbl = vals(lng) ^ dbl
Next lng
POWTOWA = dbl
Exit Function
ErrHandler:
POWTOWA = CVErr(xlErrNum)
End Function
Where POWTOWR = CVErr(xlErrNum)
处理结果大于 Double 类型的最大值,根据this https://superuser.com/questions/1041458/what-is-largest-value-number-that-i-can-store-in-an-excel-vba-variable is 1.79769313486231570x(10^308)
(我的括号:))
Results: