From b7b55f7d3575716f3eb2d3df8d02adff5a169047 Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Sun, 19 Oct 2025 12:59:28 +0700 Subject: [PATCH 1/3] feat: menambahkan algoritma cosinus --- math/cos.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 math/cos.py diff --git a/math/cos.py b/math/cos.py new file mode 100644 index 00000000..7e476a9f --- /dev/null +++ b/math/cos.py @@ -0,0 +1,56 @@ +# Menghitung cosisnus dengan menggunakan deret taylor +# https://en.wikipedia.org/wiki/Taylor_series#Trigonometric_functions +import numpy as np + + +def factorial(x): + if x < 0: + return ValueError("Angkanya harus bilangan real") + if x == 0 or x == 1: + return 1 + else: + return x * (factorial(x - 1)) + + +def cosinus(sudut): + """ + Cosinus adalah sebuah perbandingan dari sisi samping dan + miring segitiga siku-siku. + + >>> cosinus(0) + 1.0 + >>> cosinus(30) + 0.8660254037844386 + >>> cosinus(45) + 0.7071067811865475 + >>> cosinus(60) + 0.5000000000000001 + >>> cosinus(90) + -3.3769215522516056e-15 + """ + result = 0.0 + interable = 10 + radian = sudut * np.pi / 180 + for i in range(interable): + numerator = np.pow(radian, 2 * i) * np.pow(-1, i) + detector = factorial(2 * i) + result = result + (numerator / detector) + + return float(result) + + +def main(args=None): + import doctest + + doctest.testmod() + + # base case + print(cosinus(0)) # 1.0 + print(cosinus(30)) # 0.8660254037844386 + print(cosinus(45)) # 0.7071067811865475 + print(cosinus(60)) # 0.5000000000000001 + print(cosinus(90)) # -3.3769215522516056e-15 + + +if __name__ == "__main__": + main() From 9adc67e61cf75ca4902c3ccf90656bd4b7372f36 Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Wed, 22 Oct 2025 08:26:23 +0700 Subject: [PATCH 2/3] fix: menghapus numpy --- math/cos.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/math/cos.py b/math/cos.py index 7e476a9f..c3bb1f01 100644 --- a/math/cos.py +++ b/math/cos.py @@ -1,6 +1,6 @@ # Menghitung cosisnus dengan menggunakan deret taylor # https://en.wikipedia.org/wiki/Taylor_series#Trigonometric_functions -import numpy as np +import math def factorial(x): @@ -30,9 +30,9 @@ def cosinus(sudut): """ result = 0.0 interable = 10 - radian = sudut * np.pi / 180 + radian = sudut * math.pi / 180 for i in range(interable): - numerator = np.pow(radian, 2 * i) * np.pow(-1, i) + numerator = math.pow(radian, 2 * i) * math.pow(-1, i) detector = factorial(2 * i) result = result + (numerator / detector) From fb338154845ed758c6a48e61a9f6526c96d49e11 Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Thu, 13 Nov 2025 20:40:28 +0700 Subject: [PATCH 3/3] fix: menambahkan type hints --- math/cos.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/math/cos.py b/math/cos.py index c3bb1f01..91e38c58 100644 --- a/math/cos.py +++ b/math/cos.py @@ -3,7 +3,7 @@ import math -def factorial(x): +def factorial(x) -> int: if x < 0: return ValueError("Angkanya harus bilangan real") if x == 0 or x == 1: @@ -12,7 +12,7 @@ def factorial(x): return x * (factorial(x - 1)) -def cosinus(sudut): +def cosinus(sudut) -> float: """ Cosinus adalah sebuah perbandingan dari sisi samping dan miring segitiga siku-siku.