ValueError:数学域错误 [英] ValueError: math domain error

查看:91
本文介绍了ValueError:数学域错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码

def partE():
    e = 3 * 10 // 3 + 10 % 3
    print("e).", e)

partE()

,当我尝试运行它时,python返回此错误消息.我不明白为什么.有人可以解释一下吗?非常感谢!

and python comes back with this error message when I try to run it. I do not understand why. Can someone please explain? Thank you so much!

Traceback (most recent call last):
  File "C:/Users/Crisa/PycharmProjects/untitled/homeworkchap3.py", line 30, in <module>
    partD()
  File "C:/Users/Crisa/PycharmProjects/untitled/homeworkchap3.py", line 27, in partD
    d = sqrt(4.5 - 5.0) + 7 * 3
ValueError: math domain error

推荐答案

您的回溯表明您要将负数传递给math.sqrt()函数:

Your traceback indicates you are passing a negative number to the math.sqrt() function:

>>> from math import sqrt
>>> sqrt(4.5 - 5.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> sqrt(-1.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

不要那样做.根据定义,数字的平方始终为正,因此要再次获得平方根,您必须传递正数.

Don't do that. By definition, the square of a number is always positive, so to get the square root again, you must pass in a positive number.

请注意,您发布的异常与您发布的代码无关.该代码可以正常工作:

Note that the exception you posted has nothing to do with the code you posted. That code works just fine:

>>> def partE():
...     e = 3 * 10 // 3 + 10 % 3
...     print("e).", e)
... 
>>> partE()
('e).', 11)

这篇关于ValueError:数学域错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