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

问题描述 投票:-1回答:1
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)

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

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

数学域错误是什么意思?

python math sin cos
1个回答
2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.