BigInteger.DivRem Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
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)
Syntax
'Declaration
Public Shared Function DivRem ( _
dividend As BigInteger, _
divisor As BigInteger, _
<OutAttribute> ByRef remainder As BigInteger _
) As BigInteger
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.
Remarks
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.
Examples
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.
Imports System.Numerics
Module Example
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
Dim divisor As BigInteger = BigInteger.Pow(Int64.MaxValue, 2)
Dim dividends() As BigInteger = { BigInteger.Multiply(CType(Single.MaxValue, BigInteger), 2),
BigInteger.Multiply(UInt64.MaxValue, 4912105071865) + CType(11189198891058195125ul, BigInteger),
BigInteger.One,
BigInteger.Multiply(Int32.MaxValue, Int64.MaxValue),
divisor + BigInteger.One }
' Divide each dividend by divisor in three different ways.
For Each dividend As BigInteger In dividends
Dim quotient As BigInteger
Dim remainder As BigInteger = 0
' Divide using division operator.
outputBlock.Text &= String.Format("Dividend: {0}", dividend) & vbCrLf
outputBlock.Text &= String.Format("Divisor: {0}", divisor) & vbCrLf
outputBlock.Text &= "Results:" & vbCrLf
outputBlock.Text += String.Format(" Using Divide method: {0}",
BigInteger.Divide(dividend, divisor)) + vbCrLf
outputBlock.Text += String.Format(" Using Division operator: {0}",
dividend / divisor) + vbCrLf
quotient = BigInteger.DivRem(dividend, divisor, remainder)
outputBlock.Text += String.Format(" Using DivRem method: {0}, remainder {1}",
quotient, remainder) + vbCrLf
outputBlock.Text &= vbCrLf
Next
End Sub
End Module
' 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
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
Version Information
Silverlight
Supported in: 5, 4
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.