BigInteger コンストラクター
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
BigInteger構造体の新しいインスタンスを初期化します。
オーバーロード
| 名前 | 説明 |
|---|---|
| BigInteger(Byte[]) |
バイト配列内の値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。 |
| BigInteger(Decimal) |
Decimal値を使用して、BigInteger構造体の新しいインスタンスを初期化します。 |
| BigInteger(Double) |
倍精度浮動小数点値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。 |
| BigInteger(Int32) |
32 ビット符号付き整数値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。 |
| BigInteger(Int64) |
64 ビット符号付き整数値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。 |
| BigInteger(Single) |
単精度浮動小数点値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。 |
| BigInteger(UInt32) |
符号なし 32 ビット整数値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。 |
| BigInteger(UInt64) |
符号なし 64 ビット整数値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。 |
| BigInteger(ReadOnlySpan<Byte>, Boolean, Boolean) |
バイトの読み取り専用スパンの値を使用し、必要に応じて署名エンコードとエンディアンバイトオーダーを示して、 BigInteger 構造体の新しいインスタンスを初期化します。 |
BigInteger(Byte[])
重要
この API は CLS 準拠ではありません。
バイト配列内の値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。
public:
BigInteger(cli::array <System::Byte> ^ value);
[System.CLSCompliant(false)]
public BigInteger(byte[] value);
[<System.CLSCompliant(false)>]
new System.Numerics.BigInteger : byte[] -> System.Numerics.BigInteger
Public Sub New (value As Byte())
パラメーター
- value
- Byte[]
リトル エンディアン順のバイト値の配列。
- 属性
例外
value は nullです。
例
次の例では、値が {5、4、3、2、1} の 5 要素バイト配列から BigInteger オブジェクトをインスタンス化します。 その後、10 進数と 16 進数の両方で表される BigInteger 値がコンソールに表示されます。 入力配列とテキスト出力を比較すると、 BigInteger クラス コンストラクターのこのオーバーロードによって、値が4328719365 (または0x102030405) である BigInteger オブジェクトが作成される理由が明確になります。 値が 5 であるバイト配列の最初の要素は、 BigInteger オブジェクトの最下位バイトの値を定義します。これは0x05。 バイト配列の 2 番目の要素 (値が 4) は、 BigInteger オブジェクトの 2 番目のバイト (0x04など) の値を定義します。
byte[] bytes = { 5, 4, 3, 2, 1 };
BigInteger number = new BigInteger(bytes);
Console.WriteLine("The value of number is {0} (or 0x{0:x}).", number);
// The example displays the following output:
// The value of number is 4328719365 (or 0x102030405).
let bytes = [| 5uy; 4uy; 3uy; 2uy; 1uy |]
let number = new BigInteger(bytes)
printfn $"The value of number is {number} (or 0x{number:x})."
// The example displays the following output:
// The value of number is 4328719365 (or 0x102030405).
Dim bytes() As Byte = { 5, 4, 3, 2, 1 }
Dim number As New BigInteger(bytes)
Console.WriteLine("The value of number is {0} (or 0x{0:x}).", number)
' The example displays the following output:
' The value of number is 4328719365 (or 0x102030405).
次の例では、正の値と負の BigInteger 値をインスタンス化し、 ToByteArray メソッドに渡し、結果のバイト配列から元の BigInteger 値を復元します。 2 つの値は同じバイト配列で表されることに注意してください。 それらの唯一の違いは、バイト配列の最後の要素の最上位ビットにあります。 配列が負の BigInteger 値から作成された場合、このビットが設定されます (バイトの値は0xFF)。 配列が正の BigInteger 値から作成された場合、ビットは設定されません (バイトの値は 0 です)。
// Instantiate BigInteger values.
BigInteger positiveValue = BigInteger.Parse("4713143110832790377889");
BigInteger negativeValue = BigInteger.Add(-Int64.MaxValue, -60000);
BigInteger positiveValue2, negativeValue2;
// Create two byte arrays.
byte[] positiveBytes = positiveValue.ToByteArray();
byte[] negativeBytes = negativeValue.ToByteArray();
// Instantiate new BigInteger from negativeBytes array.
Console.Write("Converted {0:N0} to the byte array ", negativeValue);
foreach (byte byteValue in negativeBytes)
Console.Write("{0:X2} ", byteValue);
Console.WriteLine();
negativeValue2 = new BigInteger(negativeBytes);
Console.WriteLine("Converted the byte array to {0:N0}", negativeValue2);
Console.WriteLine();
// Instantiate new BigInteger from positiveBytes array.
Console.Write("Converted {0:N0} to the byte array ", positiveValue);
foreach (byte byteValue in positiveBytes)
Console.Write("{0:X2} ", byteValue);
Console.WriteLine();
positiveValue2 = new BigInteger(positiveBytes);
Console.WriteLine("Converted the byte array to {0:N0}", positiveValue2);
Console.WriteLine();
// The example displays the following output:
// Converted -9,223,372,036,854,835,807 to the byte array A1 15 FF FF FF FF FF 7F FF
// Converted the byte array to -9,223,372,036,854,835,807
//
// Converted 4,713,143,110,832,790,377,889 to the byte array A1 15 FF FF FF FF FF 7F FF 00
// Converted the byte array to 4,713,143,110,832,790,377,889
// Instantiate BigInteger values.
let positiveValue = BigInteger.Parse "4713143110832790377889"
let negativeValue = BigInteger.Add(-Int64.MaxValue, -60000)
// Create two byte arrays.
let positiveBytes = positiveValue.ToByteArray()
let negativeBytes = negativeValue.ToByteArray()
// Instantiate new BigInteger from negativeBytes array.
printf $"Converted {negativeValue:N0} to the byte array "
for byteValue in negativeBytes do
printf $"0x{byteValue:x2} "
printfn ""
let negativeValue2 = bigint negativeBytes
printfn $"Converted the byte array to {negativeValue2:N0}"
printfn ""
// Instantiate new BigInteger from positiveBytes array.
printf $"Converted {positiveValue:N0} to the byte array "
for byteValue in positiveBytes do
printf $"0x{byteValue:x2} "
printfn ""
let positiveValue2 = new BigInteger(positiveBytes)
printfn $"Converted the byte array to {positiveValue2:N0}"
printfn ""
// The example displays the following output:
// Converted -9,223,372,036,854,835,807 to the byte array A1 15 FF FF FF FF FF 7F FF
// Converted the byte array to -9,223,372,036,854,835,807
//
// Converted 4,713,143,110,832,790,377,889 to the byte array A1 15 FF FF FF FF FF 7F FF 00
// Converted the byte array to 4,713,143,110,832,790,377,889
' Instantiate BigInteger values.
Dim positiveValue As BigInteger = BigInteger.Parse("4713143110832790377889")
Dim negativeValue As BigInteger = BigInteger.Add(-Int64.MaxValue, -60000)
Dim positiveValue2, negativeValue2 As BigInteger
' Create two byte arrays.
Dim positiveBytes() As Byte = positiveValue.ToByteArray()
Dim negativeBytes() As Byte = negativeValue.ToByteArray()
' Instantiate new BigInteger from negativeBytes array.
Console.Write("Converted {0:N0} to the byte array ", negativeValue)
For Each byteValue As Byte In negativeBytes
Console.Write("{0:X2} ", byteValue)
Next
Console.WriteLine()
negativeValue2 = New BigInteger(negativeBytes)
Console.WriteLine("Converted the byte array to {0:N0}", negativeValue2)
Console.WriteLine()
' Instantiate new BigInteger from positiveBytes array.
Console.Write("Converted {0:N0} to the byte array ", positiveValue)
For Each byteValue As Byte In positiveBytes
Console.Write("{0:X2} ", byteValue)
Next
Console.WriteLine()
positiveValue2 = New BigInteger(positiveBytes)
Console.WriteLine("Converted the byte array to {0:N0}", positiveValue2)
Console.WriteLine()
' The example displays the following output:
' Converted -9,223,372,036,854,835,807 to the byte array A1 15 FF FF FF FF FF 7F FF
' Converted the byte array to -9,223,372,036,854,835,807
'
' Converted 4,713,143,110,832,790,377,889 to the byte array A1 15 FF FF FF FF FF 7F FF 00
' Converted the byte array to 4,713,143,110,832,790,377,889
次の例では、配列の末尾に値が 0 のバイトを追加することで、正の値が正の値が正しく負の値としてインスタンス化されないようにする方法を示します。
ulong originalNumber = UInt64.MaxValue;
byte[] bytes = BitConverter.GetBytes(originalNumber);
if (originalNumber > 0 && (bytes[bytes.Length - 1] & 0x80) > 0)
{
byte[] temp = new byte[bytes.Length];
Array.Copy(bytes, temp, bytes.Length);
bytes = new byte[temp.Length + 1];
Array.Copy(temp, bytes, temp.Length);
}
BigInteger newNumber = new BigInteger(bytes);
Console.WriteLine("Converted the UInt64 value {0:N0} to {1:N0}.",
originalNumber, newNumber);
// The example displays the following output:
// Converted the UInt64 value 18,446,744,073,709,551,615 to 18,446,744,073,709,551,615.
let originalNumber = UInt64.MaxValue
let mutable bytes = BitConverter.GetBytes originalNumber
if originalNumber > 0uL && (bytes[bytes.Length - 1] &&& 0x80uy) > 0uy then
let temp = Array.zeroCreate bytes.Length
Array.Copy(bytes, temp, bytes.Length)
bytes <- Array.zeroCreate (temp.Length + 1)
Array.Copy(temp, bytes, temp.Length)
let newNumber = bigint bytes
printfn $"Converted the UInt64 value {originalNumber:N0} to {newNumber:N0}."
// The example displays the following output:
// Converted the UInt64 value 18,446,744,073,709,551,615 to 18,446,744,073,709,551,615.
Dim originalNumber As ULong = UInt64.MaxValue
' Convert an unsigned integer to a byte array.
Dim bytes() As Byte = BitConverter.GetBytes(originalNumber)
' Determine whether the MSB of the highest-order byte is set.
If originalNumber > 0 And (bytes(bytes.Length - 1) And &h80) > 0 Then
' If the MSB is set, add one zero-value byte to the end of the array.
ReDim Preserve bytes(bytes.Length)
End If
Dim newNumber As New BigInteger(bytes)
Console.WriteLine("Converted the UInt64 value {0:N0} to {1:N0}.",
originalNumber, newNumber)
' The example displays the following output:
' Converted the UInt64 value 18,446,744,073,709,551,615 to 18,446,744,073,709,551,615.
注釈
value配列内の個々のバイトは、最下位バイトから最上位バイトまで、リトル エンディアン順である必要があります。 たとえば、次の表に示すように、数値 1,000,000,000,000 が表されます。
| 表現 | 価値 |
|---|---|
| 16 進文字列 | E8D4A51000 |
| バイト配列 (最初に最も低いインデックス) | 00 10 A5 D4 E8 00 |
BigInteger.ToByteArrayやBitConverter.GetBytesなど、数値をバイト配列に変換するほとんどのメソッドは、バイト配列をリトル エンディアン順で返します。
コンストラクターは、バイト配列内の正の値が符号と大きさの表現を使用し、負の値が 2 の補数表現を使用することを想定しています。 つまり、 value の最上位バイトの最上位ビットが設定されている場合、結果の BigInteger 値は負になります。 バイト配列のソースによっては、正の値が負の値として誤って解釈される可能性があります。 バイト配列は通常、次の方法で生成されます。
BigInteger.ToByteArray メソッドを呼び出します。 このメソッドは、正の値に対して配列内の最上位バイトの最上位ビットが 0 に設定されたバイト配列を返すため、正の値を負と誤って解釈する可能性はありません。 ToByteArray メソッドによって作成された変更されていないバイト配列は、BigInteger(Byte[]) コンストラクターに渡されると、常にラウンドトリップに成功します。
BitConverter.GetBytes メソッドを呼び出し、パラメーターとして符号付き整数を渡します。 符号付き整数は符号と大きさの表現と 2 の補数表現の両方を処理するため、正の値を負と誤って解釈する可能性はありません。
BitConverter.GetBytes メソッドを呼び出し、パラメーターとして符号なし整数を渡します。 符号なし整数は大きさのみで表されるため、正の値は負の値として誤って解釈される可能性があります。 この誤解釈を防ぐために、配列の末尾に 0 バイトの値を追加できます。 次のセクションの例では、図を示します。
前のメソッドを必ずしも呼び出さずに動的または静的にバイト配列を作成するか、既存のバイト配列を変更します。 正の値が負の値として誤って解釈されないようにするには、配列の末尾に 0 バイトの値を追加します。
valueが空のByte配列の場合、新しいBigInteger オブジェクトは BigInteger.Zero の値に初期化されます。
valueがnull場合、コンストラクターはArgumentNullExceptionをスローします。
こちらもご覧ください
適用対象
BigInteger(Decimal)
Decimal値を使用して、BigInteger構造体の新しいインスタンスを初期化します。
public:
BigInteger(System::Decimal value);
public BigInteger(decimal value);
new System.Numerics.BigInteger : decimal -> System.Numerics.BigInteger
Public Sub New (value As Decimal)
パラメーター
- value
- Decimal
10 進数。
例
次の例は、 BigInteger(Decimal) コンストラクターを使用して BigInteger オブジェクトをインスタンス化する方法を示しています。 Decimal値の配列を定義し、各値をBigInteger(Decimal)コンストラクターに渡します。 BigInteger オブジェクトに割り当てられると、Decimal値は丸めではなく切り捨てられることに注意してください。
decimal[] decimalValues = { -1790.533m, -15.1514m, 18903.79m, 9180098.003m };
foreach (decimal decimalValue in decimalValues)
{
BigInteger number = new BigInteger(decimalValue);
Console.WriteLine("Instantiated BigInteger value {0} from the Decimal value {1}.",
number, decimalValue);
}
// The example displays the following output:
// Instantiated BigInteger value -1790 from the Decimal value -1790.533.
// Instantiated BigInteger value -15 from the Decimal value -15.1514.
// Instantiated BigInteger value 18903 from the Decimal value 18903.79.
// Instantiated BigInteger value 9180098 from the Decimal value 9180098.003.
let decimalValues = [ -1790.533m; -15.1514m; 18903.79m; 9180098.003m ]
for decimalValue in decimalValues do
let number = bigint decimalValue
printfn $"Instantiated BigInteger value {number} from the Decimal value {decimalValue}."
// The example displays the following output:
// Instantiated BigInteger value -1790 from the Decimal value -1790.533.
// Instantiated BigInteger value -15 from the Decimal value -15.1514.
// Instantiated BigInteger value 18903 from the Decimal value 18903.79.
// Instantiated BigInteger value 9180098 from the Decimal value 9180098.003.
Dim decimalValues() As Decimal = { -1790.533d, -15.1514d, 18903.79d, 9180098.003d }
For Each decimalValue As Decimal In decimalValues
Dim number As New BigInteger(decimalValue)
Console.WriteLine("Instantiated BigInteger value {0} from the Decimal value {1}.",
number, decimalValue)
Next
' The example displays the following output:
' Instantiated BigInteger value -1790 from the Decimal value -1790.533.
' Instantiated BigInteger value -15 from the Decimal value -15.1514.
' Instantiated BigInteger value 18903 from the Decimal value 18903.79.
' Instantiated BigInteger value 9180098 from the Decimal value 9180098.003.
注釈
このコンストラクターを呼び出した結果は、BigInteger変数にDecimal値を明示的に割り当てるのと同じです。
このコンストラクターを呼び出すと、データが失われる可能性があります。BigInteger オブジェクトをインスタンス化すると、valueの小数部が切り捨てられます。
適用対象
BigInteger(Double)
倍精度浮動小数点値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。
public:
BigInteger(double value);
public BigInteger(double value);
new System.Numerics.BigInteger : double -> System.Numerics.BigInteger
Public Sub New (value As Double)
パラメーター
- value
- Double
倍精度浮動小数点数値。
例外
value が NaN、 NegativeInfinity、または PositiveInfinityです。
例
次の例は、 BigInteger(Double) コンストラクターを使用して BigInteger オブジェクトをインスタンス化する方法を示しています。 また、 Double データ型を使用するときに発生する可能性がある精度の損失も示しています。 Doubleには大きな値が割り当てられ、BigInteger オブジェクトに割り当てられます。 出力が示すように、この割り当てには精度の損失が伴います。 その後、両方の値が 1 ずつインクリメントされます。 出力は、 BigInteger オブジェクトが変更された値を反映しているのに対し、 Double オブジェクトには反映されないことを示しています。
// Create a BigInteger from a large double value.
double doubleValue = -6e20;
BigInteger bigIntValue = new BigInteger(doubleValue);
Console.WriteLine("Original Double value: {0:N0}", doubleValue);
Console.WriteLine("Original BigInteger value: {0:N0}", bigIntValue);
// Increment and then display both values.
doubleValue++;
bigIntValue += BigInteger.One;
Console.WriteLine("Incremented Double value: {0:N0}", doubleValue);
Console.WriteLine("Incremented BigInteger value: {0:N0}", bigIntValue);
// The example displays the following output:
// Original Double value: -600,000,000,000,000,000,000
// Original BigInteger value: -600,000,000,000,000,000,000
// Incremented Double value: -600,000,000,000,000,000,000
// Incremented BigInteger value: -599,999,999,999,999,999,999
// Create a BigInteger from a large double value.
let doubleValue = -6e20
let bigIntValue = bigint doubleValue
printfn $"Original Double value: {doubleValue:N0}"
printfn $"Original BigInteger value: {bigIntValue:N0}"
// Increment and then display both values.
let doubleValue = doubleValue + 1.
let bigIntValue = bigIntValue + BigInteger.One
printfn $"Incremented Double value: {doubleValue:N0}"
printfn $"Incremented BigInteger value: {bigIntValue:N0}"
// The example displays the following output:
// Original Double value: -600,000,000,000,000,000,000
// Original BigInteger value: -600,000,000,000,000,000,000
// Incremented Double value: -600,000,000,000,000,000,000
// Incremented BigInteger value: -599,999,999,999,999,999,999
' Create a BigInteger from a large double value.
Dim doubleValue As Double = -6e20
Dim bigIntValue As New BigInteger(doubleValue)
Console.WriteLine("Original Double value: {0:N0}", doubleValue)
Console.WriteLine("Original BigInteger value: {0:N0}", bigIntValue)
' Increment and then display both values.
doubleValue += 1
bigIntValue += BigInteger.One
Console.WriteLine("Incremented Double value: {0:N0}", doubleValue)
Console.WriteLine("Incremented BigInteger value: {0:N0}", bigIntValue)
' The example displays the following output:
' Original Double value: -600,000,000,000,000,000,000
' Original BigInteger value: -600,000,000,000,000,000,000
' Incremented Double value: -600,000,000,000,000,000,000
' Incremented BigInteger value: -599,999,999,999,999,999,999
注釈
value パラメーターの小数部は、BigInteger オブジェクトをインスタンス化するときに切り捨てられます。
Doubleデータ型の精度がないため、このコンストラクターを呼び出すとデータが失われる可能性があります。
このコンストラクターを呼び出した結果の BigInteger 値は、 Double 値を BigIntegerに明示的に割り当てた結果の値と同じです。
適用対象
BigInteger(Int32)
32 ビット符号付き整数値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。
public:
BigInteger(int value);
public BigInteger(int value);
new System.Numerics.BigInteger : int -> System.Numerics.BigInteger
Public Sub New (value As Integer)
パラメーター
- value
- Int32
32 ビット符号付き整数。
例
次の例では、 BigInteger(Int32) コンストラクターを呼び出して、32 ビット整数の配列から BigInteger 値をインスタンス化します。 また、暗黙的な変換を使用して、各 32 ビット整数値を BigInteger 変数に割り当てます。 次に、2 つの値を比較して、結果の BigInteger 値が同じであることを確立します。
int[] integers = { Int32.MinValue, -10534, -189, 0, 17, 113439,
Int32.MaxValue };
BigInteger constructed, assigned;
foreach (int number in integers)
{
constructed = new BigInteger(number);
assigned = number;
Console.WriteLine("{0} = {1}: {2}", constructed, assigned,
constructed.Equals(assigned));
}
// The example displays the following output:
// -2147483648 = -2147483648: True
// -10534 = -10534: True
// -189 = -189: True
// 0 = 0: True
// 17 = 17: True
// 113439 = 113439: True
// 2147483647 = 2147483647: True
let integers = [ Int32.MinValue; -10534; -189; 0; 17; 113439; Int32.MaxValue ]
for number in integers do
let constructed = bigint number
let assigned = number
printfn $"{constructed} = {assigned}: {constructed.Equals assigned}"
// The example displays the following output:
// -2147483648 = -2147483648: True
// -10534 = -10534: True
// -189 = -189: True
// 0 = 0: True
// 17 = 17: True
// 113439 = 113439: True
// 2147483647 = 2147483647: True
Dim integers() As Integer = { Int32.MinValue, -10534, -189, 0, 17, 113439,
Int32.MaxValue }
Dim constructed, assigned As BigInteger
For Each number As Integer In integers
constructed = New BigInteger(number)
assigned = number
Console.WriteLine("{0} = {1}: {2}", constructed, assigned,
constructed.Equals(assigned))
Next
' The example displays the following output:
' -2147483648 = -2147483648: True
' -10534 = -10534: True
' -189 = -189: True
' 0 = 0: True
' 17 = 17: True
' 113439 = 113439: True
' 2147483647 = 2147483647: True
注釈
このコンストラクターを使用して BigInteger オブジェクトをインスタンス化するときに、精度が失われる可能性はありません。
このコンストラクターを呼び出した結果の BigInteger 値は、 Int32 値を BigIntegerに割り当てた結果の値と同じです。
BigInteger構造体には、型Byte、Int16、SByte、またはUInt16のパラメーターを持つコンストラクターは含まれません。 ただし、 Int32 型では、8 ビットおよび 16 ビット符号付き整数と符号なし整数から符号付き 32 ビット整数への暗黙的な変換がサポートされています。 その結果、このコンストラクターは、 value がこれら 4 つの整数型のいずれかである場合に呼び出されます。
適用対象
BigInteger(Int64)
64 ビット符号付き整数値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。
public:
BigInteger(long value);
public BigInteger(long value);
new System.Numerics.BigInteger : int64 -> System.Numerics.BigInteger
Public Sub New (value As Long)
パラメーター
- value
- Int64
64 ビット符号付き整数。
例
次の例では、 BigInteger(Int64) コンストラクターを呼び出して、64 ビット整数の配列から BigInteger 値をインスタンス化します。 また、暗黙的な変換を使用して、各 64 ビット整数値を BigInteger 変数に割り当てます。 次に、2 つの値を比較して、結果の BigInteger 値が同じであることを確立します。
long[] longs = { Int64.MinValue, -10534, -189, 0, 17, 113439,
Int64.MaxValue };
BigInteger constructed, assigned;
foreach (long number in longs)
{
constructed = new BigInteger(number);
assigned = number;
Console.WriteLine("{0} = {1}: {2}", constructed, assigned,
constructed.Equals(assigned));
}
// The example displays the following output:
// -9223372036854775808 = -9223372036854775808: True
// -10534 = -10534: True
// -189 = -189: True
// 0 = 0: True
// 17 = 17: True
// 113439 = 113439: True
// 9223372036854775807 = 9223372036854775807: True
let longs = [ Int64.MinValue; -10534; -189; 0; 17; 113439; Int64.MaxValue ]
for number in longs do
let constructed = bigint number
let assigned = number
printfn $"{constructed} = {assigned}: {constructed.Equals assigned}"
// The example displays the following output:
// -9223372036854775808 = -9223372036854775808: True
// -10534 = -10534: True
// -189 = -189: True
// 0 = 0: True
// 17 = 17: True
// 113439 = 113439: True
// 9223372036854775807 = 9223372036854775807: True
Dim longs() As Long = { Int64.MinValue, -10534, -189, 0, 17, 113439,
Int64.MaxValue }
Dim constructed, assigned As BigInteger
For Each number As Long In longs
constructed = New BigInteger(number)
assigned = number
Console.WriteLine("{0} = {1}: {2}", constructed, assigned,
constructed.Equals(assigned))
Next
' The example displays the following output:
' -9223372036854775808 = -9223372036854775808: True
' -10534 = -10534: True
' -189 = -189: True
' 0 = 0: True
' 17 = 17: True
' 113439 = 113439: True
' 9223372036854775807 = 9223372036854775807: True
注釈
このコンストラクターを使用して BigInteger オブジェクトをインスタンス化するときに、精度が失われる可能性はありません。
このコンストラクターを呼び出した結果の BigInteger 値は、 Int64 値を BigIntegerに割り当てた結果の値と同じです。
適用対象
BigInteger(Single)
単精度浮動小数点値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。
public:
BigInteger(float value);
public BigInteger(float value);
new System.Numerics.BigInteger : single -> System.Numerics.BigInteger
Public Sub New (value As Single)
パラメーター
- value
- Single
単精度浮動小数点数値。
例外
value が NaN、 NegativeInfinity、または PositiveInfinityです。
例
次の例は、 BigInteger(Single) コンストラクターを使用して BigInteger オブジェクトをインスタンス化する方法を示しています。 また、 Single データ型を使用するときに発生する可能性がある精度の損失も示しています。 Singleには大きな負の値が割り当てられ、BigInteger オブジェクトに割り当てられます。 出力が示すように、この割り当てには精度の損失が伴います。 その後、両方の値が 1 ずつインクリメントされます。 出力は、 BigInteger オブジェクトが変更された値を反映しているのに対し、 Single オブジェクトには反映されないことを示しています。
// Create a BigInteger from a large negative Single value
float negativeSingle = Single.MinValue;
BigInteger negativeNumber = new BigInteger(negativeSingle);
Console.WriteLine(negativeSingle.ToString("N0"));
Console.WriteLine(negativeNumber.ToString("N0"));
negativeSingle++;
negativeNumber++;
Console.WriteLine(negativeSingle.ToString("N0"));
Console.WriteLine(negativeNumber.ToString("N0"));
// The example displays the following output:
// -340,282,300,000,000,000,000,000,000,000,000,000,000
// -340,282,346,638,528,859,811,704,183,484,516,925,440
// -340,282,300,000,000,000,000,000,000,000,000,000,000
// -340,282,346,638,528,859,811,704,183,484,516,925,439
// Create a BigInteger from a large negative Single value
let negativeSingle = Single.MinValue
let negativeNumber = bigint negativeSingle
printfn $"""{negativeSingle.ToString "N0"}"""
printfn $"""{negativeNumber.ToString "N0"}"""
let negativeSingle = negativeSingle + 1f
let negativeNumber = negativeNumber + 1I
printfn $"""{negativeSingle.ToString "N0"}"""
printfn $"""{negativeNumber.ToString "N0"}"""
// The example displays the following output:
// -340,282,300,000,000,000,000,000,000,000,000,000,000
// -340,282,346,638,528,859,811,704,183,484,516,925,440
// -340,282,300,000,000,000,000,000,000,000,000,000,000
// -340,282,346,638,528,859,811,704,183,484,516,925,439
' Create a BigInteger from a large negative Single value
Dim negativeSingle As Single = Single.MinValue
Dim negativeNumber As New BigInteger(negativeSingle)
Console.WriteLine(negativeSingle.ToString("N0"))
Console.WriteLine(negativeNumber.ToString("N0"))
negativeSingle += 1
negativeNumber += 1
Console.WriteLine(negativeSingle.ToString("N0"))
Console.WriteLine(negativeNumber.ToString("N0"))
' The example displays the following output:
' -340,282,300,000,000,000,000,000,000,000,000,000,000
' -340,282,346,638,528,859,811,704,183,484,516,925,440
' -340,282,300,000,000,000,000,000,000,000,000,000,000
' -340,282,346,638,528,859,811,704,183,484,516,925,439
注釈
value パラメーターの小数部は、BigInteger オブジェクトをインスタンス化するときに切り捨てられます。
Singleデータ型の精度がないため、このコンストラクターを呼び出すとデータが失われる可能性があります。
このコンストラクターを呼び出した結果の BigInteger 値は、 Single 値を BigIntegerに明示的に割り当てた結果の値と同じです。
適用対象
BigInteger(UInt32)
符号なし 32 ビット整数値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。
public:
BigInteger(System::UInt32 value);
[System.CLSCompliant(false)]
public BigInteger(uint value);
[<System.CLSCompliant(false)>]
new System.Numerics.BigInteger : uint32 -> System.Numerics.BigInteger
Public Sub New (value As UInteger)
パラメーター
- value
- UInt32
符号なし 32 ビット整数値。
- 属性
例
次の例では、 BigInteger(UInt32) コンストラクターと assignment ステートメントを使用して、符号なし 32 ビット整数の配列から BigInteger 値を初期化します。 次に、2 つの値を比較して、 BigInteger 値を初期化する 2 つの方法で同じ結果が生成されることを示します。
uint[] unsignedValues = { 0, 16704, 199365, UInt32.MaxValue };
foreach (uint unsignedValue in unsignedValues)
{
BigInteger constructedNumber = new BigInteger(unsignedValue);
BigInteger assignedNumber = unsignedValue;
if (constructedNumber.Equals(assignedNumber))
Console.WriteLine("Both methods create a BigInteger whose value is {0:N0}.",
constructedNumber);
else
Console.WriteLine("{0:N0} ≠ {1:N0}", constructedNumber, assignedNumber);
}
// The example displays the following output:
// Both methods create a BigInteger whose value is 0.
// Both methods create a BigInteger whose value is 16,704.
// Both methods create a BigInteger whose value is 199,365.
// Both methods create a BigInteger whose value is 4,294,967,295.
let unsignedValues = [ 0u; 16704u; 199365u; UInt32.MaxValue ]
for unsignedValue in unsignedValues do
let constructedNumber = bigint unsignedValue
let assignedNumber = unsignedValue
if constructedNumber.Equals assignedNumber then
printfn $"Both methods create a BigInteger whose value is {constructedNumber:N0}."
else
printfn $"{constructedNumber:N0} ≠ {assignedNumber:N0}"
// The example displays the following output:
// Both methods create a BigInteger whose value is 0.
// Both methods create a BigInteger whose value is 16,704.
// Both methods create a BigInteger whose value is 199,365.
// Both methods create a BigInteger whose value is 4,294,967,295.
Dim unsignedValues() As UInteger = { 0, 16704, 199365, UInt32.MaxValue }
For Each unsignedValue As UInteger In unsignedValues
Dim constructedNumber As New BigInteger(unsignedValue)
Dim assignedNumber As BigInteger = unsignedValue
If constructedNumber.Equals(assignedNumber) Then
Console.WriteLine("Both methods create a BigInteger whose value is {0:N0}.",
constructedNumber)
Else
Console.WriteLine("{0:N0} ≠ {1:N0}", constructedNumber, assignedNumber)
End If
Next
' The example displays the following output:
' Both methods create a BigInteger whose value is 0.
' Both methods create a BigInteger whose value is 16,704.
' Both methods create a BigInteger whose value is 199,365.
' Both methods create a BigInteger whose value is 4,294,967,295.
注釈
このコンストラクターを使用して BigInteger をインスタンス化するときに、精度が失われる可能性はありません。
このコンストラクターを呼び出した結果の BigInteger 値は、 UInt32 値を BigIntegerに割り当てた結果の値と同じです。
適用対象
BigInteger(UInt64)
符号なし 64 ビット整数値を使用して、 BigInteger 構造体の新しいインスタンスを初期化します。
public:
BigInteger(System::UInt64 value);
[System.CLSCompliant(false)]
public BigInteger(ulong value);
[<System.CLSCompliant(false)>]
new System.Numerics.BigInteger : uint64 -> System.Numerics.BigInteger
Public Sub New (value As ULong)
パラメーター
- value
- UInt64
符号なし 64 ビット整数。
- 属性
例
次の例では、BigInteger(UInt64) コンストラクターを使用して、値が MaxValue と等しいBigInteger オブジェクトをインスタンス化します。
ulong unsignedValue = UInt64.MaxValue;
BigInteger number = new BigInteger(unsignedValue);
Console.WriteLine(number.ToString("N0"));
// The example displays the following output:
// 18,446,744,073,709,551,615
let unsignedValue = UInt64.MaxValue
let number = bigint unsignedValue
printfn $"{number:N0}"
// The example displays the following output:
// 18,446,744,073,709,551,615
Dim unsignedValue As ULong = UInt64.MaxValue
Dim number As New BigInteger(unsignedValue)
Console.WriteLine(number.ToString("N0"))
' The example displays the following output:
' 18,446,744,073,709,551,615
注釈
このコンストラクターを使用して BigInteger をインスタンス化するときに、精度が失われる可能性はありません。
このコンストラクターを呼び出した結果の BigInteger 値は、 UInt64 値を BigIntegerに割り当てた結果の値と同じです。
適用対象
BigInteger(ReadOnlySpan<Byte>, Boolean, Boolean)
バイトの読み取り専用スパンの値を使用し、必要に応じて署名エンコードとエンディアンバイトオーダーを示して、 BigInteger 構造体の新しいインスタンスを初期化します。
public BigInteger(ReadOnlySpan<byte> value, bool isUnsigned = false, bool isBigEndian = false);
new System.Numerics.BigInteger : ReadOnlySpan<byte> * bool * bool -> System.Numerics.BigInteger
Public Sub New (value As ReadOnlySpan(Of Byte), Optional isUnsigned As Boolean = false, Optional isBigEndian As Boolean = false)
パラメーター
- value
- ReadOnlySpan<Byte>
大きな整数を表すバイトの読み取り専用スパン。
- isUnsigned
- Boolean
true
valueが符号なしエンコードを使用することを示す場合は。それ以外の場合はfalse (既定値)。
- isBigEndian
- Boolean
true
valueがビッグ エンディアン バイト順であることを示す場合は。それ以外の場合はfalse (既定値)。