From af39c46cd2040afdb7db7208e85304a9ca44ba93 Mon Sep 17 00:00:00 2001 From: fdai7764 Date: Sun, 28 Jan 2024 15:22:51 +0100 Subject: [PATCH] added test for sin of value between zero and negative 2 times pi as well as corresponding functionality --- src/main/py/trigonometry.py | 13 +++++++------ src/test/py/test_trigonometry.py | 2 ++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/py/trigonometry.py b/src/main/py/trigonometry.py index 0f81ae2..79eb798 100644 --- a/src/main/py/trigonometry.py +++ b/src/main/py/trigonometry.py @@ -15,18 +15,19 @@ def rad2deg(radNumber): return (radNumber * 180) / math.pi def sin_approx_bhaskara(radNumber): + shallFlipTheResult = False #the bhaskara function can only be used between zero and pi. For the rest of the sin period I simply mirrored the first arch of the curve to match the actual sine wave + + if(radNumber < 0): + radNumber *= -1 + shallFlipTheResult = not shallFlipTheResult + while(radNumber > 2 * math.pi): radNumber -= 2 * math.pi - shallFlipTheResult = 0 #the bhaskara function can only be used between zero and pi. For the rest of the sin period I simply mirrored the first arch of the curve to match the actual sine wave if math.pi < radNumber < 2 * math.pi: radNumber = subract(math.pi, radNumber) - shallFlipTheResult = 1 - - if 0 > radNumber > -2*math.pi: - radNumber *= -1 - shallFlipTheResult = 1 + shallFlipTheResult = not shallFlipTheResult num = multiply(16, radNumber) diff --git a/src/test/py/test_trigonometry.py b/src/test/py/test_trigonometry.py index 67342ad..ea90e73 100644 --- a/src/test/py/test_trigonometry.py +++ b/src/test/py/test_trigonometry.py @@ -53,6 +53,8 @@ class MyTestCase(unittest.TestCase): def test_sin_negative_point5_pi_should_be_negative_1(self): self.assertEqual(sin_approx_bhaskara(-0.5*math.pi), -1) + def test_sin_negative_1point5_pi_should_be_1(self): + self.assertEqual(sin_approx_bhaskara(-1.5*math.pi), 1) if __name__ == '__main__': unittest.main()