Implement sin function. >>> sin(0.0) 0.0 >>> sin(90.0) 1.0 >>> sin(180.0) 0.0 >>> sin(270.0) -1.0 >>> sin(0.68) 0.0118679603 >>> sin(1.97) 0.0343762121 >>> sin(64.0) 0.8987940463 >>> sin(9999.0) -0.9876883406 >>> sin(-689.0)
(
angle_in_degrees: float, accuracy: int = 18, rounded_values_count: int = 10
)
| 15 | |
| 16 | |
| 17 | def sin( |
| 18 | angle_in_degrees: float, accuracy: int = 18, rounded_values_count: int = 10 |
| 19 | ) -> float: |
| 20 | """ |
| 21 | Implement sin function. |
| 22 | |
| 23 | >>> sin(0.0) |
| 24 | 0.0 |
| 25 | >>> sin(90.0) |
| 26 | 1.0 |
| 27 | >>> sin(180.0) |
| 28 | 0.0 |
| 29 | >>> sin(270.0) |
| 30 | -1.0 |
| 31 | >>> sin(0.68) |
| 32 | 0.0118679603 |
| 33 | >>> sin(1.97) |
| 34 | 0.0343762121 |
| 35 | >>> sin(64.0) |
| 36 | 0.8987940463 |
| 37 | >>> sin(9999.0) |
| 38 | -0.9876883406 |
| 39 | >>> sin(-689.0) |
| 40 | 0.5150380749 |
| 41 | >>> sin(89.7) |
| 42 | 0.9999862922 |
| 43 | """ |
| 44 | # Simplify the angle to be between 360 and -360 degrees. |
| 45 | angle_in_degrees = angle_in_degrees - ((angle_in_degrees // 360.0) * 360.0) |
| 46 | |
| 47 | # Converting from degrees to radians |
| 48 | angle_in_radians = radians(angle_in_degrees) |
| 49 | |
| 50 | result = angle_in_radians |
| 51 | a = 3 |
| 52 | b = -1 |
| 53 | |
| 54 | for _ in range(accuracy): |
| 55 | result += (b * (angle_in_radians**a)) / factorial(a) |
| 56 | |
| 57 | b = -b # One positive term and the next will be negative and so on... |
| 58 | a += 2 # Increased by 2 for every term. |
| 59 | |
| 60 | return round(result, rounded_values_count) |
| 61 | |
| 62 | |
| 63 | if __name__ == "__main__": |
no test coverage detected