find the volume of various shapes https en wikipedia orgwikivolume https en wikipedia orgwikisphericalcap calculate the volume of a cube volcube1 1 0 volcube3 27 0 volcube0 0 0 volcube1 6 4 096000000000001 volcube1 traceback most recent call last valueerror volcube only accepts nonnegative values calculate the volume of the spherical cap volsphericalcap1 2 5 235987755982988 volsphericalcap1 6 2 6 16 621119532592402 volsphericalcap0 0 0 0 volsphericalcap1 2 traceback most recent call last valueerror volsphericalcap only accepts nonnegative values volsphericalcap1 2 traceback most recent call last valueerror volsphericalcap only accepts nonnegative values volume is 13 pi height squared 3 radius height calculate the volume of the intersection of two spheres the intersection is composed by two spherical caps and therefore its volume is the sum of the volumes of the spherical caps first it calculates the heights h1 h2 of the spherical caps then the two volumes and it returns the sum the height formulas are h1 radius1 radius2 centersdistance radius1 radius2 centersdistance 2 centersdistance h2 radius2 radius1 centersdistance radius2 radius1 centersdistance 2 centersdistance if centersdistance is 0 then it returns the volume of the smallers sphere return volsphericalcaph1 radius2 volsphericalcaph2 radius1 volspheresintersect2 2 1 21 205750411731103 volspheresintersect2 6 2 6 1 6 40 71504079052372 volspheresintersect0 0 0 0 0 volspheresintersect2 2 1 traceback most recent call last valueerror volspheresintersect only accepts nonnegative values volspheresintersect2 2 1 traceback most recent call last valueerror volspheresintersect only accepts nonnegative values volspheresintersect2 2 1 traceback most recent call last valueerror volspheresintersect only accepts nonnegative values calculate the volume of the union of two spheres that possibly intersect it is the sum of sphere a and sphere b minus their intersection first it calculates the volumes v1 v2 of the spheres then the volume of the intersection i and it returns the sum v1v2i if centersdistance is 0 then it returns the volume of the larger sphere return volsphereradius1 volsphereradius2 volspheresintersectradius1 radius2 centersdistance volspheresunion2 2 1 45 814892864851146 volspheresunion1 56 2 2 1 4 48 77802773671288 volspheresunion0 2 1 traceback most recent call last valueerror volspheresunion only accepts nonnegative values nonzero radius volspheresunion 1 56 2 2 1 4 traceback most recent call last typeerror not supported between instances of str and int volspheresunion1 none 1 traceback most recent call last typeerror not supported between instances of nonetype and int calculate the volume of a cuboid return multiple of width length and height volcuboid1 1 1 1 0 volcuboid1 2 3 6 0 volcuboid1 6 2 6 3 6 14 976 volcuboid0 0 0 0 0 volcuboid1 2 3 traceback most recent call last valueerror volcuboid only accepts nonnegative values volcuboid1 2 3 traceback most recent call last valueerror volcuboid only accepts nonnegative values volcuboid1 2 3 traceback most recent call last valueerror volcuboid only accepts nonnegative values calculate the volume of a cone wikipedia reference https en wikipedia orgwikicone return 13 areaofbase height volcone10 3 10 0 volcone1 1 0 3333333333333333 volcone1 6 1 6 0 8533333333333335 volcone0 0 0 0 volcone1 1 traceback most recent call last valueerror volcone only accepts nonnegative values volcone1 1 traceback most recent call last valueerror volcone only accepts nonnegative values calculate the volume of a right circular cone wikipedia reference https en wikipedia orgwikicone return 13 pi radius2 height volrightcirccone2 3 12 566370614359172 volrightcirccone0 0 0 0 volrightcirccone1 6 1 6 4 289321169701265 volrightcirccone1 1 traceback most recent call last valueerror volrightcirccone only accepts nonnegative values volrightcirccone1 1 traceback most recent call last valueerror volrightcirccone only accepts nonnegative values calculate the volume of a prism wikipedia reference https en wikipedia orgwikiprismgeometry return v bh volprism10 2 20 0 volprism11 1 11 0 volprism1 6 1 6 2 5600000000000005 volprism0 0 0 0 volprism1 1 traceback most recent call last valueerror volprism only accepts nonnegative values volprism1 1 traceback most recent call last valueerror volprism only accepts nonnegative values calculate the volume of a pyramid wikipedia reference https en wikipedia orgwikipyramidgeometry return 13 bh volpyramid10 3 10 0 volpyramid1 5 3 1 5 volpyramid1 6 1 6 0 8533333333333335 volpyramid0 0 0 0 volpyramid1 1 traceback most recent call last valueerror volpyramid only accepts nonnegative values volpyramid1 1 traceback most recent call last valueerror volpyramid only accepts nonnegative values calculate the volume of a sphere wikipedia reference https en wikipedia orgwikisphere return 43 pi r3 volsphere5 523 5987755982989 volsphere1 4 1887902047863905 volsphere1 6 17 15728467880506 volsphere0 0 0 volsphere1 traceback most recent call last valueerror volsphere only accepts nonnegative values volume is 43 pi radius cubed calculate the volume of a hemisphere wikipedia reference https en wikipedia orgwikihemisphere other references https www cuemath comgeometryhemisphere return 23 pi radius3 volhemisphere1 2 0943951023931953 volhemisphere7 718 377520120866 volhemisphere1 6 8 57864233940253 volhemisphere0 0 0 volhemisphere1 traceback most recent call last valueerror volhemisphere only accepts nonnegative values volume is radius cubed pi 23 calculate the volume of a circular cylinder wikipedia reference https en wikipedia orgwikicylinder return pi radius2 height volcircularcylinder1 1 3 141592653589793 volcircularcylinder4 3 150 79644737231007 volcircularcylinder1 6 1 6 12 867963509103795 volcircularcylinder0 0 0 0 volcircularcylinder1 1 traceback most recent call last valueerror volcircularcylinder only accepts nonnegative values volcircularcylinder1 1 traceback most recent call last valueerror volcircularcylinder only accepts nonnegative values volume is radius squared height pi calculate the volume of a hollow circular cylinder volhollowcircularcylinder1 2 3 28 274333882308138 volhollowcircularcylinder1 6 2 6 3 6 47 50088092227767 volhollowcircularcylinder1 2 3 traceback most recent call last valueerror volhollowcircularcylinder only accepts nonnegative values volhollowcircularcylinder1 2 3 traceback most recent call last valueerror volhollowcircularcylinder only accepts nonnegative values volhollowcircularcylinder1 2 3 traceback most recent call last valueerror volhollowcircularcylinder only accepts nonnegative values volhollowcircularcylinder2 1 3 traceback most recent call last valueerror outerradius must be greater than innerradius volhollowcircularcylinder0 0 0 traceback most recent call last valueerror outerradius must be greater than innerradius volume outerradius squared innerradius squared pi height calculate the volume of a conical frustum wikipedia reference https en wikipedia orgwikifrustum volconicalfrustum45 7 28 48490 482608158454 volconicalfrustum1 1 2 7 330382858376184 volconicalfrustum1 6 2 6 3 6 48 7240076620753 volconicalfrustum0 0 0 0 0 volconicalfrustum2 2 1 traceback most recent call last valueerror volconicalfrustum only accepts nonnegative values volconicalfrustum2 2 1 traceback most recent call last valueerror volconicalfrustum only accepts nonnegative values volconicalfrustum2 2 1 traceback most recent call last valueerror volconicalfrustum only accepts nonnegative values volume is 13 pi height radius1 squared radius2 squared radius1 radius2 calculate the volume of a torus wikipedia reference https en wikipedia orgwikitorus return 2pi2 torusradius tuberadius2 voltorus1 1 19 739208802178716 voltorus4 3 710 6115168784338 voltorus3 4 947 4820225045784 voltorus1 6 1 6 80 85179925372404 voltorus0 0 0 0 voltorus1 1 traceback most recent call last valueerror voltorus only accepts nonnegative values voltorus1 1 traceback most recent call last valueerror voltorus only accepts nonnegative values calculate the volume of an icosahedron wikipedia reference https en wikipedia orgwikiregularicosahedron from math import isclose isclosevolicosahedron2 5 34 088984228514256 true isclosevolicosahedron10 2181 694990624912374 true isclosevolicosahedron5 272 711873828114047 true isclosevolicosahedron3 49 92 740688412033628 true volicosahedron0 0 0 volicosahedron1 traceback most recent call last valueerror volicosahedron only accepts nonnegative values volicosahedron0 2 traceback most recent call last valueerror volicosahedron only accepts nonnegative values print the results of various volume calculations printvolumes printfcube volcube2 8 printfcuboid volcuboid2 2 2 8 printfcone volcone2 2 1 33 printfright circular cone volrightcirccone2 2 8 38 printfprism volprism2 2 4 printfpyramid volpyramid2 2 1 33 printfsphere volsphere2 33 5 printfhemisphere volhemisphere2 16 75 printfcircular cylinder volcircularcylinder2 2 25 1 printftorus voltorus2 2 157 9 printfconical frustum volconicalfrustum2 2 4 58 6 printfspherical cap volsphericalcap1 2 5 24 printfspheres intersetion volspheresintersect2 2 1 21 21 printfspheres union volspheresunion2 2 1 45 81 print fhollow circular cylinder volhollowcircularcylinder1 2 3 28 3 printficosahedron volicosahedron2 5 34 09 if name main main calculate the volume of a cube vol_cube 1 1 0 vol_cube 3 27 0 vol_cube 0 0 0 vol_cube 1 6 4 096000000000001 vol_cube 1 traceback most recent call last valueerror vol_cube only accepts non negative values calculate the volume of the spherical cap vol_spherical_cap 1 2 5 235987755982988 vol_spherical_cap 1 6 2 6 16 621119532592402 vol_spherical_cap 0 0 0 0 vol_spherical_cap 1 2 traceback most recent call last valueerror vol_spherical_cap only accepts non negative values vol_spherical_cap 1 2 traceback most recent call last valueerror vol_spherical_cap only accepts non negative values volume is 1 3 pi height squared 3 radius height calculate the volume of the intersection of two spheres the intersection is composed by two spherical caps and therefore its volume is the sum of the volumes of the spherical caps first it calculates the heights h1 h2 of the spherical caps then the two volumes and it returns the sum the height formulas are h1 radius_1 radius_2 centers_distance radius_1 radius_2 centers_distance 2 centers_distance h2 radius_2 radius_1 centers_distance radius_2 radius_1 centers_distance 2 centers_distance if centers_distance is 0 then it returns the volume of the smallers sphere return vol_spherical_cap h1 radius_2 vol_spherical_cap h2 radius_1 vol_spheres_intersect 2 2 1 21 205750411731103 vol_spheres_intersect 2 6 2 6 1 6 40 71504079052372 vol_spheres_intersect 0 0 0 0 0 vol_spheres_intersect 2 2 1 traceback most recent call last valueerror vol_spheres_intersect only accepts non negative values vol_spheres_intersect 2 2 1 traceback most recent call last valueerror vol_spheres_intersect only accepts non negative values vol_spheres_intersect 2 2 1 traceback most recent call last valueerror vol_spheres_intersect only accepts non negative values calculate the volume of the union of two spheres that possibly intersect it is the sum of sphere a and sphere b minus their intersection first it calculates the volumes v1 v2 of the spheres then the volume of the intersection i and it returns the sum v1 v2 i if centers_distance is 0 then it returns the volume of the larger sphere return vol_sphere radius_1 vol_sphere radius_2 vol_spheres_intersect radius_1 radius_2 centers_distance vol_spheres_union 2 2 1 45 814892864851146 vol_spheres_union 1 56 2 2 1 4 48 77802773671288 vol_spheres_union 0 2 1 traceback most recent call last valueerror vol_spheres_union only accepts non negative values non zero radius vol_spheres_union 1 56 2 2 1 4 traceback most recent call last typeerror not supported between instances of str and int vol_spheres_union 1 none 1 traceback most recent call last typeerror not supported between instances of nonetype and int calculate the volume of a cuboid return multiple of width length and height vol_cuboid 1 1 1 1 0 vol_cuboid 1 2 3 6 0 vol_cuboid 1 6 2 6 3 6 14 976 vol_cuboid 0 0 0 0 0 vol_cuboid 1 2 3 traceback most recent call last valueerror vol_cuboid only accepts non negative values vol_cuboid 1 2 3 traceback most recent call last valueerror vol_cuboid only accepts non negative values vol_cuboid 1 2 3 traceback most recent call last valueerror vol_cuboid only accepts non negative values calculate the volume of a cone wikipedia reference https en wikipedia org wiki cone return 1 3 area_of_base height vol_cone 10 3 10 0 vol_cone 1 1 0 3333333333333333 vol_cone 1 6 1 6 0 8533333333333335 vol_cone 0 0 0 0 vol_cone 1 1 traceback most recent call last valueerror vol_cone only accepts non negative values vol_cone 1 1 traceback most recent call last valueerror vol_cone only accepts non negative values calculate the volume of a right circular cone wikipedia reference https en wikipedia org wiki cone return 1 3 pi radius 2 height vol_right_circ_cone 2 3 12 566370614359172 vol_right_circ_cone 0 0 0 0 vol_right_circ_cone 1 6 1 6 4 289321169701265 vol_right_circ_cone 1 1 traceback most recent call last valueerror vol_right_circ_cone only accepts non negative values vol_right_circ_cone 1 1 traceback most recent call last valueerror vol_right_circ_cone only accepts non negative values calculate the volume of a prism wikipedia reference https en wikipedia org wiki prism_ geometry return v bh vol_prism 10 2 20 0 vol_prism 11 1 11 0 vol_prism 1 6 1 6 2 5600000000000005 vol_prism 0 0 0 0 vol_prism 1 1 traceback most recent call last valueerror vol_prism only accepts non negative values vol_prism 1 1 traceback most recent call last valueerror vol_prism only accepts non negative values calculate the volume of a pyramid wikipedia reference https en wikipedia org wiki pyramid_ geometry return 1 3 bh vol_pyramid 10 3 10 0 vol_pyramid 1 5 3 1 5 vol_pyramid 1 6 1 6 0 8533333333333335 vol_pyramid 0 0 0 0 vol_pyramid 1 1 traceback most recent call last valueerror vol_pyramid only accepts non negative values vol_pyramid 1 1 traceback most recent call last valueerror vol_pyramid only accepts non negative values calculate the volume of a sphere wikipedia reference https en wikipedia org wiki sphere return 4 3 pi r 3 vol_sphere 5 523 5987755982989 vol_sphere 1 4 1887902047863905 vol_sphere 1 6 17 15728467880506 vol_sphere 0 0 0 vol_sphere 1 traceback most recent call last valueerror vol_sphere only accepts non negative values volume is 4 3 pi radius cubed calculate the volume of a hemisphere wikipedia reference https en wikipedia org wiki hemisphere other references https www cuemath com geometry hemisphere return 2 3 pi radius 3 vol_hemisphere 1 2 0943951023931953 vol_hemisphere 7 718 377520120866 vol_hemisphere 1 6 8 57864233940253 vol_hemisphere 0 0 0 vol_hemisphere 1 traceback most recent call last valueerror vol_hemisphere only accepts non negative values volume is radius cubed pi 2 3 calculate the volume of a circular cylinder wikipedia reference https en wikipedia org wiki cylinder return pi radius 2 height vol_circular_cylinder 1 1 3 141592653589793 vol_circular_cylinder 4 3 150 79644737231007 vol_circular_cylinder 1 6 1 6 12 867963509103795 vol_circular_cylinder 0 0 0 0 vol_circular_cylinder 1 1 traceback most recent call last valueerror vol_circular_cylinder only accepts non negative values vol_circular_cylinder 1 1 traceback most recent call last valueerror vol_circular_cylinder only accepts non negative values volume is radius squared height pi calculate the volume of a hollow circular cylinder vol_hollow_circular_cylinder 1 2 3 28 274333882308138 vol_hollow_circular_cylinder 1 6 2 6 3 6 47 50088092227767 vol_hollow_circular_cylinder 1 2 3 traceback most recent call last valueerror vol_hollow_circular_cylinder only accepts non negative values vol_hollow_circular_cylinder 1 2 3 traceback most recent call last valueerror vol_hollow_circular_cylinder only accepts non negative values vol_hollow_circular_cylinder 1 2 3 traceback most recent call last valueerror vol_hollow_circular_cylinder only accepts non negative values vol_hollow_circular_cylinder 2 1 3 traceback most recent call last valueerror outer_radius must be greater than inner_radius vol_hollow_circular_cylinder 0 0 0 traceback most recent call last valueerror outer_radius must be greater than inner_radius volume outer_radius squared inner_radius squared pi height calculate the volume of a conical frustum wikipedia reference https en wikipedia org wiki frustum vol_conical_frustum 45 7 28 48490 482608158454 vol_conical_frustum 1 1 2 7 330382858376184 vol_conical_frustum 1 6 2 6 3 6 48 7240076620753 vol_conical_frustum 0 0 0 0 0 vol_conical_frustum 2 2 1 traceback most recent call last valueerror vol_conical_frustum only accepts non negative values vol_conical_frustum 2 2 1 traceback most recent call last valueerror vol_conical_frustum only accepts non negative values vol_conical_frustum 2 2 1 traceback most recent call last valueerror vol_conical_frustum only accepts non negative values volume is 1 3 pi height radius_1 squared radius_2 squared radius_1 radius_2 calculate the volume of a torus wikipedia reference https en wikipedia org wiki torus return 2pi 2 torus_radius tube_radius 2 vol_torus 1 1 19 739208802178716 vol_torus 4 3 710 6115168784338 vol_torus 3 4 947 4820225045784 vol_torus 1 6 1 6 80 85179925372404 vol_torus 0 0 0 0 vol_torus 1 1 traceback most recent call last valueerror vol_torus only accepts non negative values vol_torus 1 1 traceback most recent call last valueerror vol_torus only accepts non negative values calculate the volume of an icosahedron wikipedia reference https en wikipedia org wiki regular_icosahedron from math import isclose isclose vol_icosahedron 2 5 34 088984228514256 true isclose vol_icosahedron 10 2181 694990624912374 true isclose vol_icosahedron 5 272 711873828114047 true isclose vol_icosahedron 3 49 92 740688412033628 true vol_icosahedron 0 0 0 vol_icosahedron 1 traceback most recent call last valueerror vol_icosahedron only accepts non negative values vol_icosahedron 0 2 traceback most recent call last valueerror vol_icosahedron only accepts non negative values print the results of various volume calculations 8 8 1 33 8 38 4 1 33 33 5 16 75 25 1 157 9 58 6 5 24 21 21 45 81 28 3 34 09
from __future__ import annotations

