-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathvolume_of_torus
More file actions
38 lines (28 loc) · 904 Bytes
/
volume_of_torus
File metadata and controls
38 lines (28 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
Volume of a Torus
Reference: https://en.wikipedia.org/wiki/Torus
"""
from math import pi
def volume_of_torus(major_radius: float, minor_radius: float) -> float:
"""
Calculate the volume of a torus.
Formula:
V = 2 * π² * R * r²
:param major_radius: Distance from the center of the tube to the center of the torus (R)
:param minor_radius: Radius of the tube (r)
:return: Volume of the torus
>>> volume_of_torus(3.0, 1.0)
59.21762640653615
>>> volume_of_torus(5.0, 2.0)
394.7841760435743
>>> volume_of_torus(0.0, 0.0)
0.0
"""
if major_radius < 0:
raise ValueError("major_radius must be non-negative")
if minor_radius < 0:
raise ValueError("minor_radius must be non-negative")
return 2 * (pi ** 2) * major_radius * (minor_radius ** 2)
if __name__ == "__main__":
import doctest
doctest.testmod()