python - 如何在 Python 中找到负数的 sqrt
问题描述
当我输入一个负数时,它会给我 NaN(不是数字)作为答案
import math
x = int(input("Enter a number"))
x = math.sqrt(x)
print(x)
解决方案
负数的平方根是复数,因此您需要cmath
( complex math) 模块:
>>> import cmath
>>> cmath.sqrt(-1)
1j
>>> cmath.sqrt(1 - 2j)
(1.272019649514069-0.7861513777574233j)
推荐阅读
- python-3.x - 'xlrd' 已安装,但出现错误:“缺少可选依赖项 'xlrd'...”
- javascript - 无法让我的输入值绑定在 Svelte 3 中正常工作
- python - __dict__ 用于未显示 @property 属性的对象
- html - 有什么方法可以将 grid-template-area 设置为空白?
- java - Spring Security - 尽管允许所有请求但未经授权
- ios - iOS 14 中的可变字体
- javascript - 如何在木偶师身上睡觉
- snapshot - 是否可以通过代码创建快照并使用 pine-script 保存它?
- c - Leetcode:C语言中的187个重复DNA序列
- css - OpenTok 视频 UI ReactJS