Spheres

Continuing my series of functions that can be used for geometric calculations its time for a quick look at spheres.

Remember in all of these functions that PI is set as a constant when the module is loaded:

New-Variable -Name pi -Value ([math]::PI) -Option Constant

Calculating the volume of a sphere is relatively straight forward. I used the Math Power function to calculate the cube of the radius rather than multiplying it out 3 times as that starts to look messy.

function Get-SphereVolume {
   [CmdletBinding()]
   param (
     [double]$radius
   )

  $vol = (4/3) * $pi * [math]::Pow($radius, 3)

  [math]::Round($vol, 3)

}

The surface area of a sphere is a simpler calculation.

function Get-SphereSurfaceArea {
   [CmdletBinding()]
   param (
     [double]$radius
   )

  $area = 4 * $pi * $radius * $radius

  [math]::Round($area, 3)

}

This entry was posted in Powershell. Bookmark the permalink.

Leave a comment