|
1 | 1 | from math import pi |
2 | 2 |
|
3 | 3 |
|
4 | | -def arc_length(angle: int, radius: int) -> float: |
| 4 | +def arc_length(angle: float, radius: float) -> float: |
5 | 5 | """ |
| 6 | + Calculate the arc length of a circle. |
| 7 | + |
| 8 | + The arc length is the distance along the curved line making up the arc. |
| 9 | + Formula: arc_length = 2 * pi * radius * (angle / 360) |
| 10 | + |
| 11 | + Wikipedia: https://en.wikipedia.org/wiki/Arc_length |
| 12 | + |
| 13 | + Args: |
| 14 | + angle: The angle in degrees |
| 15 | + radius: The radius of the circle |
| 16 | + |
| 17 | + Returns: |
| 18 | + The arc length as a float |
| 19 | + |
6 | 20 | >>> arc_length(45, 5) |
7 | 21 | 3.9269908169872414 |
8 | 22 | >>> arc_length(120, 15) |
9 | 23 | 31.415926535897928 |
10 | 24 | >>> arc_length(90, 10) |
11 | 25 | 15.707963267948966 |
| 26 | + >>> arc_length(0, 10) |
| 27 | + 0.0 |
| 28 | + >>> arc_length(360, 5) |
| 29 | + 31.41592653589793 |
| 30 | + >>> arc_length(180, 10) |
| 31 | + 31.41592653589793 |
| 32 | + >>> arc_length(45.5, 10.5) |
| 33 | + 8.329618601250994 |
| 34 | + >>> arc_length(-90, 10) |
| 35 | + Traceback (most recent call last): |
| 36 | + ... |
| 37 | + ValueError: arc_length() only accepts non-negative values |
| 38 | + >>> arc_length(90, -10) |
| 39 | + Traceback (most recent call last): |
| 40 | + ... |
| 41 | + ValueError: arc_length() only accepts non-negative values |
| 42 | + >>> arc_length(-45, -5) |
| 43 | + Traceback (most recent call last): |
| 44 | + ... |
| 45 | + ValueError: arc_length() only accepts non-negative values |
12 | 46 | """ |
| 47 | + if angle < 0 or radius < 0: |
| 48 | + raise ValueError("arc_length() only accepts non-negative values") |
13 | 49 | return 2 * pi * radius * (angle / 360) |
14 | 50 |
|
15 | 51 |
|
16 | 52 | if __name__ == "__main__": |
17 | | - print(arc_length(90, 10)) |
| 53 | + import doctest |
| 54 | + |
| 55 | + doctest.testmod() |
0 commit comments