.Net Math

In addition to standard PowerShell methods we can also call .Net Math functions:

 .NET Math library:

  [math]::Round($var, p)  Round a variable to p places.
  [math]::Round($var)
  [math]::Abs(n)
  [math]::BigMul
  [math]::Ceiling(n)   Rounds everything up, towards positive infinity.
  [math]::DivRem
  [math]::Equals(n,m)  Compare two values and return a [boolean] (true/false) result. (unlike -eq the types must also match)
  [math]::Exp(p)       Raise e to a power p
  [math]::Floor(n)     Rounds everything down, towards negative infinity.
  [math]::GetHashCode
  [math]::GetType
  [math]::Log(n)    base e logarithm of n
  [math]::Log10(n)  base 10 logarithm of n
  [math]::Max(n,m)  Return the larger of two values
  [math]::Min(n,m)  Return the smaller of two values
  [math]::Pow(n,p)  Raise a number n to a power p
  [math]::IEEERemainder
  [math]::Round
  [math]::Sign
  [math]::Sqrt(n)   Square Root of a number n
  [math]::ToString
  [math]::Truncate(n)  Remove the decimal returning only an Integer. Rounds toward zero.

The trig functions all accept an angle, measured in radians.
To convert degrees to radians multiply by pi/180 using [Math]::PI 

  [math]::Acos
  [math]::Asin
  [math]::Atan
  [math]::Atan2
  [math]::Cos
  [math]::Cosh
  [math]::Sin
  [math]::Tan
  [math]::Sinh
  [math]::Tanh

Static mathematical constants:
  [math]::e
  [math]::pi

You can list all the static memners of system.math like this: (via /\/\o\/\/ )

 [system.math] | gm -static

Examples

Find the Minimum value of two variables:

$var1 = 764.4
$var2 = 1409
$answer = [math]::min($var1,$var2)
$answer

Find the Tangent of 45 degrees:

$answer = [System.Math]::Tan(45/180*[System.Math]::PI)
$answer

“ Film is one of the three universal languages, the other two: mathematics and music” ~ Frank Capra

Related PowerShell Commands:

math.class - MSDN scroll down for full definitions.
Concatenation - Several methods to combine strings together.
PowerShell Operators - More advanced Operators for Arrays and formatting expressions.


Copyright © SS64.com 1999-2019
Some rights reserved