Skip to content

Commit 36e8085

Browse files
authored
added type hints and fixed line overflows
1 parent d95d329 commit 36e8085

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

maths/maclaurin_sin.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
# Maclaurin series approximation of sine
1+
"""
2+
https://en.wikipedia.org/wiki/Taylor_series#Trigonometric_functions
3+
"""
24
from math import factorial
35

46

5-
def maclaurin_sin(theta, accuracy=30):
7+
def maclaurin_sin(theta: float, accuracy: int = 30) -> float:
68
"""
7-
Returns the maclaurin approximation of sine
9+
Finds the maclaurin approximation of sin
10+
811
:param theta: the angle to which sin is found
912
:param accuracy: the degree of accuracy wanted minimum ~ 1.5 theta
1013
:return: the value of sine in radians
1114
12-
1315
>>> maclaurin_sin(10)
1416
-0.54402111088927
1517
>>> maclaurin_sin(-10)
@@ -21,36 +23,30 @@ def maclaurin_sin(theta, accuracy=30):
2123
>>> maclaurin_sin("10")
2224
Traceback (most recent call last):
2325
...
24-
ValueError: maclaurin_sin() requires an int or float value for theta and positive int for accuracy
26+
ValueError: maclaurin_sin() requires int/float for theta and +ive int for accuracy
2527
>>> maclaurin_sin(10, -30)
2628
Traceback (most recent call last):
2729
...
28-
ValueError: maclaurin_sin() requires an int or float value for theta and positive int for accuracy
30+
ValueError: maclaurin_sin() requires int/float for theta and +ive int for accuracy
2931
>>> maclaurin_sin(10, 30.5)
3032
Traceback (most recent call last):
3133
...
32-
ValueError: maclaurin_sin() requires an int or float value for theta and positive int for accuracy
34+
ValueError: maclaurin_sin() requires int/float for theta and +ive int for accuracy
3335
>>> maclaurin_sin(10, "30")
3436
Traceback (most recent call last):
3537
...
36-
ValueError: maclaurin_sin() requires an int or float value for theta and positive int for accuracy
38+
ValueError: maclaurin_sin() requires int/float for theta and +ive int for accuracy
3739
"""
3840

39-
if (
40-
not isinstance(accuracy, int)
41-
or accuracy <= 0
42-
or type(theta) not in [int, float]
43-
):
44-
raise ValueError(
45-
"maclaurin_sin() requires an int or float value for theta and positive int for accuracy"
46-
)
41+
if not isinstance(accuracy, int) or accuracy <= 0 or type(theta) not in [int, float]:
42+
raise ValueError('maclaurin_sin() requires int/float for theta and +ive int for accuracy')
4743

4844
theta = float(theta)
4945

5046
_total = 0
5147
for r in range(accuracy):
52-
_total += ((-1) ** r) * ((theta ** (2 * r + 1)) / (factorial(2 * r + 1)))
53-
return _total
48+
_total += ((-1)**r)*((theta**(2*r+1))/(factorial(2*r+1)))
49+
return float(_total)
5450

5551

5652
if __name__ == "__main__":

0 commit comments

Comments
 (0)