我的程序中有以下代码:
theta=(180/math.pi)*0.5*math.asin((9.8*dist)/(vel**2))当我在import math之后使用上面的代码时,它会给出以下数学域错误:
Traceback (most recent call last):
File "traj.py", line 36, in <module>
processCase(caseNumber,V,D)
File "traj.py", line 20, in processCase
theta=(180/math.pi)*0.5*math.asin((9.8*dist)/(vel**2))
ValueError: math domain error所提供的资料如下:
vel= 119 dist= 1445是什么导致了这个错误。当我使用导入cMath时,错误就消失了,但是我得到了一个复数作为输出。为什么会这样呢?
发布于 2014-06-25 04:05:19
由于浮点误差将(9.8*dist)/(vel**2)舍入到1.0以上的值,因此asin函数会给出一个域错误。
您可以通过将math.asin调用中的数量限制在1.0以内来解决这个问题,而不考虑四舍五入。您可以使用Decimal算法以一种适当的“数学”方式来完成,但是对性能和复杂性有很大的影响。
我的建议是简单地在min调用中调用asin:
theta=(180/math.pi)*0.5*math.asin(min(1.0, ((9.8*dist)/(vel**2)) ) 发布于 2014-06-25 04:02:00
这是错误math.asin(),它给出了数学域错误。它作为math.asin(1.0000000000000002)
https://stackoverflow.com/questions/24399893
复制相似问题