Export (0) Print
Expand All

BigInteger.DivRem Method

Divides one BigInteger value by another, returns the result, and returns the remainder in an output parameter.

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

public static BigInteger DivRem(
	BigInteger dividend,
	BigInteger divisor,
	out BigInteger remainder
)

Parameters

dividend
Type: System.Numerics.BigInteger
The value to be divided.
divisor
Type: System.Numerics.BigInteger
The value to divide by.
remainder
Type: System.Numerics.BigInteger%
When this method returns, contains a BigInteger value that represents the remainder from the division. This parameter is passed uninitialized.

Return Value

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

ExceptionCondition
DivideByZeroException

divisor is 0 (zero).

This method preserves both the quotient and the remainder that results from integer division. If you are not interested in the remainder, use the Divide method or the division operator; if you are only interested in the remainder, use the Remainder method.

The sign of the returned remainder value is the same as the sign of the dividend parameter.

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:
© 2015 Microsoft