Python 是什么导致数学域错误?

Python 是什么导致数学域错误?,python,Python,这是我用来计算火箭模型高度的代码。在第49行,我收到一条数学域错误消息。以下是指向错误消息的链接: from math import * motor_type = raw_input("What is the motor letter") b4 = { "th" : 13.2, "i" : 5.00, "ti" : 0.8} b6 = { "th" : 12.1, "i" : 5.00, "ti" : 0.8} c = { "th" : 15.3, "i" : 10.00, "ti" : 1

这是我用来计算火箭模型高度的代码。在第49行,我收到一条数学域错误消息。以下是指向错误消息的链接:

from math import *

motor_type = raw_input("What is the motor letter")

b4 = { "th" : 13.2, "i" : 5.00, "ti" : 0.8}
b6 = { "th" : 12.1, "i" : 5.00, "ti" : 0.8}
c = { "th" : 15.3, "i" : 10.00, "ti" : 1.6}
d = { "th" : 32.9, "i" : 20.00, "ti" : 1.6}
e = { "th" : 25.0, "i" : 30.00, "ti" : 2.8} 

    #######################################################
a1 = b4["th"]
a2 = b4["i"]
a3 = b4["ti"]

b1 = b6["th"]
b2 = b6["i"]
b3 = b6["ti"]

c1 = c["th"]
c2 = c["i"]
c3 = c["ti"]

d1 = d["th"]
d2 = d["i"]
d3 = d["ti"]

e1 = e["th"]
e2 = e["i"]
e3 = e["ti"]

    #######################################################
def get_altitude(th, i, ti):
    Cd = float(.075)#drag coefficient = 0.75 for average rocket
    rho = float(1.22)#air density = 1.22 kg/m3
    g = float(9.81)#acceleration of gravity = 9.81 m/s2
    v = float(0.0)#burnout velocity in m/s
    y1 = float(0.0)#altitude at burnout
    yc = float(0.0)#coasting distance
    ta = float(0.0)#coasting time => delay time for motor
    m = float(raw_input("What is the mass in Kg of the rocket with the    motor loaded ")) #rocket mass in kg
    a = pi * (((float(int(raw_input("What is the diameter of the rocket   body in cm")))) / 200)**2) #rocket cross-sectional area in m2
    #######################################################
    k = rho*Cd*a*0.5
    q = (th - m * g) / k
    x = ((2*k*q)/ m)
    v = q * ((1 - exp(-x * ti))/(1 + exp(-x * ti)))
    yc = (m / (2*k)) * log((m*g+k * (v**2))/(m*g))
    y1 = (-m/(2*k))*log((th - m * g - k * (v**2)) / (th - (m * g)))
    qa = sqrt(m*g/k)
    qb = sqrt(g*k/m)
    ta = atan(v/qa) / qb
    altitude = y1 + yc 
    print "Time from burnout to apogee"
    print ta
    print "Max altitude"
    print altitude 

if motor_type == "b4":
    get_altitude(a1, a2, a3)
elif motor_type == "b6":
    get_altitude(b1, b2, b3)
elif motor_type == "c":
    get_altitude(c1, c2, c3)
elif motor_type == "d":
    get_altitude(d1, d2, d3)
elif motor_type == "e":
    get_altitude(e1, e2, e3)


在第49行,您正在执行
log
操作,但
log
仅针对大于0的数字定义。因此,您得到一个数学域错误。检查你的价值观

作为故意造成这种错误的一个随机例子

假设我为电机输入“b4”,然后分别为质量(以千克为单位)和直径(以厘米为单位)输入0.0005和1,
log
中表达式的值将为-3.6*10^6,并且未定义该值的对数


另外,我注意到一件很小的事情,就是在代码上加上修饰,对变量执行
float(0.0)
是不必要的。0.0已经是一个浮点数了。同样,对于其他花车。

谢谢。当你说“检查值”时,你的意思是要进行检查以确保值大于0吗?是的,否则会引发数学域错误,因为只为大于零的值定义了日志