from math import pi, pow


def vol_cube(side_length: float) -> float:
    
    if side_length < 0:
        raise ValueError("vol_cube() only accepts non-negative values")
    return pow(side_length, 3)


def vol_spherical_cap(height: float, radius: float) -> float:
    
    if height < 0 or radius < 0:
        raise ValueError("vol_spherical_cap() only accepts non-negative values")
    
    return 1 / 3 * pi * pow(height, 2) * (3 * radius - height)


def vol_spheres_intersect(
    radius_1: float, radius_2: float, centers_distance: float
) -> float:
    
    if radius_1 < 0 or radius_2 < 0 or centers_distance < 0:
        raise ValueError("vol_spheres_intersect() only accepts non-negative values")
    if centers_distance == 0:
        return vol_sphere(min(radius_1, radius_2))

    h1 = (
        (radius_1 - radius_2 + centers_distance)
        * (radius_1 + radius_2 - centers_distance)
        / (2 * centers_distance)
    )
    h2 = (
        (radius_2 - radius_1 + centers_distance)
        * (radius_2 + radius_1 - centers_distance)
        / (2 * centers_distance)
    )

    return vol_spherical_cap(h1, radius_2) + vol_spherical_cap(h2, radius_1)


def vol_spheres_union(
    radius_1: float, radius_2: float, centers_distance: float
) -> float:
    

    if radius_1 <= 0 or radius_2 <= 0 or centers_distance < 0:
        raise ValueError(
            "vol_spheres_union() only accepts non-negative values, non-zero radius"
        )

    if centers_distance == 0:
        return vol_sphere(max(radius_1, radius_2))

    return (
        vol_sphere(radius_1)
        + vol_sphere(radius_2)
        - vol_spheres_intersect(radius_1, radius_2, centers_distance)
    )


