基本算术运算中的歧义 Python
Ambiguity in basic arithmetic operations Python
场景如下:
In [5]: (2.0 - 5.0**(0.5)) ** (1.0/3.0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-5-d064023f1ac5> in <module>()
----> 1 (2.0 - 5.0**(0.5)) ** (1.0/3.0)
ValueError: negative number cannot be raised to a fractional power
In [7]: -1.0 ** (1.0/3.0)
Out[7]: -1.0
以上操作是在 python 解释器上完成的。对于第一个表达式,它给出了值错误并表示 negative number can't have fractional power
!!所以,首先,为什么这个错误很明显,-ve 数字可以有立方根或五次根等。此外,如果是这种情况,它应该是一致的,而在第二种情况下,当 -1 被提升到分次幂 (1/3).
有人可以解释为什么会这样吗?
**
运算符具有特定的绑定行为;来自 power operator 文档:
The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right.
[...]
Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands): -1**2
results in -1
.
因此您的 second 示例执行为:
-(1.0 ** (1.0/3.0))
也就是说,-
一元运算符应用于 **
的结果,因为该运算符绑定得更紧密。结果你有正数的 1/3 次方,然后才变成负数。
在您的第一个示例中,表达式被解析为
(2.0 - (5.0**(0.5))) ** (1.0/3.0)
这里没有一元运算符,但是 **
幂运算符确实比二元 -
减法运算符具有更高的优先级。
然后解析为
(2.0 - 2.23606797749979) ** (1.0/3.0)
这是
(-0.2360679774997898) ** (1.0/3.0)
尝试将负数提高到分数也是如此。
Python 2 **
(和 pow()
函数)不支持在输入最多为 float
个对象时生成复数支持。首先将负浮点值转换为 complex()
数字:
>>> (-1.0) ** 0.5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: negative number cannot be raised to a fractional power
>>> (-1+0j) ** 0.5
(6.123233995736766e-17+1j)
>>> (2+0j - 5.0**(0.5)) ** (1.0/3.0)
(0.30901699437494756+0.535233134659635j)
这在 Python 3 中发生了变化,其中为负数的分数次方返回复数结果。
场景如下:
In [5]: (2.0 - 5.0**(0.5)) ** (1.0/3.0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-5-d064023f1ac5> in <module>()
----> 1 (2.0 - 5.0**(0.5)) ** (1.0/3.0)
ValueError: negative number cannot be raised to a fractional power
In [7]: -1.0 ** (1.0/3.0)
Out[7]: -1.0
以上操作是在 python 解释器上完成的。对于第一个表达式,它给出了值错误并表示 negative number can't have fractional power
!!所以,首先,为什么这个错误很明显,-ve 数字可以有立方根或五次根等。此外,如果是这种情况,它应该是一致的,而在第二种情况下,当 -1 被提升到分次幂 (1/3).
有人可以解释为什么会这样吗?
**
运算符具有特定的绑定行为;来自 power operator 文档:
The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right.
[...]
Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands):
-1**2
results in-1
.
因此您的 second 示例执行为:
-(1.0 ** (1.0/3.0))
也就是说,-
一元运算符应用于 **
的结果,因为该运算符绑定得更紧密。结果你有正数的 1/3 次方,然后才变成负数。
在您的第一个示例中,表达式被解析为
(2.0 - (5.0**(0.5))) ** (1.0/3.0)
这里没有一元运算符,但是 **
幂运算符确实比二元 -
减法运算符具有更高的优先级。
然后解析为
(2.0 - 2.23606797749979) ** (1.0/3.0)
这是
(-0.2360679774997898) ** (1.0/3.0)
尝试将负数提高到分数也是如此。
Python 2 **
(和 pow()
函数)不支持在输入最多为 float
个对象时生成复数支持。首先将负浮点值转换为 complex()
数字:
>>> (-1.0) ** 0.5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: negative number cannot be raised to a fractional power
>>> (-1+0j) ** 0.5
(6.123233995736766e-17+1j)
>>> (2+0j - 5.0**(0.5)) ** (1.0/3.0)
(0.30901699437494756+0.535233134659635j)
这在 Python 3 中发生了变化,其中为负数的分数次方返回复数结果。