August 27, 2015

PowerShell - Math

PowerShell supports mathematical tasks primarily through its support for the System.Math class in the .NET Framework.

To explore what methods we have in Math class, lets run this command:
[Math] | Get-Member -Static -MemberType Method

TypeNameNameMemberTypeDefinition
System.MathAbsMethodstatic sbyte Abs(sbyte value), static int16 Abs(int16 value), static int Abs(int value), static long Abs(long value), static float Abs(float value), static double Abs(double value), static decimal Abs(de cimal value)
System.MathAcosMethodstatic double Acos(double d)</ td>
System.MathAsinMethodstatic double Asin(double d)</ td>
System.MathAtanMethodstatic double Atan(double d)</ td>
System.MathAtan2Methodstatic double Atan2(double y, double x)
System.MathBigMulMethodstatic long BigMul(int a, in t b)
System.MathCeilingMethodstatic decimal Ceiling(deci mal d), static double Ceiling(double a)
System.MathCosMethodstatic double Cos(double d)
System.MathCoshMethodstatic double Cosh(double valu e)
System.MathDivRemMethodstatic int DivRem(int a, int b, [ref] int result), static long DivRem(long a, long b, [ref] long result)
System.MathEqualsMethodstatic bool Equals(System.Ob ject objA, System.Object objB)
System.MathExpMethodstatic double Exp(double d)
System.MathFloorMethodstatic decimal Floor(decimal d), static double Floor(double d)
System.MathIEEERemainderMethodstatic double IEEERem ainder(double x, double y)
System.MathLogMethodstatic double Log(double d), st atic double Log(double a, double newBase)
System.MathLog10Methodstatic double Log10(double d)
System.MathMaxMethodstatic sbyte Max(sbyte val1, sb yte val2), static byte Max(byte val1, byte val2), static int16 Max(int16 val1, int16 v al2), static uint16 Max(uint16 val1, uint16 val2), static int Max(int val1, int val2), static uint32 Max(uint32 val1, uint32 val2), static long Max(long val1, long val2), s tatic uint64 Max(uint64 val1, uint64 val2), static float Max(float val1, float val2), static double Max(double val1, double val2), static decimal Max(decimal val1, decimal val2)
System.MathMinMethodstatic sbyte Min(sbyte val1, sb yte val2), static byte Min(byte val1, byte val2), static int16 Min(int16 val1, int16 v al2), static uint16 Min(uint16 val1, uint16 val2), static int Min(int val1, int val2), static uint32 Min(uint32 val1, uint32 val2), static long Min(long val1, long val2), s tatic uint64 Min(uint64 val1, uint64 val2), static float Min(float val1, float val2), static double Min(double val1, double val2), static decimal Min(decimal val1, decimal val2)
System.MathPowMethodstatic double Pow(double x, dou ble y)
System.MathReferenceEqualsMethodstatic bool Referen ceEquals(System.Object objA, System.Object objB)
System.MathRoundMethodstatic double Round(double a) , static double Round(double value, int digits), static double Round(double value, Sys tem.MidpointRounding mode), static double Round(double value, int digits, System.Midpo intRounding mode), static decimal Round(decimal d), static decimal Round(decimal d, in t decimals), static decimal Round(decimal d, System.MidpointRounding mode), static dec imal Round(decimal d, int decimals, System.MidpointRounding mode)
System.MathSignMethodstatic int Sign(sbyte value), static int Sign(int16 value), static int Sign(int value), static int Sign(long value), static int Sign(float value), static int Sign(double value), static int Sign(decimal value)
System.MathSinMethodstatic double Sin(double a)
System.MathSinhMethodstatic double Sinh(double valu e)
System.MathSqrtMethodstatic double Sqrt(double d)</ td>
System.MathTanMethodstatic double Tan(double a)
System.MathTanhMethodstatic double Tanh(double valu e)
System.MathTruncateMethodstatic decimal Truncate(de cimal d), static double Truncate(double d)


And also get properties:
[Math] | Get-Member -Static -MemberType Property

   TypeName: System.Math

Name MemberType Definition            
---- ---------- ----------            
E    Property   static double E {get;}
PI   Property   static double PI {get;}

These methods are as simple to use as it seems. Lets test few methods.

function trunc($number) { [Math]::Truncate($number) }
$result = 3/2
trunc $result
1

Math.Truncate method will converts the decimal number to integral value. We have placed this method in a script block to define our own PowerShell function and then we called it in PowerShell way.

For absolute value of a number, [Math]::Abs() method:
[Math]::Abs(-10.6)
10.6

To find the power of a number, use the [Math]::Pow() method.
[Math]::Pow(5, 2)
25

For Square root of a number, use the [Math]::Sqrt() method:
[Math]::Sqrt(100)
10

To find the sine, cosine, or tangent of an angle (given in radians), use the [Math]::Sin(),
[Math]::Cos(), or [Math]::Tan() method:
[Math]::Sin( [Math]::PI / 2 )
1

Similarly to find the angle of a sine, cosine, or tangent value, use the [Math]::ASin(), [Math]::ACos(), or [Math]::ATan() methods

Use the Measure-Object cmdlet to measure these statistical properties of a list. Just pipe the objects to the Measure-Object cmdlet:
1..10 | Measure-Object -Average -Sum -Maximum -Minimum
Count    : 10
Average  : 5.5
Sum      : 55
Maximum  : 10
Minimum  : 1
Property :

To measure the numeric features of a specific property in a stream of objects, supply that property name to the -Property parameter of the Measure-Object cmdlet. For example, in a directory with files:
Get-ChildItem | Measure-Object -Property Length -Max -Min -Average -Sum
Count    : 16
Average  : 831.25
Sum      : 13300
Maximum  : 2756
Minimum  : 67
Property : Length

References:
Windows PowerShell Cookbook


No comments:

Post a Comment