4개의 풀이가 있습니다.
import math
def calculate_trigonometric_function(x: float) -> float:
"""This function refers to Taylor's theorm.
A remainder for cos or sin at x follows Cauchy's remainder.
The function has 2 steps. Step1 gets a degree of
Taylor polynomial function at x. Step 2 calculates sin and cos at x
and returns two values
"""
# Step1: get a degree of Taylor polynomial function at x
degree = 0
error = 5 * 1e-6
while True:
# Upper limit of Cauchy's remainder for sin or cos at x
upper_limit = x**degree / math.factorial(degree)
if upper_limit < error:
break
else:
degree += 1
# Step2: Calculate sin, cos at x
sin = 0
cos = 0
for i in range(degree):
sin += (-1)**i * x**((2 * i) + 1) / math.factorial(2 * i + 1)
cos += (-1)**i * x**(2 * i) / math.factorial(2 * i)
return sin + cos
nfact = {1:1, 2:2}
accuracy = 10
def fact(n):
global nfact
if n in nfact:
return nfact[n]
else:
ans = n*fact(n-1)
nfact[n] = ans
return ans
def sin(n):
ans = 0
for i in range(1, accuracy, 2):
ans += (n ** i) / fact(i) * (-1) ** (i // 2)
return ans
def cos(n):
ans = 1
for i in range(2, accuracy, 2):
ans += (n ** i) / fact(i) * (-1) ** (i//2)
return ans
x = float(input('input number: '))
print('result: ', sin(x) + cos(x))
import math
def calSin(x):
range = 0.1**10
x = x * math.pi / 180
sin = x
i = 1
while x > range :
x = x**(2*i + 1) / math.factorial(2*i + 1)
sin += (-1)**(2*i + 1) * x
i += 1
return sin
def calCos(x):
range = 0.1**8
x = x * math.pi / 180
cos = 1
i = 1
while x > range :
x = x**(2*i) / math.factorial(2*i)
cos += ((-1)**i) * x
i += 1
return cos
q = int(input('육십분법의 각을 입력하세요(예: 0 ~ 360) : '))
print('\nsin{0} + cos{0} = {1}'.format(q, calSin(q)+calCos(q)))