Here a few fun things you can do with the Math class (never thought I would say that…)
You’ll see that most objects have properties, methods and (sometimes) events.
Say an object has a property like color. You can read that property and see that it has been set to “Blue”. Most properties can either be read and sometime set. Read is often referred to as Get. So a property often has a Set and Get.
Now a method is other things you can do with methods and the concept will become much clearer. Lets look at some methods you can play around with the Math Class.
Static methods with the Math class. Notice that these methods don’t have closing brackets “()”
PS C:\> [math]::pi # Pi 3.14159265358979 PS C:\> [math]::e # Natural logarithm 2.71828182845905
Here are some dynamic classes you can use with the Math class.
![math_method_powertab](https://reszolver.wordpress.com/wp-content/uploads/2016/11/math_method_powertab.jpg?w=338&h=408)
I am using PowerTab to get a list of all methods.
You can also get them by starting intellisense in the ISE… Just hit Ctrl-Space if IntelliSense doesn’t appear automatically. So here are some of the methods in the math class that I use most often…
PS C:\> 5/3 1.66666666666667 PS C:\> [math]::round(5/3) 2 PS C:\> [math]::floor(5/3) 1 PS C:\> [math]::ceiling(5/3) 2
![intellisense](https://reszolver.wordpress.com/wp-content/uploads/2016/11/intellisense.jpg?w=700)
PS C:\> [math]::Pow(5,2) 25 PS C:\> [math]::Sqrt(25) 5 PS C:\> [math]::Sqrt(5) 2.23606797749979 # Using a method inside another method... PS C:\> [math]::round([math]::Sqrt(5)) 2
So how do you find out how to use one of these methods? Try entering the method without the closing brackets “()” . For example, how do you use the Min method? Can you compare more than two numbers?
PS C:\Users\Yves> [math]::min OverloadDefinitions ------------------- static sbyte Min(sbyte val1, sbyte val2) static byte Min(byte val1, byte val2) static int16 Min(int16 val1, int16 val2) 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) static 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)
PS C:\Users\Yves> [math]::min(55,23) # the minimum of two numbers 23
PS C:\Users\Yves> [math]::sqrt OverloadDefinitions ------------------- static double Sqrt(double d)
PS C:\Users\Yves> [math]::pow OverloadDefinitions ------------------- static double Pow(double x, double y)
One thought on “Stupid math tricks!”