2017-08-03 59 views
-1

這是我的代碼來計算兩邊和一個角度時,三角形的第三邊。角度的輸入應該是度,但自math.cos()以弧度爲單位的值,所以我們需要將其轉換。蟒蛇數學域誤差計算三角形的第三角度

import math 
a = float(input("enter value for side 1:")) 
b = float(input("enter value for side 2:")) 
c = float(input("enter angle value in degrees:")) 
e = math.radians(c) 
g = (a * a) + (b * b) 
d = math.sqrt(g - (2 * a * c * (math.cos(e)))) 
print("third side value", d) 

我收到以下錯誤:

Traceback (most recent call last): 
    File "C:/Users/User/PycharmProjects/untitled/question3.py", line 7, in <module> 
    d = math.sqrt(g - (2 * a * c * (math.cos(e)))) 
ValueError: math domain error 

任何整改?

回答

0

您正在使用錯誤的公式。餘弦規則
side_a^2 + side_b^2 - 2 * side_a * side_b * cos(angle)你有
side_a^2 + side_b^2 - 2 * side_a * angle_in_degrees * cos(angle_in_radians)

您正在收到錯誤,因爲g - (2 * a * c * (math.cos(e)))可能是(也是)負數。 math.sqrt只接受正面的論據。