Monday, 9 August 2010

Divide-BigInteger.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script divides big integers using .NET Framework. 
  4. .DESCRIPTION 
  5.     This script reimplements an MSDN Sample script using powershell. The 
  6.     script creates a dividor and an arry of dividends, then performs  
  7.     division operations several ways. 
  8. .NOTES 
  9.     File Name  : Divide-BigInteger.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12.                  .NET Framework 4.0 or higher 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.divide.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: . 'C:\Users\tfl\AppData\Local\Temp\Untitled7.ps1' 
  20.     Dividend: 680,564,693,277,057,719,623,408,366,969,033,850,880 
  21.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  22.     Results: 
  23.        Using Divide method:     7 
  24.        Using Division operator: 7 
  25.        Using DivRem method:     7, remainder 85,070,551,165,415,408,691,630,012,479,406,342,137 
  26.  
  27.     Dividend: 90,612,345,123,875,509,091,827,560,007,100,099 
  28.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  29.     Results: 
  30.        Using Divide method:     0 
  31.        Using Division operator: 0 
  32.        Using DivRem method:     0, remainder 90,612,345,123,875,509,091,827,560,007,100,099 
  33.      
  34.     Dividend: 1 
  35.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  36.     Results: 
  37.        Using Divide method:     0 
  38.        Using Division operator: 0 
  39.        Using DivRem method:     0, remainder 1 
  40.      
  41.     Dividend: 19,807,040,619,342,712,359,383,728,129 
  42.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  43.     Results: 
  44.        Using Divide method:     0 
  45.        Using Division operator: 0 
  46.        Using DivRem method:     0, remainder 19,807,040,619,342,712,359,383,728,129 
  47.   
  48.     Dividend: 85,070,591,730,234,615,847,396,907,784,232,501,250 
  49.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  50.     Results:  
  51.        Using Divide method:     1 
  52.        Using Division operator: 1 
  53.        Using DivRem method:     1, remainder 1 
  54.   
  55. #> 
  56.  
  57. # Add namespace 
  58. $r = [system.Reflection.Assembly]::LoadWithPartialName("System.Numerics"
  59.  
  60. #Create a big integer divisor and an array of dividends 
  61. $divisor = [system.numerics.BigInteger]::pow([Int64]::MaxValue, 2) 
  62. $dividends = @() 
  63. $dividends += [system.numerics.BigInteger]::Multiply(([system.numerics.BigInteger] [system.Single]::MaxValue), 2) 
  64. $dividends += [system.numerics.BigInteger]::Parse("90612345123875509091827560007100099")  
  65. $dividends += [system.numerics.BigInteger]::One 
  66. $dividends += [system.numerics.BigInteger]::Multiply([Int32]::MaxValue, [Int64]::MaxValue) 
  67. $dividends += $divisor + [system.numerics.BigInteger]::One  
  68.   
  69. #Divide each dividend by divisor in three different ways 
  70. foreach ($dividend in $dividends) { 
  71.     "Dividend: {0:N0}" -f $dividend 
  72.     "Divisor:  {0:N0}" -f $divisor 
  73.     "Results:" 
  74.     "   Using Divide method:     {0:N0}" -f  [system.Numerics.BigInteger]::Divide($dividend, $divisor
  75.     "   Using Division operator: {0:N0}" -f ($dividend/$divisor
  76.     $remainder = [system.Numerics.BigInteger]::Zero 
  77.     $quotient = [system.numerics.BigInteger]::DivRem($dividend, $divisor, [ref] $remainder
  78.     "   Using DivRem method:     {0:N0}, remainder {1:N0}" -f $quotient, $remainder 
  79.     "" 
  80. }         

No comments: