Math.DivRem 方法

定义

计算两个数字的商,并在输出参数中返回余数。

重载

DivRem(IntPtr, IntPtr)

生成两个已签名本机大小的数字的商和余数。

DivRem(Int64, Int64, Int64)

计算两个 64 位有符号整数的商,并通过输出参数返回余数。

DivRem(Int32, Int32, Int32)

计算两个 32 位有符号整数的商,并通过输出参数返回余数。

DivRem(UIntPtr, UIntPtr)

生成两个无符号本机大小的数字的商和余数。

DivRem(UInt64, UInt64)

生成两个无符号 64 位数字的商和余数。

DivRem(UInt32, UInt32)

生成两个无符号 32 位数字的商和余数。

DivRem(Int64, Int64)

生成两个 64 位有签名数字的商和余数。

DivRem(SByte, SByte)

生成两个 8 位有签名数字的商和余数。

DivRem(Int32, Int32)

生成两个 32 位有签名数字的商和余数。

DivRem(Int16, Int16)

生成两个 16 位有签名数字的商和余数。

DivRem(Byte, Byte)

生成两个无符号 8 位数字的商和余数。

DivRem(UInt16, UInt16)

生成两个无符号 16 位数字的商和余数。

DivRem(IntPtr, IntPtr)

生成两个已签名本机大小的数字的商和余数。

public:
 static ValueTuple<IntPtr, IntPtr> DivRem(IntPtr left, IntPtr right);
public static (IntPtr,IntPtr) DivRem (IntPtr left, IntPtr right);
static member DivRem : nativeint * nativeint -> ValueTuple<nativeint, nativeint>
Public Shared Function DivRem (left As IntPtr, right As IntPtr) As ValueTuple(Of IntPtr, IntPtr)

参数

left
IntPtr

nativeint

被除数。

right
IntPtr

nativeint

除数。

返回

ValueTuple<IntPtr,IntPtr>

ValueTuple<nativeint,nativeint>

指定数字的商和余数。

适用于

DivRem(Int64, Int64, Int64)

计算两个 64 位有符号整数的商,并通过输出参数返回余数。

public:
 static long DivRem(long a, long b, [Runtime::InteropServices::Out] long % result);
public static long DivRem (long a, long b, out long result);
static member DivRem : int64 * int64 * int64 -> int64
Public Shared Function DivRem (a As Long, b As Long, ByRef result As Long) As Long

参数

a
Int64

被除数。

b
Int64

除数。

result
Int64

此方法返回时,包含余数。

返回

Int64

指定数字的商。

例外

b 为零。

示例

下面的示例演示 DivRem(Int64, Int64, Int64) 方法。

using System;

public class Example
{
   public static void Main()
   {
      // Define several positive and negative dividends.
      long[] dividends = { Int64.MaxValue, 13952, 0, -14032,
                           Int64.MinValue };
      // Define one positive and one negative divisor.
      long[] divisors = { 2000, -2000 };

      foreach (long divisor in divisors)
      {
         foreach (long dividend in dividends)
         {
            long remainder;
            long quotient = Math.DivRem(dividend, divisor, out remainder);
            Console.WriteLine(@"{0:N0} \ {1:N0} = {2:N0}, remainder {3:N0}",
                              dividend, divisor, quotient, remainder);
         }
      }
   }
}
// The example displays the following output:
//    9,223,372,036,854,775,807 \ 2,000 = 4,611,686,018,427,387, remainder 1,807
//    13,952 \ 2,000 = 6, remainder 1,952
//    0 \ 2,000 = 0, remainder 0
//    -14,032 \ 2,000 = -7, remainder -32
//    -9,223,372,036,854,775,808 \ 2,000 = -4,611,686,018,427,387, remainder -1,808
//    9,223,372,036,854,775,807 \ -2,000 = -4,611,686,018,427,387, remainder 1,807
//    13,952 \ -2,000 = -6, remainder 1,952
//    0 \ -2,000 = 0, remainder 0
//    -14,032 \ -2,000 = 7, remainder -32
//    -9,223,372,036,854,775,808 \ -2,000 = 4,611,686,018,427,387, remainder -1,808
Module Example
   Public Sub Main()
      ' Define several positive and negative dividends.
      Dim dividends() As Long = { Int64.MaxValue, 13952, 0, -14032, _
                                     Int64.MinValue }
      ' Define one positive and one negative divisor.
      Dim divisors() As Long = { 2000, -2000 }
      
      For Each divisor As Long In divisors
         For Each dividend As Long In dividends
            Dim remainder As Long 
            Dim quotient As Long = Math.DivRem(dividend, divisor, remainder)
            Console.WriteLine("{0:N0} \ {1:N0} = {2:N0}, remainder {3:N0}", _
                              dividend, divisor, quotient, remainder)
         Next
      Next                                
   End Sub
