用sqrt函数计算一个数的平方根

如果给它一个负数作为参数会怎样?

输入

>>>from math import qsrt
>>>sqrt(-1)

返回

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

在其他平台会有显示nan

PS: nan是一个特殊值的简写,意思是“not a number”非数值。


为什么sqrt函数不能求负数的平方根?

由于是自学只能百度了

知道了sqrt函数只能处理浮点数。

于是这里要使用cmath(complex math)的模块来处理。

>>>import cmath
>>>cmath.sqrt(-1)
1j

1j是个虚数.虚数都是以j结尾的,长整数是以L结尾。

Python中没有单独的虚数类型 它们被看作实数部分为0的复数。

讲真数学不好学习这种什么数数数的 真的是头疼