24/7/365 Support

Perform Complex Arithmetic in Windows PowerShell

Problem

You want to use PowerShell to calculate more complex or advanced mathematical results.

Solution

PowerShell supports more advanced mathematical tasks primarily through its sup port 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

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.

Working with any root

To determine any root (such as the cube root) of a number, you can use the function given in Example 61.

Example 61. A root function and some example calculations

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 wellknown 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 [Math]::Exp() and [Math]::Log() functions use e (approximately 2.718) as a base largely because the number appears so frequently in the type of mathematics that use these functions!

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.

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 straightforward than the root function. Acircle has 2*Pi radians if you measure in radians, and 360 degrees if you measure in degrees. That gives the following two functions:

PS >function ConvertRadiansToDegrees($angle) { $angle / (2 * [Math]::Pi) * 360 } PS >function ConvertDegreesToRadians($angle) { $angle / 360 * (2 * [Math]::Pi) }

and their usage:

PS >ConvertRadiansToDegrees ([Math]::Pi) 180 PS >ConvertRadiansToDegrees ([Math]::Pi / 2) 90 PS >ConvertDegreesToRadians 360 6.28318530717959 PS >ConvertDegreesToRadians 45 0.785398163397448 PS >[Math]::Tan( (ConvertDegreesToRadians 45) ) 1

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say