python数学域错误-sqrt

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29375970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:29:53  来源:igfitidea点击:

python math domain error - sqrt

pythonmathdnssqrt

提问by szzso24

What causes the problem?

导致问题的原因是什么?

from math import sqrt
print "a : "
a = float(raw_input())
print "b : "
b = float(raw_input())
print "c : "
c = float(raw_input())
d = (a + b + c)/2
s = sqrt(d*(d-a)*(d-b)*(d-c))
print "a+b+c =", a, b, c
print "Distr. =", d*2, "Area =", s

Error:

错误:

Traceback (most recent call last):
   File "C:/Python27/fájlok/háromsz?g terület2.py", line 11, in <module>
       s = sqrt(d*(d-a)*(d-b)*(d-c))
ValueError: math domain error

采纳答案by Bhargav Rao

The problem is that the Heron's formulaholds good only when the sum of the two numbers are greater than the third. You need to check that explicitly.

问题是只有当两个数字之和大于第三个时,Heron 公式才成立。您需要明确检查。

A better way as you are using a code to do that is by using Exception handling

使用代码来做到这一点的更好方法是使用异常处理

try:
    s = sqrt(d*(d-a)*(d-b)*(d-c))
    print "a+b+c =", a, b, c
    print "Distr. =", d*2, "Area =", s
except ValueError:
    print "Please enter 3 valid sides"

If you want to do it without tryblock you can do it as

如果你想在没有try阻塞的情况下做到这一点,你可以这样做

delta = (d*(d-a)*(d-b)*(d-c))
if delta>0:
    s = sqrt(delta)
    print "a+b+c =", a, b, c
    print "Distr. =", d*2, "Area =", s
else:
    print "Please enter 3 valid sides"

回答by aneroid

sqrtgives that error when you try to use it with a negative number. sqrt(-4)gives that error because the result is a complex number.

sqrt当您尝试将其与负数一起使用时会出现该错误。sqrt(-4)给出该错误,因为结果是一个复数

For that, you need cmath:

为此,您需要cmath

>>> from cmath import sqrt
>>> sqrt(-4)
2j
>>> sqrt(4)
(2+0j)

回答by SilverInternet

I got the same error with my code until I used cmathinstead of mathlike aneroid said:

我的代码遇到了同样的错误,直到我使用cmath而不是math像 aneroid 说的那样:

import sys
import random
import cmath

x = random.randint(1, 100)
y = random.randint(1, 100)

a = 2 * x * cmath.sqrt(1 - x * 2 - y * 2)
b = 2 * cmath.sqrt(1 - x * 2 - y * 2)
c = 1 - 2 * (x * 2 + y * 2)

print ( 'The point on the sphere is: ', (a, b, c) )

This way ran my code properly.

这样可以正确运行我的代码。

回答by Subham Debnath

Use cmath instead..

使用 cmath 代替..

import cmath
num=cmath.sqrt(your_number)
print(num)

Now regardless of whether the number is negetive or positive you will get a result...

现在无论数字是负数还是正数,您都会得到结果......

回答by zch

I think the problem is that you cannot define a triangle with whatever sides the users inputs. Try to make a triangle with length of sides, 10,2 and 1. Impossible. So, sometimes the Heron's Formula cannot work, because there is no triangle.

我认为问题是你不能用用户输入的任何边定义一个三角形。尝试制作一个边长为 10,2 和 1 的三角形。不可能。所以,有时苍鹭公式行不通,因为没有三角形。