End Module
' The example displays the following output:
'    9,223,372,036,854,775,807 \ 2,000 = 4,611,686,018,427,387, remainder 1,807
'    13,952 \ 2,000 = 6, remainder 1,952
'    0 \ 2,000 = 0, remainder 0
'    -14,032 \ 2,000 = -7, remainder -32
'    -9,223,372,036,854,775,808 \ 2,000 = -4,611,686,018,427,387, remainder -1,808
'    9,223,372,036,854,775,807 \ -2,000 = -4,611,686,018,427,387, remainder 1,807
'    13,952 \ -2,000 = -6, remainder 1,952
'    0 \ -2,000 = 0, remainder 0
'    -14,032 \ -2,000 = 7, remainder -32
'    -9,223,372,036,854,775,808 \ -2,000 = 4,611,686,018,427,387, remainder -1,808

注解

余数值等于余数运算符 的结果

另请参阅

适用于

DivRem(Int32, Int32, Int32)

计算两个 32 位有符号整数的商,并通过输出参数返回余数。

public:
 static int DivRem(int a, int b, [Runtime::InteropServices::Out] int % result);
public static int DivRem (int a, int b, out int result);
static member DivRem : int * int * int -> int
Public Shared Function DivRem (a As Integer, b As Integer, ByRef result As Integer) As Integer

参数

a
Int32

被除数。

b
Int32

除数。

result
Int32

此方法返回时,包含余数。

返回

Int32

指定数字的商。

例外

b 为零。

示例

下面的示例演示 DivRem(Int32, Int32, Int32) 方法。

using System;

public class Example
{
   public static void Main()
   {
      // Define several positive and negative dividends.
      int[] dividends = { Int32.MaxValue, 13952, 0, -14032,
                                     Int32.MinValue };
      // Define one positive and one negative divisor.
      int[] divisors = { 2000, -2000 };

      foreach (int divisor in divisors)
      {
         foreach (int dividend in dividends)
         {
            int remainder;
            int quotient = Math.DivRem(dividend, divisor, out remainder);
            Console.WriteLine(@"{0:N0} \ {1:N0} = {2:N0}, remainder {3:N0}",
                              dividend, divisor, quotient, remainder);
         }
      }
   }
}
// The example displays the following output:
//       2,147,483,647 \ 2,000 = 1,073,741, remainder 1,647
//       13,952 \ 2,000 = 6, remainder 1,952
//       0 \ 2,000 = 0, remainder 0
//       -14,032 \ 2,000 = -7, remainder -32
//       -2,147,483,648 \ 2,000 = -1,073,741, remainder -1,648
//       2,147,483,647 \ -2,000 = -1,073,741, remainder 1,647
//       13,952 \ -2,000 = -6, remainder 1,952
//       0 \ -2,000 = 0, remainder 0
//       -14,032 \ -2,000 = 7, remainder -32
//       -2,147,483,648 \ -2,000 = 1,073,741, remainder -1,648
Module Example
   Public Sub Main()
      ' Define several positive and negative dividends.
      Dim dividends() As Integer = { Int32.MaxValue, 13952, 0, -14032, _
                                     Int32.MinValue }
      ' Define one positive and one negative divisor.
      Dim divisors() As Integer = { 2000, -2000 }
      
      For Each divisor As Integer In divisors
         For Each dividend As Integer In dividends
            Dim remainder As Integer 
            Dim quotient As Integer = Math.DivRem(dividend, divisor, remainder)
            Console.WriteLine("{0:N0} \ {1:N0} = {2:N0}, remainder {3:N0}", _
                              dividend, divisor, quotient, remainder)
         Next
      Next                                
   End Sub
