BigInteger.DivRem 方法

定義

多載

DivRem(BigInteger, BigInteger)

計算兩個值的商數和餘數。

DivRem(BigInteger, BigInteger, BigInteger)

某個 BigInteger 值除以另一個值,傳回結果,並在輸出參數中傳回餘數。

DivRem(BigInteger, BigInteger)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

計算兩個值的商數和餘數。

C#
public static (System.Numerics.BigInteger Quotient, System.Numerics.BigInteger Remainder) DivRem (System.Numerics.BigInteger left, System.Numerics.BigInteger right);

參數

left
BigInteger

除法的值 right

right
BigInteger

除以 left的值。

傳回

除以 leftright的商數和餘數。

實作

適用於

.NET 9 及其他版本
產品 版本
.NET 7, 8, 9

DivRem(BigInteger, BigInteger, BigInteger)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

某個 BigInteger 值除以另一個值,傳回結果,並在輸出參數中傳回餘數。

C#
public static System.Numerics.BigInteger DivRem (System.Numerics.BigInteger dividend, System.Numerics.BigInteger divisor, out System.Numerics.BigInteger remainder);

參數

dividend
BigInteger

做為被除數的值。

divisor
BigInteger

做為除數的值。

remainder
BigInteger

當這個方法傳回時,會包含表示相除餘數的 BigInteger 值。 這個參數會以未初始化的狀態傳遞。

傳回

相除的商數。

例外狀況

divisor 是 0 (零)。

範例

下列範例會建立值的陣列 BigInteger 。 然後它會使用每個元素做為使用 Divide 方法的除法運算中的商數、除法運算元 (/) ,以及 DivRem 方法。

C#
using System;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      BigInteger divisor = BigInteger.Pow(Int64.MaxValue, 2);

      BigInteger[] dividends = { BigInteger.Multiply((BigInteger) Single.MaxValue, 2),
                                 BigInteger.Parse("90612345123875509091827560007100099"),
                                 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;

         Console.WriteLine("Dividend: {0:N0}", dividend);
         Console.WriteLine("Divisor:  {0:N0}", divisor);
         Console.WriteLine("Results:");
         Console.WriteLine("   Using Divide method:     {0:N0}",
                           BigInteger.Divide(dividend, divisor));
         Console.WriteLine("   Using Division operator: {0:N0}",
                           dividend / divisor);
         quotient = BigInteger.DivRem(dividend, divisor, out remainder);
         Console.WriteLine("   Using DivRem method:     {0:N0}, remainder {1:N0}",
                           quotient, remainder);

         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//    Dividend: 680,564,693,277,057,719,623,408,366,969,033,850,880
//    Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249
//    Results:
//       Using Divide method:     7
//       Using Division operator: 7
//       Using DivRem method:     7, remainder 85,070,551,165,415,408,691,630,012,479,406,342,137
//
//    Dividend: 90,612,345,123,875,509,091,827,560,007,100,099
//    Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249
//    Results:
//       Using Divide method:     0
//       Using Division operator: 0
//       Using DivRem method:     0, remainder 90,612,345,123,875,509,091,827,560,007,100,099
//
//    Dividend: 1
//    Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249
//    Results:
//       Using Divide method:     0
//       Using Division operator: 0
//       Using DivRem method:     0, remainder 1
//
//    Dividend: 19,807,040,619,342,712,359,383,728,129
//    Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249
//    Results:
//       Using Divide method:     0
//       Using Division operator: 0
//       Using DivRem method:     0, remainder 19,807,040,619,342,712,359,383,728,129
//
//    Dividend: 85,070,591,730,234,615,847,396,907,784,232,501,250
//    Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249
//    Results:
//       Using Divide method:     1
//       Using Division operator: 1
//       Using DivRem method:     1, remainder 1

備註

這個方法會保留整數除法所產生的商數和餘數。 如果您對餘數不感興趣,請使用 Divide 方法或除法運算符;如果您只對餘數感興趣,請使用 Remainder 方法。

remainder 回值的正負號與 參數的 dividend 正負號相同。

方法的行為 DivRemMath.DivRem 方法的行為相同。

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0