{"id":8141,"date":"2015-01-27T00:01:00","date_gmt":"2015-01-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/01\/27\/working-with-fixed-number-sizes-in-powershell\/"},"modified":"2019-02-18T10:35:46","modified_gmt":"2019-02-18T17:35:46","slug":"working-with-fixed-number-sizes-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/working-with-fixed-number-sizes-in-powershell\/","title":{"rendered":"Working with Fixed Number Sizes in PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about working with fixed number sizes in Windows PowerShell.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I have a script that returns a specific type of number, so I added a type constraint on the variable that holds the number. The problem is that sometimes when I run the script, I get errors. I cannot figure this out. Can you help me?<\/p>\n<p>&mdash;BG<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello BG,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. We have not had any snow this year here in Charlotte, North Carolina. This is good news, and of course, bad news. The good news is that because the city is rarely prepared for snow storms, any snow at all causes major problems. The bad news is that we do not get to see snow very often&mdash;maybe once every three or four years, if we are lucky. Personally, I enjoy looking at snow from the windows, but I really do not like it when I have to get outside in it. Snow causes lots of problems if you are not expecting it, and if you are not prepared for it.<\/p>\n<p>We run into much the same issue when it comes to constraining number types in Windows PowerShell. Because Windows PowerShell has a greatly enhanced adaptive-type system, and it can change the types of the objects that lay under the covers, one rarely needs to deal with things (such as a type mismatch) that plague other object-oriented languages.<\/p>\n<p>This, of course, has good and bad effects. At times, if I am not really paying attention, I can get tripped up if I am not expecting the object transformation. To be honest, this has only happened to me a couple of times, and it was usually because I was trying to do something pretty tricky in the first place. One way around this, is to place a type constraint on the object, and then force it to raise an error message when the change takes place. This can be helpful for spotting these sorts of changes.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> To be clear, most of the time (in fact, I would say over 90 percent of the time), it is best to allow Windows PowerShell to manage the types.<\/p>\n<h2>Changing number types<\/h2>\n<p>The default number type in Windows PowerShell is an <b>Int<\/b>, and that is also an <b>Int32<\/b>. If the number becomes too big, Windows PowerShell will change the number from an <b>Int<\/b> to a <b>Double<\/b>. I wrote the following script to illustrate the point:<\/p>\n<p style=\"margin-left:30px\">function get-double<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Param($a)<\/p>\n<p style=\"margin-left:30px\">&nbsp; $b = $b + $a<\/p>\n<p style=\"margin-left:30px\">&nbsp; &quot;now $($b.gettype())&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp; if ($b -eq &#039;Infinity&#039;) {Return}<\/p>\n<p style=\"margin-left:30px\">&nbsp; get-double $b<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>I load the function and call it by passing the number 2. This is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-Double 2<\/p>\n<p>When I run the script, it runs for a few seconds and then stops when it reaches <b>Infinity<\/b>. Part of the output is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-27-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-27-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>It is clear from the output that when Windows PowerShell reaches the maximum value of <b>Int32<\/b>, it changes the type of number that is contained inside the variable <b>$b<\/b> to a <b>Double<\/b>. Most of the time, this is the behavior I want.<\/p>\n<h3>Constraining the type of the number in a variable<\/h3>\n<p>But suppose that the value of <b>$b<\/b> should never become greater than a certain value, and that number is an <b>Int<\/b>. The script appears to work, but it takes a long time to complete. As I troubleshoot the script, I decide to constrain the value of <b>$b<\/b> to a number type that makes sense for my script. I&#039;m going to try an <b>Int16<\/b>.<\/p>\n<p>In the following script, I use the type constraint <b>[int16]<\/b> to ensure that the variable <b>$b<\/b> only holds an <b>Int16<\/b>.<\/p>\n<p style=\"margin-left:30px\">function get-int<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Param($a)<\/p>\n<p style=\"margin-left:30px\">&nbsp;$erroractionpreference = &quot;stop&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp; [int16]$b = $b + $a<\/p>\n<p style=\"margin-left:30px\">&nbsp; &quot;now $b&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp; get-int $b<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>If the value in <b>$b<\/b> becomes greater than 32768, an error arises. Because the error that arises is not a terminating error, the script would run and run and run, but continue to display errors. That is not the behavior I want, so I changed the value of <b>$errorActionPreference<\/b> from <b>&quot;continue&quot;<\/b> to <b>&quot;stop&quot;<\/b>. The output is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-27-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-27-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>By constraining the type of number that I store in the <b>$b<\/b> variable, it becomes obvious that the problem with my script is that I am, somehow, getting a number that is too big. In reality, a problem such as that is usually traceable to one of the following:<\/p>\n<ul>\n<li>A loop that runs too long (an upper limit is not properly controlling the loop execution)<\/li>\n<li>Variables that are not being properly initialized prior to reuse<\/li>\n<\/ul>\n<p>Because Windows PowerShell has an adaptive-type system, such problems are often difficult to see. They show themselves as weird and inconsistent output, or you may have a script that appears to work, but it takes a really long time to complete. Constraining my types can make troubleshooting easier and enable me to more readily spot such problems.<\/p>\n<p>BG, that is all there is to using numeric type constraints. Numbers Week will continue tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about working with fixed number sizes in Windows PowerShell. &nbsp;Hey, Scripting Guy! I have a script that returns a specific type of number, so I added a type constraint on the variable that holds the number. The problem is that sometimes when I run the script, I get [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,398,3,45],"class_list":["post-8141","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-numbers","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about working with fixed number sizes in Windows PowerShell. &nbsp;Hey, Scripting Guy! I have a script that returns a specific type of number, so I added a type constraint on the variable that holds the number. The problem is that sometimes when I run the script, I get [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=8141"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8141\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=8141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}