1
- # Maclaurin series approximation of sine
1
+ """
2
+ https://en.wikipedia.org/wiki/Taylor_series#Trigonometric_functions
3
+ """
2
4
from math import factorial
3
5
4
6
5
- def maclaurin_sin (theta , accuracy = 30 ):
7
+ def maclaurin_sin (theta : float , accuracy : int = 30 ) -> float :
6
8
"""
7
- Returns the maclaurin approximation of sine
9
+ Finds the maclaurin approximation of sin
10
+
8
11
:param theta: the angle to which sin is found
9
12
:param accuracy: the degree of accuracy wanted minimum ~ 1.5 theta
10
13
:return: the value of sine in radians
11
14
12
-
13
15
>>> maclaurin_sin(10)
14
16
-0.54402111088927
15
17
>>> maclaurin_sin(-10)
@@ -21,36 +23,30 @@ def maclaurin_sin(theta, accuracy=30):
21
23
>>> maclaurin_sin("10")
22
24
Traceback (most recent call last):
23
25
...
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
25
27
>>> maclaurin_sin(10, -30)
26
28
Traceback (most recent call last):
27
29
...
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
29
31
>>> maclaurin_sin(10, 30.5)
30
32
Traceback (most recent call last):
31
33
...
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
33
35
>>> maclaurin_sin(10, "30")
34
36
Traceback (most recent call last):
35
37
...
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
37
39
"""
38
40
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' )
47
43
48
44
theta = float (theta )
49
45
50
46
_total = 0
51
47
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 )
54
50
55
51
56
52
if __name__ == "__main__" :
0 commit comments