Skip to content

Commit d728f5a

Browse files
advik-student-devpre-commit-ci[bot]cclauss
authored
Added some more comments to volume.py in maths folder (TheAlgorithms#7080)
* Added some more comments added some more comments (to formulas which need it) which make the code more readable and understandable. might make a list of all the formulas on the top, later * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestions from code review * The order changes the result * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix long line * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent c6582b3 commit d728f5a

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

maths/volume.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
2-
Find Volumes of Various Shapes.
3-
Wikipedia reference: https://en.wikipedia.org/wiki/Volume
2+
Find the volume of various shapes.
3+
* https://en.wikipedia.org/wiki/Volume
4+
* https://en.wikipedia.org/wiki/Spherical_cap
45
"""
56
from __future__ import annotations
67

@@ -30,8 +31,7 @@ def vol_cube(side_length: int | float) -> float:
3031

3132
def vol_spherical_cap(height: float, radius: float) -> float:
3233
"""
33-
Calculate the Volume of the spherical cap.
34-
:return 1/3 pi * height ^ 2 * (3 * radius - height)
34+
Calculate the volume of the spherical cap.
3535
>>> vol_spherical_cap(1, 2)
3636
5.235987755982988
3737
>>> vol_spherical_cap(1.6, 2.6)
@@ -49,6 +49,7 @@ def vol_spherical_cap(height: float, radius: float) -> float:
4949
"""
5050
if height < 0 or radius < 0:
5151
raise ValueError("vol_spherical_cap() only accepts non-negative values")
52+
# Volume is 1/3 pi * height squared * (3 * radius - height)
5253
return 1 / 3 * pi * pow(height, 2) * (3 * radius - height)
5354

5455

@@ -263,6 +264,7 @@ def vol_sphere(radius: float) -> float:
263264
"""
264265
if radius < 0:
265266
raise ValueError("vol_sphere() only accepts non-negative values")
267+
# Volume is 4/3 * pi * radius cubed
266268
return 4 / 3 * pi * pow(radius, 3)
267269

268270

@@ -274,7 +276,7 @@ def vol_hemisphere(radius: float) -> float:
274276
>>> vol_hemisphere(1)
275277
2.0943951023931953
276278
>>> vol_hemisphere(7)
277-
718.3775201208659
279+
718.377520120866
278280
>>> vol_hemisphere(1.6)
279281
8.57864233940253
280282
>>> vol_hemisphere(0)
@@ -286,7 +288,8 @@ def vol_hemisphere(radius: float) -> float:
286288
"""
287289
if radius < 0:
288290
raise ValueError("vol_hemisphere() only accepts non-negative values")
289-
return 2 / 3 * pi * pow(radius, 3)
291+
# Volume is radius cubed * pi * 2/3
292+
return pow(radius, 3) * pi * 2 / 3
290293

291294

292295
def vol_circular_cylinder(radius: float, height: float) -> float:
@@ -312,7 +315,8 @@ def vol_circular_cylinder(radius: float, height: float) -> float:
312315
"""
313316
if height < 0 or radius < 0:
314317
raise ValueError("vol_circular_cylinder() only accepts non-negative values")
315-
return pi * pow(radius, 2) * height
318+
# Volume is radius squared * height * pi
319+
return pow(radius, 2) * height * pi
316320

317321

318322
def vol_hollow_circular_cylinder(
@@ -344,6 +348,7 @@ def vol_hollow_circular_cylinder(
344348
...
345349
ValueError: outer_radius must be greater than inner_radius
346350
"""
351+
# Volume - (outer_radius squared - inner_radius squared) * pi * height
347352
if inner_radius < 0 or outer_radius < 0 or height < 0:
348353
raise ValueError(
349354
"vol_hollow_circular_cylinder() only accepts non-negative values"
@@ -356,7 +361,7 @@ def vol_hollow_circular_cylinder(
356361
def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> float:
357362
"""Calculate the Volume of a Conical Frustum.
358363
Wikipedia reference: https://en.wikipedia.org/wiki/Frustum
359-
:return 1/3 * pi * height * (radius_1^2 + radius_top^2 + radius_1 * radius_2)
364+
360365
>>> vol_conical_frustum(45, 7, 28)
361366
48490.482608158454
362367
>>> vol_conical_frustum(1, 1, 2)
@@ -378,6 +383,8 @@ def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> floa
378383
...
379384
ValueError: vol_conical_frustum() only accepts non-negative values
380385
"""
386+
# Volume is 1/3 * pi * height *
387+
# (radius_1 squared + radius_2 squared + radius_1 * radius_2)
381388
if radius_1 < 0 or radius_2 < 0 or height < 0:
382389
raise ValueError("vol_conical_frustum() only accepts non-negative values")
383390
return (

0 commit comments

Comments
 (0)