End Module
' The example displays the following output:
'       2,147,483,647 \ 2,000 = 1,073,741, remainder 1,647
'       13,952 \ 2,000 = 6, remainder 1,952
'       0 \ 2,000 = 0, remainder 0
'       -14,032 \ 2,000 = -7, remainder -32
'       -2,147,483,648 \ 2,000 = -1,073,741, remainder -1,648
'       2,147,483,647 \ -2,000 = -1,073,741, remainder 1,647
'       13,952 \ -2,000 = -6, remainder 1,952
'       0 \ -2,000 = 0, remainder 0
'       -14,032 \ -2,000 = 7, remainder -32
'       -2,147,483,648 \ -2,000 = 1,073,741, remainder -1,648

注解

余数值等于余数运算符 的结果

另请参阅

适用于

DivRem(UIntPtr, UIntPtr)

重要

此 API 不符合 CLS。

生成两个无符号本机大小的数字的商和余数。

public:
 static ValueTuple<UIntPtr, UIntPtr> DivRem(UIntPtr left, UIntPtr right);
[System.CLSCompliant(false)]
public static (UIntPtr,UIntPtr) DivRem (UIntPtr left, UIntPtr right);
public static (UIntPtr,UIntPtr) DivRem (UIntPtr left, UIntPtr right);
[<System.CLSCompliant(false)>]
static member DivRem : unativeint * unativeint -> ValueTuple<unativeint, unativeint>
static member DivRem : unativeint * unativeint -> ValueTuple<unativeint, unativeint>
Public Shared Function DivRem (left As UIntPtr, right As UIntPtr) As ValueTuple(Of UIntPtr, UIntPtr)

参数

left
UIntPtr

unativeint

被除数。

right
UIntPtr

unativeint

除数。

返回

ValueTuple<UIntPtr,UIntPtr>

ValueTuple<unativeint,unativeint>

指定数字的商和余数。

属性

适用于

DivRem(UInt64, UInt64)

重要

此 API 不符合 CLS。

生成两个无符号 64 位数字的商和余数。

public:
 static ValueTuple<System::UInt64, System::UInt64> DivRem(System::UInt64 left, System::UInt64 right);
[System.CLSCompliant(false)]
public static (ulong,ulong) DivRem (ulong left, ulong right);
public static (ulong,ulong) DivRem (ulong left, ulong right);
[<System.CLSCompliant(false)>]
static member DivRem : uint64 * uint64 -> ValueTuple<uint64, uint64>
static member DivRem : uint64 * uint64 -> ValueTuple<uint64, uint64>
Public Shared Function DivRem (left As ULong, right As ULong) As ValueTuple(Of ULong, ULong)

参数

left
UInt64

被除数。

right
UInt64

除数。

返回

ValueTuple<UInt64,UInt64>

指定数字的商和余数。

属性

适用于

DivRem(UInt32, UInt32)

重要

此 API 不符合 CLS。

生成两个无符号 32 位数字的商和余数。

public:
 static ValueTuple<System::UInt32, System::UInt32> DivRem(System::UInt32 left, System::UInt32 right);
[System.CLSCompliant(false)]
public static (uint,uint) DivRem (uint left, uint right);
public static (uint,uint) DivRem (uint left, uint right);
[<System.CLSCompliant(false)>]
static member DivRem : uint32 * uint32 -> ValueTuple<uint32, uint32>
static member DivRem : uint32 * uint32 -> ValueTuple<uint32, uint32>
Public Shared Function DivRem (left As UInteger, right As UInteger) As ValueTuple(Of UInteger, UInteger)

参数

left
UInt32

被除数。

right
UInt32

除数。

返回

