【问题标题】:Why am I getting math domain error when using asin, acos and atan?为什么在使用 asin、acos 和 atan 时出现数学域错误?
【发布时间】:2017-12-20 23:18:34
【问题描述】:
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

数学域错误是什么意思?

【问题讨论】:

  • 当您遇到这些错误时,您/用户输入了哪些值?可能只是您输入的值无法计算。比炒作或类似的东西更长的形容词。
  • 可能是因为您使用的变量尚未赋值。 (假设您选择“B”。您设置了 *CAH,然后使用 *SOH。)
  • 刚刚意识到并编辑了这一点
  • 因为oppDIVhyp 可能不在[-1,1]...
  • 感谢 mypetlion 是 m8 的问题

标签: python math trigonometry


【解决方案1】:

ValueError: math domain error 是在要求函数使用对计算没有意义的参数计算值时产生的。例如,如果您要求记录负数。在这种情况下,我敢打赌,用户输入的值对于直角三角形来说是不可能的。例如,比其他两侧之一短的斜边。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2016-08-08
    • 2021-01-05
    • 2021-06-26
    • 2020-12-20
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多