Guest User

Untitled

a guest
Mar 10th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. def float_mod(x,a):
  2.     k = int(x/a)
  3.     if (x*a) < 0:
  4.         k -= 1
  5.     return x - float(k)*a
  6.  
  7.  
  8. def ratio_based_sinus(x):
  9.     epsilon = 1.0e-16
  10.     last = x
  11.     retval = x
  12.  
  13.     n = 1
  14.     while 1:
  15.         n += 2
  16.         last *= (-x*x) / ( n * (n - 1) )
  17.         retval += last
  18.  
  19.         if abs(last) < epsilon:
  20.             break
  21.  
  22.     return retval
  23.  
  24.  
  25. def final_sin(x):
  26.     two_pi = 6.2831853
  27.     one_pi = 3.1415926
  28.     x = float_mod( x, two_pi )  # wrap it around, reduce range to (0,2*pi)
  29.  
  30.     if x > one_pi:      # reduce range to (0,pi)
  31.         return -ratio_based_sinus(x - one_pi)
  32.     else:
  33.         return ratio_based_sinus(x)
  34.  
  35.  
  36.  
  37. # Test
  38. import math
  39.  
  40. theta = 1.50
  41. v1 = final_sin(theta)
  42. v2 = math.sin(theta)
  43.  
  44. print "our-sin:    ", v1
  45. print "math.sqrt():", v2
Advertisement
Add Comment
Please, Sign In to add comment