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 result is
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.
Meanwhile when I write the code as
x = -0.26185950714291484 y = 1/5 print("The x=", x) Q = x**y print(Q)
I get
The x= -0.26185950714291484 (0.6188299348758349+0.44960626529008196j)
I am literally shocked. I cannot understand what is going on. If I just put the numbers manually I can calculate the result. But if I do them computationally I get an error ???
Advertisement
Answer
Well they look the same by they are not the same, with the code:
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)
you have a variable ‘x’ of type <class 'numpy.float64'>
, whereas with:
x = -0.26185950714291484 y = 1/5 print("The x=", x) Q = x**y print(Q)
the variable ‘x’ is of type <class 'float'>
.
Then you applied the exponentiation with a value of different types, namely <type one of type ‘numpy.float64’> (i.e., ‘x’) with another of type <class ‘float’> (i.e., ‘y’). You want to avoid the problems is to convert ‘x’ to float was well:
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)
Output:
(0.6188299348758349+0.44960626529008196j)