python 中的负 pow

发布于 2024-10-01 07:42:26 字数 234 浏览 5 评论 0原文

我有这个问题

>>> import math
>>> math.pow(-1.07,1.3)  
Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
ValueError: math domain error

有什么建议吗?

I have this problem

>>> import math
>>> math.pow(-1.07,1.3)  
Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
ValueError: math domain error

any suggestion ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

墨小沫ゞ 2024-10-08 07:42:26

(-1.07)1.3 不是实数,因此会出现数学域错误。

如果您需要一个复数,则 ab 必须重写为 eb ln a,例如,

>>> import cmath
>>> cmath.exp(1.3 * cmath.log(-1.07))
(-0.6418264288034731-0.8833982926856789j)

如果您只想返回 NaN,请捕获该异常。

>>> import math
>>> def pow_with_nan(x, y):
...   try:
...     return math.pow(x, y)
...   except ValueError:
...     return float('nan')
...
>>> pow_with_nan(1.3, -1.07)   # 1.3 ** -1.07
0.755232399659047
>>> pow_with_nan(-1.07, 1.3)   # (-1.07) ** 1.3
nan

顺便说一句,在 Python 中,通常使用内置的 a ** b 来提高幂,而不是 math.pow(a, b)

>>> 1.3 ** -1.07
0.755232399659047
>>> (-1.07) ** 1.3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: negative number cannot be raised to a fractional power
>>> (-1.07+0j) ** 1.3
(-0.6418264288034731-0.8833982926856789j)

(-1.07)1.3 will not be a real number, thus the Math domain error.

If you need a complex number, ab must be rewritten into eb ln a, e.g.

>>> import cmath
>>> cmath.exp(1.3 * cmath.log(-1.07))
(-0.6418264288034731-0.8833982926856789j)

If you just want to return NaN, catch that exception.

>>> import math
>>> def pow_with_nan(x, y):
...   try:
...     return math.pow(x, y)
...   except ValueError:
...     return float('nan')
...
>>> pow_with_nan(1.3, -1.07)   # 1.3 ** -1.07
0.755232399659047
>>> pow_with_nan(-1.07, 1.3)   # (-1.07) ** 1.3
nan

BTW, in Python usually the built-in a ** b is used for raising power, not math.pow(a, b).

>>> 1.3 ** -1.07
0.755232399659047
>>> (-1.07) ** 1.3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: negative number cannot be raised to a fractional power
>>> (-1.07+0j) ** 1.3
(-0.6418264288034731-0.8833982926856789j)
玩物 2024-10-08 07:42:26

不要使用 pow,并将指数设为复数(添加 0j)。这是一个例子:

In [15]: (-1.07)**(1.3+0j)
Out[15]: (-0.64182642880347307-0.88339829268567893j)

不需要数学函数:)

Don't use pow, and make the exponent complex (add 0j to it). Here is an example:

In [15]: (-1.07)**(1.3+0j)
Out[15]: (-0.64182642880347307-0.88339829268567893j)

No need for math functions :)

妳是的陽光 2024-10-08 07:42:26

复数(和负数)的非整数幂涉及一个重要的微妙之处。指数函数在实线上单射;即 exp(a) = exp(b) 意味着 a = b。在复平面上情况并非如此。由于 exp(2*pi*i) = 1,因此指数函数是 2*pi*i 周期的。

这就引出了一个问题:我们使用日志函数的哪个分支?该问题是复杂分析的核心问题之一。

Python 正在智能地应对这种情况。除非您明确使用其复数构造函数,否则您将进行实数交易。由于负数的分数幂永远不是真实的,Python 会适当地抛出异常。

Noninteger powers of complex (and negative) numbers involve an important subtlety. The exponential function is injective on the real line; i.e. exp(a) = exp(b) implies a = b. This is NOT so on the complex plane. Since exp(2*pi*i) = 1, the exponential function is 2*pi*i-periodic.

This leads to the problem: Which branch of the log function do we use? Said question is one of the central questions of complex analysis.

Python is responding intelligently to this situation. Unless you explicitly use its complex number constructor, you are going to be trafficking in reals. Since fractional powers of negatives are NEVER real, Python is appropriately throwing an exception.

做个ˇ局外人 2024-10-08 07:42:26

我正在使用 python 2.5.4,我得到这个:

>>> import math
>>> math.pow(-1.07,1.3)  
nan

你使用什么 python 版本?

I am using python 2.5.4 and I get this:

>>> import math
>>> math.pow(-1.07,1.3)  
nan

What python version are you using?

深居我梦 2024-10-08 07:42:26

从这篇文章的标题来看,为负,您是否可能实际上想要 1.3-1.07 而不是 -1.071.3

From the title of this post indicating that the power is negative, is it possible that you actually wanted 1.3-1.07 rather than -1.071.3?

雨巷深深 2024-10-08 07:42:26

负基数的幂是复数。
这是一个解释如何修复它的示例:

from numpy import *
t  = arange(-3, 3, 0.1)

for n in range(0,len(t)):
    T = t[n]
    x = (complex(-0.5,0))**T
    print(T, x)

Powers of negative bases are complex numbers.
Here's an example that explains how to fix it:

from numpy import *
t  = arange(-3, 3, 0.1)

for n in range(0,len(t)):
    T = t[n]
    x = (complex(-0.5,0))**T
    print(T, x)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文