How can I take the square root of -1 using python?

前端 未结 5 471
天涯浪人
天涯浪人 2020-12-06 17:59

When I take the square root of -1 it gives me an error:

invalid value encountered in sqrt

How do I fix that?

///         


        
5条回答
  •  萌比男神i
    2020-12-06 18:15

    To avoid the invalid value warning/error, the argument to numpy's sqrt function must be complex:

    In [8]: import numpy as np
    
    In [9]: np.sqrt(-1+0j)
    Out[9]: 1j
    

    As @AshwiniChaudhary pointed out in a comment, you could also use the cmath standard library:

    In [10]: cmath.sqrt(-1)
    Out[10]: 1j
    

提交回复
热议问题