数值计算中的意外结果 - (double_scalar 中遇到无效值)
Unexpected result in the numerical calculation - (invalid value encountered in double_scalar )
from numpy import log as ln
z = 3
k = 2
x = 1 - ln(1 + z) / ln(1 + k)
y = 1/5
print("The x=", x)
Q = x**y
print(Q)
结果是
The x= -0.26185950714291484
c:\Users\-\Desktop\... RuntimeWarning: invalid value
encountered in double_scalars
Q = x**y
nan
I am having troubling to understand the problem here. It's clear that I can print the x
, but I cannot take the power of it.
同时当我把代码写成
x = -0.26185950714291484
y = 1/5
print("The x=", x)
Q = x**y
print(Q)
我明白了
The x= -0.26185950714291484
(0.6188299348758349+0.44960626529008196j)
我真的很震惊。我无法理解发生了什么。如果我只是手动输入数字,我可以计算出结果。但是如果我计算它们,我会得到一个错误 ???
好吧,它们看起来一样,但它们不一样,代码为:
from numpy import log as ln
z = 3
k = 2
x = 1 - ln(1 + z) / ln(1 + k)
y = 1/5
print("The x=", x)
Q = x**y
print(Q)
您有一个类型为 <class 'numpy.float64'>
的变量 'x',而使用:
x = -0.26185950714291484
y = 1/5
print("The x=", x)
Q = x**y
print(Q)
变量'x'的类型是<class 'float'>
。
然后您使用不同类型的值应用求幂,即 (即 'x')和另一个类型 (即 'y')。您要避免的问题是将 'x' 转换为 float 很好:
from numpy import log as ln
z = 3
k = 2
x = 1 - ln(1 + z) / ln(1 + k)
y = 1/5
Q = float(x)**y
print(Q)
输出:
(0.6188299348758349+0.44960626529008196j)
from numpy import log as ln
z = 3
k = 2
x = 1 - ln(1 + z) / ln(1 + k)
y = 1/5
print("The x=", x)
Q = x**y
print(Q)
结果是
The x= -0.26185950714291484
c:\Users\-\Desktop\... RuntimeWarning: invalid value
encountered in double_scalars
Q = x**y
nan
I am having troubling to understand the problem here. It's clear that I can print the
x
, but I cannot take the power of it.
同时当我把代码写成
x = -0.26185950714291484
y = 1/5
print("The x=", x)
Q = x**y
print(Q)
我明白了
The x= -0.26185950714291484
(0.6188299348758349+0.44960626529008196j)
我真的很震惊。我无法理解发生了什么。如果我只是手动输入数字,我可以计算出结果。但是如果我计算它们,我会得到一个错误 ???
好吧,它们看起来一样,但它们不一样,代码为:
from numpy import log as ln
z = 3
k = 2
x = 1 - ln(1 + z) / ln(1 + k)
y = 1/5
print("The x=", x)
Q = x**y
print(Q)
您有一个类型为 <class 'numpy.float64'>
的变量 'x',而使用:
x = -0.26185950714291484
y = 1/5
print("The x=", x)
Q = x**y
print(Q)
变量'x'的类型是<class 'float'>
。
然后您使用不同类型的值应用求幂,即
from numpy import log as ln
z = 3
k = 2
x = 1 - ln(1 + z) / ln(1 + k)
y = 1/5
Q = float(x)**y
print(Q)
输出:
(0.6188299348758349+0.44960626529008196j)