One of my all time favourite films is “The Thomas Crown Affair” – the original NOT the remake. The films theme tune – Windmillls of your Mind – with its focus on circles got me thinking about geometrical calculations in general and circles in particular. I decided I’d create a library of such calculations – eventually it’ll be available on github – starting with circles.

Two calculations come to mind immediately for circles – area of a circle and its circumference:

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

#region CIRCLES
function Get-CircleArea {
   param (

  $area = $pi * $radius * $radius

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


function Get-CircleCircumference  {
   param (

  $ccmfrnce = 2 * $pi * $radius

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

#endregion CIRCLES

I started by creating a constant for the value of PI. As its a constant you can’t change or remove it. Defining it once saves time and effort.

Both functions take a single parameter – the radius of the circle. The area or circumference is calculated.

Area = pi * radius squared

Circumference = 2 * pi * radius

The output is  rounded to three decimal places.

You’ll notice that the function only contains the calculation. I’ve not added any pipeline ability, help, comments, error handling or any other “production level code”. This is deliberate as I want to concentrate on the calculations and most of these functions will be fairly simple. If you want the bells and whistles think of it as an opportunity <GRIN>

Next up will be spheres.

This entry was posted in Powershell. Bookmark the permalink.

2 Responses to Circles

  1. I dislike the [Math]::Round() at the end, where you’re arbitrarily losing precision (presumably for aesthetic reasons). It’s like finishing a function with a call to a format-* or sort-object cmdlet

    • I don’t like a huge bunch of digits after the decimal point – its pseudo-accuracy and in the vast majority of cases 3 decimal places is more than enough accuracy. The functions are easy to change to remove the rounding if that’s the preference

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s