def vol_cuboid(width: float, height: float, length: float) -> float:
    
    if width < 0 or height < 0 or length < 0:
        raise ValueError("vol_cuboid() only accepts non-negative values")
    return float(width * height * length)


def vol_cone(area_of_base: float, height: float) -> float:
    
    if height < 0 or area_of_base < 0:
        raise ValueError("vol_cone() only accepts non-negative values")
    return area_of_base * height / 3.0


def vol_right_circ_cone(radius: float, height: float) -> float:
    
    if height < 0 or radius < 0:
        raise ValueError("vol_right_circ_cone() only accepts non-negative values")
    return pi * pow(radius, 2) * height / 3.0


def vol_prism(area_of_base: float, height: float) -> float:
    
    if height < 0 or area_of_base < 0:
        raise ValueError("vol_prism() only accepts non-negative values")
    return float(area_of_base * height)


def vol_pyramid(area_of_base: float, height: float) -> float:
    
    if height < 0 or area_of_base < 0:
        raise ValueError("vol_pyramid() only accepts non-negative values")
    return area_of_base * height / 3.0


def vol_sphere(radius: float) -> float:
    
    if radius < 0:
        raise ValueError("vol_sphere() only accepts non-negative values")
    
    return 4 / 3 * pi * pow(radius, 3)