ValueTuple<UInt32,UInt32>

指定数字的商和余数。

属性

适用于

DivRem(Int64, Int64)

生成两个 64 位有签名数字的商和余数。

public:
 static ValueTuple<long, long> DivRem(long left, long right);
public static (long,long) DivRem (long left, long right);
static member DivRem : int64 * int64 -> ValueTuple<int64, int64>
Public Shared Function DivRem (left As Long, right As Long) As ValueTuple(Of Long, Long)

参数

left
Int64

被除数。

right
Int64

除数。

返回

ValueTuple<Int64,Int64>

指定数字的商和余数。

适用于

DivRem(SByte, SByte)

重要

此 API 不符合 CLS。

生成两个 8 位有签名数字的商和余数。

public:
 static ValueTuple<System::SByte, System::SByte> DivRem(System::SByte left, System::SByte right);
[System.CLSCompliant(false)]
public static (sbyte,sbyte) DivRem (sbyte left, sbyte right);
public static (sbyte,sbyte) DivRem (sbyte left, sbyte right);
[<System.CLSCompliant(false)>]
static member DivRem : sbyte * sbyte -> ValueTuple<sbyte, sbyte>
static member DivRem : sbyte * sbyte -> ValueTuple<sbyte, sbyte>
Public Shared Function DivRem (left As SByte, right As SByte) As ValueTuple(Of SByte, SByte)

参数

left
SByte

被除数。

right
SByte

除数。

返回

ValueTuple<SByte,SByte>

指定数字的商和余数。

属性

适用于

DivRem(Int32, Int32)

生成两个 32 位有签名数字的商和余数。

public:
 static ValueTuple<int, int> DivRem(int left, int right);
public static (int,int) DivRem (int left, int right);
static member DivRem : int * int -> ValueTuple<int, int>
Public Shared Function DivRem (left As Integer, right As Integer) As ValueTuple(Of Integer, Integer)

参数

left
Int32

被除数。

right
Int32

除数。

返回

ValueTuple<Int32,Int32>

指定数字的商和余数。

适用于

DivRem(Int16, Int16)

生成两个 16 位有签名数字的商和余数。

public:
 static ValueTuple<short, short> DivRem(short left, short right);
public static (short,short) DivRem (short left, short right);
static member DivRem : int16 * int16 -> ValueTuple<int16, int16>
Public Shared Function DivRem (left As Short, right As Short) As ValueTuple(Of Short, Short)

参数

left
Int16

被除数。

right
Int16

除数。

返回

ValueTuple<Int16,Int16>

指定数字的商和余数。

适用于

DivRem(Byte, Byte)

生成两个无符号 8 位数字的商和余数。

public:
 static ValueTuple<System::Byte, System::Byte> DivRem(System::Byte left, System::Byte right);
public static (byte,byte) DivRem (byte left, byte right);
static member DivRem : byte * byte -> ValueTuple<byte, byte>
Public Shared Function DivRem (left As Byte, right As Byte) As ValueTuple(Of Byte, Byte)

参数

left
Byte

被除数。

right
Byte

除数。

返回

ValueTuple<Byte,Byte>

指定数字的商和余数。

适用于

DivRem(UInt16, UInt16)

重要

此 API 不符合 CLS。

生成两个无符号 16 位数字的商和余数。

public:
 static ValueTuple<System::UInt16, System::UInt16> DivRem(System::UInt16 left, System::UInt16 right);
[System.CLSCompliant(false)]
public static (ushort,ushort) DivRem (ushort left, ushort right);
public static (ushort,ushort) DivRem (ushort left, ushort right);
[<System.CLSCompliant(false)>]
static member DivRem : uint16 * uint16 -> ValueTuple<uint16, uint16>
static member DivRem : uint16 * uint16 -> ValueTuple<uint16, uint16>
Public Shared Function DivRem (left As UShort, right As UShort) As ValueTuple(Of UShort, UShort)

参数

left
UInt16

被除数。

right
UInt16

除数。

返回

ValueTuple<UInt16,UInt16>

指定数字的商和余数。

属性

适用于