Cosine(A/3)
Background
edit
The value can be approximated by the sequence For example equals
|
Implementation
edit
# python code
from decimal import *
getcontext().prec = 100
def cosQuarterAngle (cos4A) :
cos4A = Decimal(str(cos4A))
cos2A = ((cos4A+1)/2).sqrt()
return ((cos2A+1)/2).sqrt()
def cosAplusB (cosA,cosB) :
cosA,cosB = [ Decimal(str(v)) for v in (cosA,cosB) ]
sinA = (1 - cosA*cosA).sqrt()
sinB = (1 - cosB*cosB).sqrt()
v1 = cosA*cosB
v2 = sinA*sinB
return v1 - v2, v1 + v2
def cosAfrom_cos3A(cos3A) :
cos3A = Decimal(str(cos3A))
if 1 >= cos3A >= -1 : pass
else :
print ('cosAfrom_cos3A(cos3A) : cos3A not in valid range.')
return None
if cos3A == 0 :
# 3A = 90 , return cos30
# 3A = 90 + 360 = 450 , return cos150, 30+120
# 3A = 90 + 720 = 810 , return cos270, 30-120 = -90
cos30 = Decimal(3).sqrt()/2
return cos30, -cos30, Decimal(0)
cos60 = Decimal('0.5')
if cos3A == 1 :
# 3A = 0 , return cos0
# 3A = 360 , return cos120, 0+120
# 3A = 720 , return cos240, 0-120
return Decimal(1), -cos60, -cos60
if cos3A == -1 :
# 3A = 180 , return cos60
# 3A = 180 + 360 = 540 , return cos180, 60+120
# 3A = 180 + 720 = 900 , return cos300, 60-120 = -60
return cos60, Decimal(-1), cos60
cosA = cosQuarterAngle (cos3A)
next = cosQuarterAngle (cosA)
count = 0
while(next != 1):
cosA = cosAplusB (cosA,next) [0]
next = cosQuarterAngle (next)
count += 1
print ('count =',count)
cos120 = -cos60
return (cosA,) + cosAplusB (cosA,cos120)
Result is accurate to about half the precision. For example, if precision is set to result is accurate to approximately places of decimals, and is achieved with about 81 passes through loop. |