Stupid math tricks!

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
Using PowerTab to see all methods

 

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
Using IntelliSense in the ISE
 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)
So you can see that you can only compare 2 numbers , but of different types…
PS C:\Users\Yves> [math]::min(55,23) # the minimum of two numbers
23
So in the next examples, you can see that you have to use a number that can be converted as a double and the result will always be a double:
PS C:\Users\Yves> [math]::sqrt
OverloadDefinitions
-------------------
static double Sqrt(double d)
And the power method need 2 doubles separated by a comma.  This is practical when you don’t know how to use a method.
PS C:\Users\Yves> [math]::pow
OverloadDefinitions
 -------------------
 static double Pow(double x, double y)
So now you are using raw .NET in PowerShell, you genius you! In my next post (Sting theory) I will show you to mess around with string methods.  Stay  tuned…

One thought on “Stupid math tricks!

Leave a comment