-9

I've been trying to write code for this function, but I can't get asin to work in python 2.7. any ideas why?

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)
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • What do you mean by `I can't get asin to work`? What makes you think that it is not working? – crayzeewulf Sep 26 '13 at 21:51
  • 7
    Dividing integer by integer returns integer in Python < 3.0. Read http://www.python.org/dev/peps/pep-0238/ – Michael Butscher Sep 26 '13 at 21:52
  • 1
    @MichaelButscher: You should probably write that up as an answer, because it's almost certainly the OP's only problem here. (That's assuming "not working" means something like "returning `0.0` for perfectly reasonable inputs", but it seems likely.) – abarnert Sep 26 '13 at 22:22

2 Answers2

1

Dividing integer by integer (as you do with n/H inside math.asin(n/H)) returns an integer (which is the floor value of the division result) in Python before 3.0. You must either convert at least one operand to float or declare at the beginning of the Python source file

from __future__ import division

Read http://python.org/dev/peps/pep-0238 for details

Michael Butscher
  • 10,028
  • 4
  • 24
  • 25
0

Code Corrections

There are a few problems:

  1. There is no built-in sin, it needs to be used from math as math.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
    
  2. There is zero division.

    ---> 26 Direction = math.atan(Fy/Fx)
    
    ZeroDivisionError: float division by zero
    

    This originates from n/H integer division as @MichaelButscher states.

  3. There is no sqrt built-in, also use math.sqrt

    ---> 30 Magnitude = sqrt(Fx*Fy + Fy*Fy)
    
    NameError: name 'sqrt' is not defined
    
  4. If the inputs are floats they are truncated to ints, this seem unnecessary.

Final corrected code:

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)

('The force magnitude is', 1291.77652735702)
('The force direction is', 0.00017336501979739458)
Community
  • 1
  • 1
tmthydvnprt
  • 10,398
  • 8
  • 52
  • 72