Export (0) Print
Expand All

BigInteger.Divide Method

Divides one BigInteger value by another and returns the result.

Namespace:  System.Numerics
Assembly:  System.Numerics (in System.Numerics.dll)

public static BigInteger Divide(
	BigInteger dividend,
	BigInteger divisor
)

Parameters

dividend
Type: System.Numerics.BigInteger
The value to be divided.
divisor
Type: System.Numerics.BigInteger
The value to divide by.

Return Value

Type: System.Numerics.BigInteger
The quotient of the division.

ExceptionCondition
DivideByZeroException

divisor is 0 (zero).

The Divide method performs integer division; any remainder that results from the division is discarded. To perform integer division while preserving the remainder, call the DivRem method. To retrieve only the remainder, call the Remainder method.

The Divide method can be used by languages that do not support operator overloading. Its behavior is identical to division using the division operator.

The following example creates an array of BigInteger values. It then uses each element as the quotient in a division operation that uses the Divide method, the division operator (/), and the DivRem method.


using System;
using System.Numerics;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      BigInteger divisor = BigInteger.Pow(Int64.MaxValue, 2);

      BigInteger[] dividends = { BigInteger.Multiply((BigInteger) Single.MaxValue, 2), 
                                 ((BigInteger) UInt64.MaxValue) * 4912105071865 + 11189198891058195125, 
                                 BigInteger.One, 
                                 BigInteger.Multiply(Int32.MaxValue, Int64.MaxValue),
                                 divisor + BigInteger.One };

      // Divide each dividend by divisor in three different ways.
      foreach (BigInteger dividend in dividends)
      {
         BigInteger quotient;
         BigInteger remainder = 0;

         outputBlock.Text += String.Format("Dividend: {0}\n", dividend);
         outputBlock.Text += String.Format("Divisor:  {0}\n", divisor);
         outputBlock.Text += "Results:\n";
         outputBlock.Text += String.Format("   Using Divide method:     {0}\n",
                           BigInteger.Divide(dividend, divisor));
         outputBlock.Text += String.Format("   Using Division operator: {0}\n",
                           dividend / divisor);
         quotient = BigInteger.DivRem(dividend, divisor, out remainder);
         outputBlock.Text += String.Format("   Using DivRem method:     {0}, remainder {1}\n",
                           quotient, remainder);

         outputBlock.Text += "\n";
      }
   }
}
// The example displays the following output:
//    Dividend: 680564693277057719623408366969033850880
//    Divisor:  85070591730234615847396907784232501249
//    Results:
//       Using Divide method:     7
//       Using Division operator: 7
//       Using DivRem method:     7, remainder 85070551165415408691630012479406342137
//    
//    Dividend: 90612345123875509091827560007100099
//    Divisor:  85070591730234615847396907784232501249
//    Results:
//       Using Divide method:     0
//       Using Division operator: 0
//       Using DivRem method:     0, remainder 90612345123875509091827560007100099
//    
//    Dividend: 1
//    Divisor:  85070591730234615847396907784232501249
//    Results:
//       Using Divide method:     0
//       Using Division operator: 0
//       Using DivRem method:     0, remainder 1
//    
//    Dividend: 19807040619342712359383728129
//    Divisor:  85070591730234615847396907784232501249
//    Results:
//       Using Divide method:     0
//       Using Division operator: 0
//       Using DivRem method:     0, remainder 19807040619342712359383728129
//    
//    Dividend: 85070591730234615847396907784232501250
//    Divisor:  85070591730234615847396907784232501249
//    Results:
//       Using Divide method:     1
//       Using Division operator: 1
//       Using DivRem method:     1, remainder 1


Silverlight

Supported in: 5, 4

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft