File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Menghitung cosisnus dengan menggunakan deret taylor
2+ # https://en.wikipedia.org/wiki/Taylor_series#Trigonometric_functions
3+ import math
4+
5+
6+ def factorial (x ) -> int :
7+ if x < 0 :
8+ return ValueError ("Angkanya harus bilangan real" )
9+ if x == 0 or x == 1 :
10+ return 1
11+ else :
12+ return x * (factorial (x - 1 ))
13+
14+
15+ def cosinus (sudut ) -> float :
16+ """
17+ Cosinus adalah sebuah perbandingan dari sisi samping dan
18+ miring segitiga siku-siku.
19+
20+ >>> cosinus(0)
21+ 1.0
22+ >>> cosinus(30)
23+ 0.8660254037844386
24+ >>> cosinus(45)
25+ 0.7071067811865475
26+ >>> cosinus(60)
27+ 0.5000000000000001
28+ >>> cosinus(90)
29+ -3.3769215522516056e-15
30+ """
31+ result = 0.0
32+ interable = 10
33+ radian = sudut * math .pi / 180
34+ for i in range (interable ):
35+ numerator = math .pow (radian , 2 * i ) * math .pow (- 1 , i )
36+ detector = factorial (2 * i )
37+ result = result + (numerator / detector )
38+
39+ return float (result )
40+
41+
42+ def main (args = None ):
43+ import doctest
44+
45+ doctest .testmod ()
46+
47+ # base case
48+ print (cosinus (0 )) # 1.0
49+ print (cosinus (30 )) # 0.8660254037844386
50+ print (cosinus (45 )) # 0.7071067811865475
51+ print (cosinus (60 )) # 0.5000000000000001
52+ print (cosinus (90 )) # -3.3769215522516056e-15
53+
54+
55+ if __name__ == "__main__" :
56+ main ()
You can’t perform that action at this time.
0 commit comments