Unexpected result in the numerical calculation - (invalid value encountered)

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 1K views
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
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)

The result is

Code:
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 a code like

Code:
 x = -0.26185950714291484
    y = 1/5
    print("The x=", x)
    Q = x**y

    print(Q)

I get

Code:
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 ?
 
Physics news on Phys.org
When you set x to a numpy result it uses numpy's overload of the ** operator which can't handle fractional powers of negative numbers. You need to convert x to a Python float: Q = float(x) ** y
 
  • Like
  • Informative
Likes   Reactions: sysprog, Arman777 and FactChecker