Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fungsi derivative #56

Merged
merged 6 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions OpenSeries/matematika.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,20 @@ def integral(
for i in range(1, iterasi):
result += f(a + i * delta)
return round(result * delta)


def turunan(f: Callable[[float], float], x: Union[int, float]) -> float:
"""
Args:
f (Callable[[float]float]: input fungsi
x (int) : input value
Return:
float : hasil dari kalkulasi apromasif
"""
# insial nilai h
h: float = 0.0001
# mengecek tipe data pada nilai input pada paramter a dan b dan iterasi
if not isinstance(x, (float, int)):
return error.ErrorTipeData(["float", "int"])
else:
return (f(x + h) - f(x)) / h
2 changes: 2 additions & 0 deletions testing/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
TestDistribusiBinomial,
TestGaussian,
TestIntegral,
TestDerivative,
)

from testing.fisika_test import (
Expand Down Expand Up @@ -56,6 +57,7 @@
TestDistribusiBinomial,
TestGaussian,
TestIntegral,
TestDerivative,
]

testing_fisika: list = [
Expand Down
22 changes: 22 additions & 0 deletions testing/matematika_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,25 @@ def f(x):
hasil = matematika.integral(a, b, iterasi)
with self.assertRaises(error.ErrorTipeData):
raise hasil


class TestDerivative(unittest.TestCase):
def setUp(self):
def f(x):
return x * x

self.inputFungsi = f
self.inputValue = 4

def testing_input_value_integer(self):
hasil = matematika.turunan(self.inputFungsi, self.inputValue)
self.assertAlmostEqual(hasil, 8.000, places=3)

def testing_input_value_desimal(self):
hasil = matematika.turunan(self.inputFungsi, float(self.inputValue))
self.assertAlmostEqual(hasil, 8.000, places=3)

def testing_input_value_string(self):
hasil = matematika.turunan(self.inputFungsi, str(self.inputValue))
with self.assertRaises(error.ErrorTipeData):
raise hasil
Loading