Skip to content

Commit 8767031

Browse files
authored
Add comprehensive doctests to arc_length.py
- Improved type hints (int -> float) - Added edge case tests (angle=0, 360, 180) - Added float value tests - Added error handling for negative values - Enhanced docstring with formula and Wikipedia link - Added proper error messages Contributes to #9943
1 parent c79034c commit 8767031

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

maths/arc_length.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,55 @@
11
from math import pi
22

33

4-
def arc_length(angle: int, radius: int) -> float:
4+
def arc_length(angle: float, radius: float) -> float:
55
"""
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+
620
>>> arc_length(45, 5)
721
3.9269908169872414
822
>>> arc_length(120, 15)
923
31.415926535897928
1024
>>> arc_length(90, 10)
1125
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
1246
"""
47+
if angle < 0 or radius < 0:
48+
raise ValueError("arc_length() only accepts non-negative values")
1349
return 2 * pi * radius * (angle / 360)
1450

1551

1652
if __name__ == "__main__":
17-
print(arc_length(90, 10))
53+
import doctest
54+
55+
doctest.testmod()

0 commit comments

Comments
 (0)