Monthly Archive

Categories

Maths Functions

I’ve needed to access the maths functions in .NET for a few things recently and finally decided to create functions for the ones I use all the time.  I’ve started with 3 functions and put them into a module. The good thing about this is that I can easily load them dynamically and any other modules I create can load this module.

 

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
#Requires -version 2.0
function pi {
    [System.Math]::PI
}
function power {
    param ([double]$a, [double]$b)
    [System.Math]::Pow($a, $b)
}
function sqrt {
    param ([double]$a)
    [System.Math]::Sqrt($a)
}
function pi {
    [System.Math]::PI
}

 

Three functions defined so far. 

pi returns the value of PI – 3.142…..

power raises a number to a particular power so

$x = power 3 3

is equivalent to 3*3*3

sqrt returns the square root of the number

if you want other roots use

power 1000 (1/3) to get the cube root – replace 3 by whatever root is required

I’ll add a few more tomorrow.

Technorati Tags: PowerShell,Maths

Leave a Reply