Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def float_mod(x,a):
- k = int(x/a)
- if (x*a) < 0:
- k -= 1
- return x - float(k)*a
- def ratio_based_sinus(x):
- epsilon = 1.0e-16
- last = x
- retval = x
- n = 1
- while 1:
- n += 2
- last *= (-x*x) / ( n * (n - 1) )
- retval += last
- if abs(last) < epsilon:
- break
- return retval
- def final_sin(x):
- two_pi = 6.2831853
- one_pi = 3.1415926
- x = float_mod( x, two_pi ) # wrap it around, reduce range to (0,2*pi)
- if x > one_pi: # reduce range to (0,pi)
- return -ratio_based_sinus(x - one_pi)
- else:
- return ratio_based_sinus(x)
- # Test
- import math
- theta = 1.50
- v1 = final_sin(theta)
- v2 = math.sin(theta)
- print "our-sin: ", v1
- print "math.sqrt():", v2
Advertisement
Add Comment
Please, Sign In to add comment