Python 为什么在使用asin、acos和atan时会出现数学域错误?
当我把'oppDIVhyp'、'adjDIVhyp'或'oppDIVhyp'放在math.a[sin、cos或tan]中时,我得到的回报是一个数学域错误:Python 为什么在使用asin、acos和atan时会出现数学域错误?,python,math,trigonometry,Python,Math,Trigonometry,当我把'oppDIVhyp'、'adjDIVhyp'或'oppDIVhyp'放在math.a[sin、cos或tan]中时,我得到的回报是一个数学域错误: import math def SOH(oppositeSOH, hypotenuseSOH): oppDIVhyp = oppositeSOH / hypotenuseSOH soh = math.asin(oppDIVhyp) print("The angle theta is equivelent to", so
import math
def SOH(oppositeSOH, hypotenuseSOH):
oppDIVhyp = oppositeSOH / hypotenuseSOH
soh = math.asin(oppDIVhyp)
print("The angle theta is equivelent to", soh)
def CAH(adjacentCAH, hypotenuseCAH):
adjDIVhyp = adjacentCAH / hypotenuseCAH
cah = math.acos(adjDIVhyp)
print("Angle theta is equivelent to", cah)
def TOA(oppositeTOA, adjacentTOA):
oppDIVhyp = oppositeTOA / adjacentTOA
toa = math.atan(oppDIVadj)
print("Angle theta is equivelent to", toa)
SOHCAHTOA = input("Do you want to calculate angle theta with a: soh, b: cah, c: toh ")
A = SOHCAHTOA.upper()
if A == 'A':
oppositeSOH = int(input("Enter the length of the opposite side "))
hypotenuseSOH = int(input("Enter the length of the hypotenuse "))
SOH(oppositeSOH, hypotenuseSOH)
if A == 'B':
adjacentCAH = int(input("Enter the length of the adjacent side "))
hypotenuseCAH = int(input("Enter the length of the hypotenuse "))
CAH(adjacentSOH, hypotenuseSOH)
if A == 'C':
oppositeCAH = int(input("Enter the length of the opposite side "))
adjacentCAH = int(input("Enter the length of the hypotenuse "))
TOA(oppositeSOH, adjacentSOH)
回溯(最近一次呼叫最后一次):
文件“C:\Users\Alex\Documents\sohcahtoa.py”,第23行,在
SOH(对立面SOH,斜边SOH)
文件“C:\Users\Alex\Documents\sohcahtoa.py”,第5行,在SOH中
soh=数学asin(oppDIVhyp)
ValueError:数学域错误
数学域错误是什么意思?当要求函数使用对计算没有意义的参数计算值时,会产生
ValueError:math domain error
。例如,如果您要求提供负数日志。在这种情况下,我敢打赌用户输入的值对于直角三角形来说是不可能的。例如,比另两条边中的一条短的斜边。当要求函数使用对计算没有意义的参数计算值时,会产生ValueError:math domain error
。例如,如果您要求提供负数日志。在这种情况下,我敢打赌用户输入的值对于直角三角形来说是不可能的。例如,比其他两条边中的一条短的斜边。当您/用户收到这些错误时,您/用户输入的值是什么?这可能只是因为您正在输入无法计算的值。一个比hyp或类似的词长的形容词。可能是因为你使用的变量没有给它赋值。(假设您选择了“B”。您设置了*CAH,但随后使用*SOH。)刚刚意识到并编辑了这一点,因为oppDIVhyp
可能不在[-1,1]…谢谢mypetlion,这就是问题所在M8当您收到这些错误时,您/用户输入了哪些值?这可能只是因为您正在输入无法计算的值。一个比hyp或类似的词长的形容词。可能是因为你使用的变量没有给它赋值。(假设您选择了“B”。您设置了*CAH,但随后使用*SOH。)刚刚意识到并编辑了这一点,因为oppDIVhyp
可能不在[-1,1]中……谢谢我的小朋友,这就是问题所在
Traceback (most recent call last):
File "C:\Users\Alex\Documents\sohcahtoa.py", line 23, in <module>
SOH(oppositeSOH, hypotenuseSOH)
File "C:\Users\Alex\Documents\sohcahtoa.py", line 5, in SOH
soh = math.asin(oppDIVhyp)
ValueError: math domain error