使用asin,acos和atan时为什么会出现数学域错误?

时间:2017-12-20 23:18:35

标签: python math sin cos

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)

当我把' oppDIVhyp',' adjDIVhyp'或' oppDIVhyp'在math.a [sin,cos或tan]中,我得到一个数学域错误:

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

数学域错误是什么意思?

1 个答案:

答案 0 :(得分:2)

当要求函数使用对计算没有意义的参数计算值时,会生成ValueError: math domain error。例如,如果要求输入负数的日志。在这种情况下,我敢打赌,用户输入的是直角三角形不可能的值。例如,斜边短于其他两边的斜边。