Skip to content

Commit

Permalink
feat: menambahkan konversi matematika (#2)
Browse files Browse the repository at this point in the history
- derajat ke radian
- radian ke gradian
- gradian ke radian

Signed-off-by: slowy07 <[email protected]>
  • Loading branch information
slowy07 authored Jan 19, 2024
1 parent 61d92a4 commit 6221e4b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
39 changes: 39 additions & 0 deletions OpenSeries/matematika.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,45 @@ def radian_ke_derajat(radian: Union[float, int]) -> Union[float, str]:
return radian * (180 / constant.pi)


def derajat_ke_radian(derajat: Union[float, int]) -> Union[float, str]:
"""
mengubah nilai derajat ke nilai radian
parameter:
derajat (float atau int): nilai derajat
"""
if not isinstance(derajat, (float, int)):
return pesan_error.error_tipe_data(["float", "int"])
else:
return derajat * (constant.pi / 180)


def radian_ke_gradian(radian: Union[float, int]) -> Union[float, str]:
"""
mengubah nilai radian ke gradian
Parameter:
radian (float atau int): nilai radian
"""
if not isinstance(radian, (float, int)):
return pesan_error.error_tipe_data(["float", "int"])
else:
return radian * (200 / constant.pi)


def gradian_ke_radian(gradian: Union[float, int]) -> Union[float, str]:
"""
mengubah nilai gradian ke radian
parameter:
gradian (float atau int): nilai gradian
"""
if not isinstance(gradian, (float, int)):
return pesan_error.error_tipe_data(["float", "int"])
else:
return gradian * (constant.pi / 200)


def luas_lingkaran(jari: Union[float, int]) -> Union[float, str]:
"""
menghitung luas lingkaran
Expand Down
4 changes: 2 additions & 2 deletions testing/main_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from testing.matematika_test import (
TestKonversi,
TestKelilingLingkaran,
TestRadianKeDerajat,
TestDiameterLingkaran,
TestPersamaanKuadrat,
TestRataRata,
Expand All @@ -23,7 +23,7 @@
if __name__ == "__main__":
testing_matematika: list = [
TestKelilingLingkaran,
TestRadianKeDerajat,
TestKonversi,
TestDiameterLingkaran,
TestPersamaanKuadrat,
TestRataRata,
Expand Down
34 changes: 24 additions & 10 deletions testing/matematika_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,32 @@
from OpenSeries.util import error as error


class TestRadianKeDerajat(unittest.TestCase):
def test_tipe_data_float(self):
hasil = matematika.radian_ke_derajat(1.0)
self.assertEqual(hasil, 57.29577951308232)
class TestKonversi(unittest.TestCase):
def test_radian_ke_derajat(self):
self.assertAlmostEqual(matematika.radian_ke_derajat(1), 57.2957795131)
self.assertAlmostEqual(matematika.radian_ke_derajat(0), 0)
self.assertEqual(
matematika.radian_ke_derajat("12"), error.error_tipe_data(["float", "int"])
)

def test_tipe_data_int(self):
hasil = matematika.radian_ke_derajat(2)
self.assertEqual(hasil, 114.59155902616465)
def test_derajat_ke_radian(self):
self.assertAlmostEqual(matematika.derajat_ke_radian(180), 3.1415926535)
self.assertAlmostEqual(matematika.derajat_ke_radian(0), 0)
self.assertEqual(
matematika.derajat_ke_radian("15"), error.error_tipe_data(["float", "int"])
)

def test_nilai_input_tidak_valid(self):
hasil = matematika.radian_ke_derajat("12")
self.assertEqual(hasil, error.error_tipe_data(["float", "int"]))
def test_radian_ke_gradian(self):
self.assertAlmostEqual(matematika.radian_ke_gradian(180), 11459.1559026)
self.assertEqual(
matematika.radian_ke_gradian("128"), error.error_tipe_data(["float", "int"])
)

def test_gradian_ke_radian(self):
self.assertAlmostEqual(matematika.gradian_ke_radian(52), 0.8168140899)
self.assertEqual(
matematika.gradian_ke_radian("200"), error.error_tipe_data(["float", "int"])
)


class TestKelilingLingkaran(unittest.TestCase):
Expand Down

0 comments on commit 6221e4b

Please sign in to comment.