我一直在尝试为这个函数编写代码,但我不能让asin
在python 2.7中工作。任何想法为什么?
import math
from decimal import *
def main():
H = raw_input ("Please enter hight:")
H = int(float(H))
m = raw_input ("Please enter crest thickness:")
m = int(float(m))
n = raw_input ("Please enter base thikness:")
n = int(float(n))
Fx = 0.0
Fy = 0.0
Magnitude = 0.0
Direction = 0.0
p = 1000 #dencity
g = 9.81 #gravity
R = math.sqrt(H*H + n*n)
#Force in x direction
Fx = (-p*g*m*(H*H))/2.0
#Force in y direction
Fy = -p*g*R*(((math.asin(n/H))/2.0)-sin((2*math.asin(n/H))/4.0))
#Overall force
Direction = math.atan(Fy/Fx)
Magnitude = sqrt(Fx*Fy + Fy*Fy)
print ("The force magnitude is", Magnitude)
print ("The force direction is", Direction)
答案 0 :(得分:1)
将整数除以整数(与n/H
中的math.asin(n/H)
一样)会在3.0之前的Python中返回一个整数(这是除法结果的最低值)。您必须将至少一个操作数转换为float
或在Python源文件的开头声明
from __future__ import division
答案 1 :(得分:0)
有一些问题:
sin
---> 23 Fy = -p*g*R*(((math.asin(n/H))/2.0)-sin((2*math.asin(n/H))/4.0))
NameError: name 'sin' is not defined
这来自---> 26 Direction = math.atan(Fy/Fx)
ZeroDivisionError: float division by zero
整数除以@MichaelButscher states。
内置n/H
,也使用math.sqrt
sqrt
如果输入为---> 30 Magnitude = sqrt(Fx*Fy + Fy*Fy)
NameError: name 'sqrt' is not defined
,则会将其截断为float
,这似乎没必要。
最终更正的代码:
int
import math
from decimal import *
from __future__ import division
H = 10.0 # raw_input("Please enter hight:")
H = float(H)
m = 0.2 # raw_input("Please enter crest thickness:")
m = float(m)
n = 2.0 # raw_input("Please enter base thikness:")
n = float(n)
Fx = 0.0
Fy = 0.0
Magnitude = 0.0
Direction = 0.0
p = 1000 #dencity
g = 9.81 #gravity
R = math.sqrt(H*H + n*n)
#Force in x direction
Fx = (-p*g*m*(H*H))/2.0
#Force in y direction
Fy = -p*g*R*(((math.asin(n/H))/2.0)-math.sin((2*math.asin(n/H))/4.0))
#Overall force
Direction = math.atan(Fy/Fx)
Magnitude = math.sqrt(Fx*Fy + Fy*Fy)
print ("The force magnitude is", Magnitude)
print ("The force direction is", Direction)