def vol_hemisphere(radius: float) -> float:
    
    if radius < 0:
        raise ValueError("vol_hemisphere() only accepts non-negative values")
    
    return pow(radius, 3) * pi * 2 / 3


def vol_circular_cylinder(radius: float, height: float) -> float:
    
    if height < 0 or radius < 0:
        raise ValueError("vol_circular_cylinder() only accepts non-negative values")
    
    return pow(radius, 2) * height * pi


def vol_hollow_circular_cylinder(
    inner_radius: float, outer_radius: float, height: float
) -> float:
    
    
    if inner_radius < 0 or outer_radius < 0 or height < 0:
        raise ValueError(
            "vol_hollow_circular_cylinder() only accepts non-negative values"
        )
    if outer_radius <= inner_radius:
        raise ValueError("outer_radius must be greater than inner_radius")
    return pi * (pow(outer_radius, 2) - pow(inner_radius, 2)) * height


def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> float:
    
    
    
    if radius_1 < 0 or radius_2 < 0 or height < 0:
        raise ValueError("vol_conical_frustum() only accepts non-negative values")
    return (
        1
        / 3
        * pi
        * height
        * (pow(radius_1, 2) + pow(radius_2, 2) + radius_1 * radius_2)
    )


def vol_torus(torus_radius: float, tube_radius: float) -> float:
    
    if torus_radius < 0 or tube_radius < 0:
        raise ValueError("vol_torus() only accepts non-negative values")
    return 2 * pow(pi, 2) * torus_radius * pow(tube_radius, 2)


