Thursday, February 16, 2017

Arbitrary precision integers in PowerShell

If for some reason you find yourself in need of facilities for handling extremely large numbers with arbitrary precision in PowerShell, you can take advantage of the BigInteger type. First you'll need to load the assembly that contains it:

Add-Type -AssemblyName System.Numerics

You can then refer to the type as bigint. Operators on it work as you expect. One thing to note is that it does not have a constructor that takes a string. Rather, you need to use the static Parse method if you want to hold an arbitrarily large number without losing precision. If you tried to use a constructor with a string, the string would be converted to one of the normal numeric types, losing precision before bigint comes into play at all.

No comments:

Post a Comment