Perform Complex Arithmetic
1. Problem
You want to use PowerShell to calculate more complex or advanced mathematical results.
2. Solution
PowerShell supports more advanced mathematical tasks primarily through its support for the System.Math class in the .NET Framework.
To find the absolute value of a number, use the [Math]::Abs() method:
       PS >[Math]::Abs(-10.6)
       10.6
To find the power (such as the square or the cube) of a number, use the [Math]:: Pow() method. In this case, finding 123 squared:
       PS >[Math]::Pow(123, 2)
       15129
To find the square root of a number, use the [Math]::Sqrt() method:
       PS >[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:
       PS >[Math]::Sin( [Math]::PI / 2 )
       1
To find the angle (given in radians) of a sine, cosine, or tangent value, use the [Math]::ASin(), [Math]::ACos(), or [Math]::ATan() method:
       PS >[Math]::ASin(1)
       1.5707963267949
See Section 3.9, “Learn About Types and Objects.” to learn how to find out what other features the System.Math class provides.
3. Discussion
Once you start working with the System.Math class, it may seem as though its designers left out significant pieces of functionality. The class supports the square root of a number, but doesn’t support other roots (such as the cube root). It supports sine, cosine, and tangent (and their inverses) in radians, but not in the more commonly used measure of degrees.
3.1. Working with any root
To determine any root (such as the cube root) of a number, you can use the function given in Example 6-1.
Example 6-1. A root function and some example calculations
|
Code View: Scroll / Show All PS >function root($number, $root) { [Math]::Exp($([Math]::Log($number) / $root)) }
PS >root 64 3 4 PS >root 25 5 1.90365393871588 PS >[Math]::Pow(1.90365393871588, 5) 25.0000000000001 PS >[Math]::Pow( $(root 25 5), 5) 25 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
|
This function applies the mathematical fact that you can express any root (such as the cube root) of a number as a series of operations with any other well-known root. Although you can pick 2 as a base (which generates square roots and powers of 2), the [Math]::Exp() and [Math]::Log() methods in the System.Math class use a more common mathematical base called e.
|
The example also illustrates a very important point about math on computers. When you use this function (or anything else that manipulates floating point numbers), always be aware that the results of floating point answers are only ever approximations of the actual result. If you combine multiple calculations in the same statement, programming and scripting languages can sometimes improve the accuracy of their answer (such as in the second [Math]::Pow() attempt), but that exception is rare.
Some mathematical systems avoid this problem by working with equations and calculations as symbols (and not numbers). Like humans, these systems know that taking the square of a number that you just took the square root of gives you the original number right back—so they don’t actually have to do either of those operations. These systems, however, are extremely specialized and usually very expensive.
3.2. Working with degrees instead of radians
Converting radians (the way that mathematicians commonly measure angles) to degrees (the way that most people commonly measure angles) is much more straight-forward than the root function. A circle has 2 * Pi radians if you measure in radians, and 360 degrees if you measure in degrees. That gives the following two functions:
Code View: Scroll / Show All
       PS >function Convert-RadiansToDegrees($angle) { $angle / (2 * [Math]::Pi) * 360 }
       PS >function Convert-DegreesToRadians($angle) { $angle / 360 * (2 * [Math]::Pi) }
                                     Â
and their usage:
       PS >Convert-RadiansToDegrees ([Math]::Pi)
       180
       PS >Convert-RadiansToDegrees ([Math]::Pi / 2)
       90
       PS >Convert-DegreesToRadians 360
       6.28318530717959
       PS >Convert-DegreesToRadians 45
       0.785398163397448
       PS >[Math]::Tan( (Convert-DegreesToRadians 45) )
       1
Tags: arithmetic, cmd, cmdlets, complex, convert, framework, math, power shell, powershell, ps 