def vol_icosahedron(tri_side: float) -> float:
    
    if tri_side < 0:
        raise ValueError("vol_icosahedron() only accepts non-negative values")
    return tri_side**3 * (3 + 5**0.5) * 5 / 12


def main():
    
    print("Volumes:")
    print(f"Cube: {vol_cube(2) = }")  
    print(f"Cuboid: {vol_cuboid(2, 2, 2) = }")  
    print(f"Cone: {vol_cone(2, 2) = }")  
    print(f"Right Circular Cone: {vol_right_circ_cone(2, 2) = }")  
    print(f"Prism: {vol_prism(2, 2) = }")  
    print(f"Pyramid: {vol_pyramid(2, 2) = }")  
    print(f"Sphere: {vol_sphere(2) = }")  
    print(f"Hemisphere: {vol_hemisphere(2) = }")  
    print(f"Circular Cylinder: {vol_circular_cylinder(2, 2) = }")  
    print(f"Torus: {vol_torus(2, 2) = }")  
    print(f"Conical Frustum: {vol_conical_frustum(2, 2, 4) = }")  
    print(f"Spherical cap: {vol_spherical_cap(1, 2) = }")  
    print(f"Spheres intersetion: {vol_spheres_intersect(2, 2, 1) = }")  
    print(f"Spheres union: {vol_spheres_union(2, 2, 1) = }")  
    print(
        f"Hollow Circular Cylinder: {vol_hollow_circular_cylinder(1, 2, 3) = }"
    )  
    print(f"Icosahedron: {vol_icosahedron(2.5) = }")  


if __name__ == "__main__":
    main()