Finds the maclaurin approximation of sin :param theta: the angle to which sin is found :param accuracy: the degree of accuracy wanted minimum :return: the value of sine in radians >>> from math import isclose, sin >>> all(isclose(maclaurin_sin(x, 50), sin(x)) for
(theta: float, accuracy: int = 30)
| 6 | |
| 7 | |
| 8 | def maclaurin_sin(theta: float, accuracy: int = 30) -> float: |
| 9 | """ |
| 10 | Finds the maclaurin approximation of sin |
| 11 | |
| 12 | :param theta: the angle to which sin is found |
| 13 | :param accuracy: the degree of accuracy wanted minimum |
| 14 | :return: the value of sine in radians |
| 15 | |
| 16 | |
| 17 | >>> from math import isclose, sin |
| 18 | >>> all(isclose(maclaurin_sin(x, 50), sin(x)) for x in range(-25, 25)) |
| 19 | True |
| 20 | >>> maclaurin_sin(10) |
| 21 | -0.5440211108893691 |
| 22 | >>> maclaurin_sin(-10) |
| 23 | 0.5440211108893704 |
| 24 | >>> maclaurin_sin(10, 15) |
| 25 | -0.544021110889369 |
| 26 | >>> maclaurin_sin(-10, 15) |
| 27 | 0.5440211108893704 |
| 28 | >>> maclaurin_sin("10") |
| 29 | Traceback (most recent call last): |
| 30 | ... |
| 31 | ValueError: maclaurin_sin() requires either an int or float for theta |
| 32 | >>> maclaurin_sin(10, -30) |
| 33 | Traceback (most recent call last): |
| 34 | ... |
| 35 | ValueError: maclaurin_sin() requires a positive int for accuracy |
| 36 | >>> maclaurin_sin(10, 30.5) |
| 37 | Traceback (most recent call last): |
| 38 | ... |
| 39 | ValueError: maclaurin_sin() requires a positive int for accuracy |
| 40 | >>> maclaurin_sin(10, "30") |
| 41 | Traceback (most recent call last): |
| 42 | ... |
| 43 | ValueError: maclaurin_sin() requires a positive int for accuracy |
| 44 | """ |
| 45 | |
| 46 | if not isinstance(theta, (int, float)): |
| 47 | raise ValueError("maclaurin_sin() requires either an int or float for theta") |
| 48 | |
| 49 | if not isinstance(accuracy, int) or accuracy <= 0: |
| 50 | raise ValueError("maclaurin_sin() requires a positive int for accuracy") |
| 51 | |
| 52 | theta = float(theta) |
| 53 | div = theta // (2 * pi) |
| 54 | theta -= 2 * div * pi |
| 55 | return sum( |
| 56 | (-1) ** r * theta ** (2 * r + 1) / factorial(2 * r + 1) for r in range(accuracy) |
| 57 | ) |
| 58 | |
| 59 | |
| 60 | def maclaurin_cos(theta: float, accuracy: int = 30) -> float: |
no test coverage detected