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

feat: menambahkan fungsi fisika #24

Merged
merged 1 commit into from
Jan 28, 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
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ repos:
- --ignore-missing-imports
- --non-interactive
- --install-types
exclude: example
additional_dependencies: [types-requests]

- repo: https://github.com/pre-commit/mirrors-prettier
Expand Down
51 changes: 51 additions & 0 deletions OpenSeries/fisika.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,54 @@ def ketinggian_barometrik(
else:
hasil = 44_330 * (1 - (tekanan / 101_325) ** (1 / 5.5255))
return hasil


def gaya_sentripental(
massa: Union[float, int], velocity: Union[float, int], radius: Union[float, int]
) -> Union[float, int, error.Error, error.ErrorTipeData]:
"""
fungsi untuk menghitung gaya sentripental. gaya sentripental adalah gaya yang bekerja
pada benda dalam gerak lengkung arahnya menuju ke sumbu rotasi atau pusat kelengkungan

Parameter:
massa (float): masa benda
v (float): kecepatan dari benda
radius (float): jari-jari lintasan melingkar
"""
if not all(isinstance(data, (float, int)) for data in [massa, velocity, radius]):
return error.ErrorTipeData(["float", "int"])
if massa < 0:
return error.Error("Massa tidak boleh negatif")
if radius <= 0:
return error.Error("Radius selalu angka positif")
return (massa * (velocity) ** 2) / radius


def efek_doppler(
org_frek: Union[float, int],
gelombang_vel: Union[float, int],
obs_vel: Union[float, int],
src_vel: Union[float, int],
) -> Union[float, error.ErrorDibagiNol, error.ErrorTipeData, error.Error]:
"""
fungsi untuk menghitung efek doppler

parameter:
org_frek (int atau float): frekuensi gelombang sumber diam
gelombang_vel_vel (int atau float): kecepatan gelombang dalam medium
obs_vel (int atau float): kecepatan pengamatan
src_vel (int atau float): kecepatan sumber
"""
if not all(
isinstance(data, (float, int))
for data in [org_frek, gelombang_vel, obs_vel, src_vel]
):
return error.ErrorTipeData(["int", "float"])
if gelombang_vel == src_vel:
return error.ErrorDibagiNol()
doppler = (org_frek * (gelombang_vel + obs_vel)) / (gelombang_vel - src_vel)
if doppler <= 0:
return error.Error(
"frekuensi tidak positif, kecepatan sumber relatif lebih besar dari kecepatan gelombang dalam medium"
)
return doppler
5 changes: 5 additions & 0 deletions example/fisika/efek_doppler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import OpenSeries.fisika as fisika

# menghitung fungsi dari efek doppler
doppler_efek = fisika.efek_doppler(100, 330, 10)
print(doppler_efek)
5 changes: 5 additions & 0 deletions example/fisika/gaya_sentripental.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import OpenSeries.fisika as fisika

# menghitung gaya sentripental
hasil = round(fisika.gaya_sentripental(10, 15, 5))
print(hasil)
49 changes: 49 additions & 0 deletions testing/fisika_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,52 @@ def test_tekanan_tipe_data_salah(self):
hasil = fisika.ketinggian_barometrik("12")
with self.assertRaises(error.ErrorTipeData):
raise hasil


class TestGayaSentripental(unittest.TestCase):
def test_gaya_sentripental(self):
hasil = fisika.gaya_sentripental(10, 5, 2)
self.assertEqual(hasil, 125.0)

def test_gaya_sentripental_error_tipe_data(self):
hasil = fisika.gaya_sentripental("12", 5, 2)
self.assertIsInstance(hasil, error.ErrorTipeData)
with self.assertRaises(error.ErrorTipeData):
raise hasil

def test_gaya_sentripental_minus(self):
hasil = fisika.gaya_sentripental(-10, 5, 2)
self.assertIsInstance(hasil, error.Error)
with self.assertRaises(error.Error):
raise hasil

def test_gaya_sentripental_nol(self):
hasil = fisika.gaya_sentripental(10, 5, 0)
self.assertIsInstance(hasil, error.Error)
with self.assertRaises(error.Error):
raise hasil


class TestEfekDoppler(unittest.TestCase):
def test_efek_doppler(self):
hasil = fisika.efek_doppler(100, 340, 20, 10)
rounding_hasil = round(hasil)
self.assertEqual(rounding_hasil, 109)

def test_efek_doppler_invalid_tipe_data(self):
hasil = fisika.efek_doppler("12", "340", "20", 10)
self.assertIsInstance(hasil, error.ErrorTipeData)
with self.assertRaises(error.ErrorTipeData):
raise hasil

def test_efek_doppler_dibagi_nol(self):
hasil = fisika.efek_doppler(0, 0, 0, 0)
self.assertIsInstance(hasil, error.ErrorDibagiNol)
with self.assertRaises(error.ErrorDibagiNol):
raise hasil

def test_efek_doppler_nilai_negatif(self):
hasil = fisika.efek_doppler(-100, -340, 20, 10)
self.assertIsInstance(hasil, error.Error)
with self.assertRaises(error.Error):
raise hasil
4 changes: 4 additions & 0 deletions testing/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
TestGerakLurusBeraturan,
TestEnergiKinetik,
TestKetinggianBarometrik,
TestGayaSentripental,
TestEfekDoppler,
)

from testing.statistika_test import TestFungsiEntropy, TestFungiStandardDeviasi
Expand All @@ -46,6 +48,8 @@
TestGerakLurusBeraturan,
TestEnergiKinetik,
TestKetinggianBarometrik,
TestGayaSentripental,
TestEfekDoppler,
]

testing_statistika: list = [
Expand Down
Loading