asin函数在python中不起作用

时间:2013-09-26 21:48:55

标签: python

我一直在尝试为这个函数编写代码,但我不能让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)

2 个答案:

答案 0 :(得分:1)

将整数除以整数(与n/H中的math.asin(n/H)一样)会在3.0之前的Python中返回一个整数(这是除法结果的最低值)。您必须将至少一个操作数转换为float或在Python源文件的开头声明

from __future__ import division

阅读http://python.org/dev/peps/pep-0238了解详情

答案 1 :(得分:0)

代码更正

有一些问题:

  1. 没有内置<a>,需要在math中使用math.sin

    sin
  2. zero division

    ---> 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

  3. 内置n/H,也使用math.sqrt

    sqrt
  4. 如果输入为---> 30 Magnitude = sqrt(Fx*Fy + Fy*Fy) NameError: name 'sqrt' is not defined ,则会将其截断为float,这似乎没必要。

  5. 最终更正的代码:

    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)
    
相关问题