Vous êtes sur la page 1sur 1

def sinToCos(sinTheta):

'''
sinTheta: sin(theta)
Returns cos(theta) given sin(theta)
(Assumes 0 <= theta <= 90)
'''
return (1 - sinTheta**2)**.5
def findSin(sinA, sinB):
'''
sinA: sin(A)
sinB: sing(B)
Returns value of sin((A+B)/2)
given the value of sin(A) and sin(B)
'''
cosA, cosB = sinToCos(sinA), sinToCos(sinB)
return ((((1-cosA)/2)**.5) * (((1+cosB)/2)**.5)) \
+ ((((1+cosA)/2)**.5) * (((1-cosB)/2)**.5))
sinDict = {0.0: 0.0, 30.0: 0.5, 90.0:1.0}
accuracy = 0.001
def sin(theta):
'''
Returns the desired value of sin(theta)
within some accuracy: accuracy
'''
try:
return sinDict[theta] # See's if theta is in sinDict
except KeyError:
if theta - 270 >= 0:
return -sinToCos(sin(theta - 270))
elif theta - 180 >= 0:
return -sin(theta - 180)
elif theta - 90 >= 0:
return sinToCos(sin(theta - 90))
else:
startAngle = 0 if theta < 30 else 30 # theta could be > 0 or > 30
stopAngle = 30 if theta < 30 else 90 # theta could be < 30 or < 90
curAngle = (startAngle + stopAngle)/2
curAngle = round(curAngle, 5)
sinDict[curAngle] = findSin(sinDict[startAngle], sinDict[stopAngle])
while True:
if abs(curAngle-theta) <= accuracy:
return sinDict[curAngle] # Returns sin(curAngle) if curAngle
is close enought to theta
else: # Tries to find another curAngle which is close enought to
theta
if theta > curAngle:
startAngle = curAngle
else:
stopAngle = curAngle
curAngle = (startAngle + stopAngle)/2
curAngle = round(curAngle, 5)
sinDict[curAngle] = findSin(sinDict[startAngle], sinDict[sto
pAngle])

Vous aimerez peut-être aussi