计算角度时 python 中的数学域错误
math domain error in python when caluclating angles
尝试根据给定的边计算各种三角形的角度。
我有所有的算法可以这样做,一个小问题是我找不到 'math domain error'。
感谢任何帮助:)
from math import *
def angles(a, b, c):
if a + b <= c or a + c <= b or c + b <= a:
return [0, 0, 0]
elif a == b == c:
return [60,60,60]
tempLst = []
d = max(a,b,c) #To know which angle is the largest
if a == d:
if (b ** 2 + c ** 2) < d**2: #To know wether a triangle is an obtuse triangle
tempLst.append(round(degrees(acos((c**2 - a**2 - b**2) / (-2 * c * b)))))
tempLst.append(round(asin(a/sin(tempLst[0])*b)))
else:
tempLst.append(round(degrees(asin(c/a))))
tempLst.append(round(degrees(asin(b/a))))
elif b == d:
if (a**2+c**2) < d**2:
tempLst.append(round(degrees(acos((c**2 - a**2 - b**2)/-2*c*a))))
tempLst.append(round(asin(b/sin(tempLst[0])*a)))
else:
tempLst.append(round(degrees(asin(c/b))))
tempLst.append(round(degrees(asin(a/b))))
else:
if (b**2+a**2) < d**2:
tempLst.append(round(degrees(acos((c**2 - a**2 - b**2)/-2*a*b))))
tempLst.append(round(asin(c/sin(tempLst[0])*b)))
else:
tempLst.append(round(degrees(asin(a/c))))
tempLst.append(round(degrees(asin(b/c))))
tempLst.append(180 - tempLst[0] - tempLst[1])
return tempLst
错误:
ValueError:数学域错误
角度,25
当尝试输入这些值时:angles(11, 20, 30) ---> "Unhandled exception"
谢谢大家
你的触发器关闭了。使用 Law of Cosines
from math import *
def angles(a, b, c):
if a + b <= c or a + c <= b or c + b <= a:
return [0, 0, 0]
elif a == b == c:
return [60, 60, 60]
tempLst = [
round(degrees(acos((a ** 2 + b ** 2 - c ** 2) / (2 * a * b)))), # C
round(degrees(acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)))), # A
round(degrees(acos((c ** 2 + a ** 2 - b ** 2) / (2 * c * a)))) # B
]
return tempLst
print(angles(11, 20, 30))
# -> [149, 11, 20]
尝试根据给定的边计算各种三角形的角度。 我有所有的算法可以这样做,一个小问题是我找不到 'math domain error'。 感谢任何帮助:)
from math import *
def angles(a, b, c):
if a + b <= c or a + c <= b or c + b <= a:
return [0, 0, 0]
elif a == b == c:
return [60,60,60]
tempLst = []
d = max(a,b,c) #To know which angle is the largest
if a == d:
if (b ** 2 + c ** 2) < d**2: #To know wether a triangle is an obtuse triangle
tempLst.append(round(degrees(acos((c**2 - a**2 - b**2) / (-2 * c * b)))))
tempLst.append(round(asin(a/sin(tempLst[0])*b)))
else:
tempLst.append(round(degrees(asin(c/a))))
tempLst.append(round(degrees(asin(b/a))))
elif b == d:
if (a**2+c**2) < d**2:
tempLst.append(round(degrees(acos((c**2 - a**2 - b**2)/-2*c*a))))
tempLst.append(round(asin(b/sin(tempLst[0])*a)))
else:
tempLst.append(round(degrees(asin(c/b))))
tempLst.append(round(degrees(asin(a/b))))
else:
if (b**2+a**2) < d**2:
tempLst.append(round(degrees(acos((c**2 - a**2 - b**2)/-2*a*b))))
tempLst.append(round(asin(c/sin(tempLst[0])*b)))
else:
tempLst.append(round(degrees(asin(a/c))))
tempLst.append(round(degrees(asin(b/c))))
tempLst.append(180 - tempLst[0] - tempLst[1])
return tempLst
错误: ValueError:数学域错误
角度,25
当尝试输入这些值时:angles(11, 20, 30) ---> "Unhandled exception"
谢谢大家
你的触发器关闭了。使用 Law of Cosines
from math import *
def angles(a, b, c):
if a + b <= c or a + c <= b or c + b <= a:
return [0, 0, 0]
elif a == b == c:
return [60, 60, 60]
tempLst = [
round(degrees(acos((a ** 2 + b ** 2 - c ** 2) / (2 * a * b)))), # C
round(degrees(acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)))), # A
round(degrees(acos((c ** 2 + a ** 2 - b ** 2) / (2 * c * a)))) # B
]
return tempLst
print(angles(11, 20, 30))
# -> [149, 11, 20]