Math.DivRem メソッド
定義
2 つの数値の商を計算し、出力パラメーターの剰余を返します。Calculates the quotient of two numbers and also returns the remainder in an output parameter.
オーバーロード
DivRem(Int32, Int32, Int32) |
2 つの 32 ビット符号付き整数の商を計算し、出力パラメーターの剰余を返します。Calculates the quotient of two 32-bit signed integers and also returns the remainder in an output parameter. |
DivRem(Int64, Int64, Int64) |
2 つの 64 ビット符号付き整数の商を計算し、出力パラメーターの剰余を返します。Calculates the quotient of two 64-bit signed integers and also returns the remainder in an output parameter. |
DivRem(Int32, Int32, Int32)
2 つの 32 ビット符号付き整数の商を計算し、出力パラメーターの剰余を返します。Calculates the quotient of two 32-bit signed integers and also returns the remainder in an output parameter.
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
被除数。The dividend.
- b
- Int32
除数。The divisor.
- result
- Int32
剰余。The remainder.
戻り値
指定した数値の商。The quotient of the specified numbers.
例外
b
が 0 です。b
is zero.
例
DivRem(Int32, Int32, Int32)メソッドの例を次に示します。The following example demonstrates the DivRem(Int32, Int32, Int32) method.
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
注釈
剰余の値は、 剰余演算子の結果と等しくなります。The remainder value equals the result of the remainder operator.
こちらもご覧ください
適用対象
DivRem(Int64, Int64, Int64)
2 つの 64 ビット符号付き整数の商を計算し、出力パラメーターの剰余を返します。Calculates the quotient of two 64-bit signed integers and also returns the remainder in an output parameter.
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
被除数。The dividend.
- b
- Int64
除数。The divisor.
- result
- Int64
剰余。The remainder.
戻り値
指定した数値の商。The quotient of the specified numbers.
例外
b
が 0 です。b
is zero.
例
DivRem(Int64, Int64, Int64)メソッドの例を次に示します。The following example demonstrates the DivRem(Int64, Int64, Int64) method.
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
注釈
剰余の値は、 剰余演算子の結果と等しくなります。The remainder value equals the result of the remainder operator.