【问题标题】:How To Find The sqrt Of A Negative Number In Python如何在 Python 中找到负数的 sqrt
【发布时间】:2019-10-03 13:59:08
【问题描述】:

当我输入一个负数时,它会给我 NaN(不是数字)作为答案

import math
x = int(input("Enter a number"))
x = math.sqrt(x)
print(x)

【问题讨论】:

    标签: python math python-import


    【解决方案1】:

    负数的平方根是复数,因此您需要 cmathcomplex 数学)模块:

    >>> import cmath
    >>> cmath.sqrt(-1)
    1j
    >>> cmath.sqrt(1 - 2j)
    (1.272019649514069-0.7861513777574233j)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 2014-12-28
      相关资源
      最近更新 更多