Sunday, June 26, 2016

PowerShell v3 in a Year Day 4 about Arithmetic Operators

,
Much of the arithmetic operator information is straightforward, as you would assume it to be in algebra or math class:

  • + : adds the value on the right to the value on the left
  • - : subtracts the value on the right from the value on the left
  • - : preceding a number, negates it
  • * : multiples two numbers
  • * : copies strings/arrays the number of times specified
  • / : divides the two numbers
  • % : returns the modulo, aka, the remainder of a division operator
One of the really important things about the arithmetic operators is the order of precedence. Knowing this can solve a lot of little mysteries in weird results. This is straight from the help:
  • Parentheses ()
  • - (for a negative number)
  • *, /, %
  • +, - (for subtraction)
And a series of demonstrations straight from the help to give examples of how it plays out in actual practice:
  • 3+6/3*4= 11
  • 10+4/2 = 12
  • (10+4)/2 = 7
  • (3+3)/(1+1) =
And a few more interest variable evaluations:
  • a= 0
  • $b = 1,2
  • $c = -1,-2
  • $b[$a] = $c[$a++]
  • $b returns 1, -1
Rounding in PowerShell is similar to your standard algebra. If a value is equal to or greater than 0.5 it rounds to the higher integer value. Otherwise, it rounds down.

Interestingly, non-numeric types respond to the arithmetic operators, as in strings, arrays and hashtables. Both addition and multiplication can be done with the above, excluding hashtables. You cant multiply hashtables. When you add any of the above, they are merely concatenated. In the process a new object is created, so if you are doing massive computations, realize memory will get taxed fairly heavily. So, if we have two arrays:
  • $a= 1,2,3
  • $b = 4,5,
    Adding them together gives you:
    • $a+ $b = 1,2,3,4,5,
    One gotcha here is that, if you are performing arithmetic with objects of various types (lets say an int, a string and an array) the leftmost value is what all values are attempted to convert to. If all successfully convert, the operation succeeds. If not, it fails. Here are four examples:
    • "file"+ 16 = file16
    • $array= 1,2,3; $array + 16 = 1,2,3,16
    • $array+ "file"; 1,2,3,file
    • "file"* 3 = filefilefile
    Hash tables and arrays are a bit unique and reading the help can give you more precise example to demonstrate the value. 
    • If you add a hashtable to an array, its an array with a hashtable. 
    • If you add two hashtables, its one larger hashtable. 
    • Hashtables and non-collections cannot be added. 
    • Hashtables with identical keys cannot be added since that invalidates the uniqueness condition of a hashtable key set.
    Using the += operator can add elements to both arrays and hashtables. The syntax for both is different. Not that you must wrap a key/value pair, for a hashtable, to use the += approach, otherwise, you need to use .Add(key,value):
    • $array+= 1
    • $hashtable+= @{d =1}
    When adding two different types, for instance, an int and a decimal, the cast to retain greatest precision is automatically performed. Similarly, if two datatypes require an automatic widening conversion, PowerShell handles it without remark.

    Performing arithmetic operations with variables is essentially the same thing as working with the underlying variable values. For instance, if I have this:
    • $a= 1
    • $b= 1
    • $a+ $b = 2
      Similarly, referring back to the leftmost type determines conversion type rule:
      • $a= Windows
      • $b = PowerShell
      • $c = 3
      • $a + $b + $c = Windows PowerShell 3
      Not only can operators be used with expressions in numbers, strings and arrays... Objects are also subject to the rules. Take the example of Get-Date
      • Get-Date returns Wednesday, September12, 2012 2:30:16 AM 
      • $day= new-timespan-day 1; (get-date) + $day returns Thursday, September13, 2012 2:36:39 AM
      Note the help says get-date + $day, but, when I tested this, you had to run a (get-date) subexpression evaluation first. I threw this in Connect as a doco bug fix if you want to upvote. Lowly, but, anyway...
        A second example:

        • get-process| where {($_.ws * 2) -gt 50mb}  returns a list of process objects whose doubled Working Sets sizes were greater than 50Mb. The key here is to realize you can use calculations on the fly with operators (in this case *, not just +).
        A series of basic arithmetic operators:
        • 1+ 1 = 2
        • 1 - 1 = 0
        • -(6 + 3) = -9
        • * 2 = 12 
        • / 2 = 3.5
        • 7 % 2 = 1
        • w * 3 = www
        There are plenty of further examples that could be explored, but, many of the quirks you will run into are explored above. As you have time, try out combinations of weird things you think you know the answer to so as to validate PowerShells computational rules against your own. This is key to avoiding some of the most confusing of all problems: unexpressed conceptual fundamentals. 

          0 comments to “PowerShell v3 in a Year Day 4 about Arithmetic Operators”

          Post a Comment

           

          Computer Info Copyright © 2016 -- Powered by Blogger