語言

BigInteger 建構函式

定義

初始化結構的新實例 BigInteger 。

多載

名稱 Description
BigInteger(Byte[])

利用位元組陣列中的值初始化結構的新實例 BigInteger 。

BigInteger(Decimal)

使用一個BigInteger值初始化結構的新實例Decimal。

BigInteger(Double)

使用雙精度浮點數值初始化結構的新實例 BigInteger 。

BigInteger(Int32)

使用一個 32 位元有號整數值初始化結構的新 BigInteger 實例。

BigInteger(Int64)

使用一個 64 位元有符號整數值初始化結構的新 BigInteger 實例。

BigInteger(Single)

使用單精度浮點數初始化結構的新實例 BigInteger 。

BigInteger(UInt32)

使用無符號的 32 位元整數值初始化結構的新實例 BigInteger 。

BigInteger(UInt64)

初始化一個結構的新實例 BigInteger ,使用無符號的 64 位元整數值。

BigInteger(ReadOnlySpan<Byte>, Boolean, Boolean)

使用唯讀位元組區間的值初始化結構的新實例 BigInteger ,並可選擇標示簽名編碼與字節序序。

BigInteger(Byte[])

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

重要

此 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。

範例

以下範例實例化 BigInteger 一個來自 5 元素位元組陣列的物件,其值為 {5, 4, 3, 2, 1}。 接著它會將該數值顯示 BigInteger 給主控台,這些數值以十進位和十六進位數字表示。 將輸入陣列與文字輸出比較,可以清楚理解為何類別建構子的過載 BigInteger 會產生 BigInteger 一個值為4328719365(或0x102030405)的物件。 位元組陣列的第一個元素,其值為 5,定義了物件最低階位元 BigInteger 組的值,該位元組為 0x05。 位元組陣列的第二個元素,其值為 4,定義了物件第二個位元 BigInteger 組的值,該位元組為 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 值。 請注意,這兩個值是由相同的位元組陣列表示。 它們唯一的差別是在位元組陣列中最後一個元素的最高有效位元。 若陣列由負 BigInteger 值建立,則此位元為設定(位元組值為0xFF)。 如果陣列是由正 BigInteger 值建立,位元就不是設定(位元組值為零)。

// 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

以下範例說明如何確保正值不會被錯誤實例化為負值,方法是在陣列末尾加入一個值為零的位元組。

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 如下表所示:

代表 價值觀
十六進位字串 E8D4A51000
位元組陣列(最低索引先行) 00 10 A5 D4 E8 00

大多數將數值轉換為位元組陣列的方法,如 BigInteger.ToByteArray 和 BitConverter.GetBytes,會以小端序回傳位元組陣列。

建構子期望位元組陣列中的正值使用符號與大小表示,負值則使用二的補數表示。 換句話說,若設定中 value 最高階位元組的最高階位元,所得 BigInteger 值為負值。 根據位元組陣列的來源,這可能導致正值被誤解為負值。 位元組陣列通常透過以下方式產生:

  • 藉由呼叫 BigInteger.ToByteArray 方法。 由於此方法回傳的位元組陣列中,最高階位元組的最高位元設為零,表示正值時不會被誤判為負值。 由 ToByteArray 此方法建立的未修改位元組陣列,在傳遞給 BigInteger(Byte[]) 建構子時總是能成功往返。

  • 方法是呼叫該 BitConverter.GetBytes 方法,並將一個有符號的整數作為參數傳遞給它。 由於帶符號整數同時處理符號與大小表示及二補數表示,因此不會將正值誤解為負值。

  • 方法是呼叫該 BitConverter.GetBytes 方法,並將一個無符號整數作為參數傳遞。 由於無符號整數僅以大小表示,正值可能會被誤解為負值。 為了避免這種誤解,你可以在陣列末尾加上一個零位元組的值。 下一節的範例提供了說明。

  • 可以透過動態或靜態方式建立位元組陣列,而不一定呼叫上述任何方法,或修改現有的位元組陣列。 為了避免正值被誤認為負值,你可以在陣列末端加上零位元組值。

若 value 為空 Byte 陣列,則新 BigInteger 物件初始化為 的值 BigInteger.Zero。 若 value ,null則構造子拋出 。ArgumentNullException

另請參閱

適用於

BigInteger(Decimal)

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

使用一個BigInteger值初始化結構的新實例Decimal。

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

十進位數。

範例

以下範例說明使用 BigInteger(Decimal) 建構子來實例化物件 BigInteger 的方法。 它定義一個數Decimal值陣列,然後將每個值傳給建構子。BigInteger(Decimal) 請注意 Decimal ,當值被指派給 BigInteger 物件時,是截斷而非四捨五入。

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.

備註

呼叫此建構子的結果與明確指 Decimal 派變 BigInteger 數值相同。

呼叫此建構子可能導致資料遺失;在實例化物件value時,任何BigInteger分數部分都會被截斷。

適用於

BigInteger(Double)

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

使用雙精度浮點數值初始化結構的新實例 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

雙精確度浮點值。

例外狀況

範例

以下範例說明使用 BigInteger(Double) 建構子來實例化物件 BigInteger 的方法。 同時也說明了使用資料 Double 型別時可能產生的精確度損失。 A Double 被賦予一個較大的值,接著將該值分配給物件 BigInteger 。 如輸出所示,此指派會失去精確度。 然後兩個數值會增加一。 輸出顯示 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)

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

使用一個 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 數。 接著比較兩個值,以確定結果 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 是這四種整數類型中的任一,則稱此構造子為 。

適用於

BigInteger(Int64)

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

使用一個 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 數。 接著比較兩個值,以確定結果 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.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

使用單精度浮點數初始化結構的新實例 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

單精確度浮點值。

例外狀況

範例

以下範例說明使用 BigInteger(Single) 建構子來實例化物件 BigInteger 的方法。 同時也說明了使用資料 Single 型別時可能產生的精確度損失。 A Single 被賦予一個很大的負值,接著又被賦予一個 BigInteger 物件。 如輸出所示,此指派會失去精確度。 然後兩個數值會增加一。 輸出顯示 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)

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

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Numerics.BigInteger.BigInteger(Int64)

使用無符號的 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) 建構子與指派語句,從一組無號 32 位元整數陣列初始 BigInteger 化值。 接著比較這兩種值,以證明兩種初始化 BigInteger 方法產生相同的結果。

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.

備註

使用此建構子實例化 a BigInteger 時,精度不會損失。

BigInteger呼叫此建構子所產生的值與將UInt32值指派給 BigInteger所產生的值相同。

適用於

BigInteger(UInt64)

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

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Numerics.BigInteger.BigInteger(Double)

初始化一個結構的新實例 BigInteger ,使用無符號的 64 位元整數值。

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) 建構子實例 BigInteger 化一個值為 MaxValue的物件。

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

備註

使用此建構子實例化 a BigInteger 時,精度不會損失。

BigInteger呼叫此建構子所產生的值與將UInt64值指派給 BigInteger所產生的值相同。

適用於

BigInteger(ReadOnlySpan<Byte>, Boolean, Boolean)

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

使用唯讀位元組區間的值初始化結構的新實例 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 (預設值)。

適用於