BigInteger 构造函数

定义

初始化 BigInteger 结构的新实例。

重载

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)

使用 64 位无符号整数值初始化 BigInteger 结构的新实例。

BigInteger(ReadOnlySpan<Byte>, Boolean, Boolean)

使用字节的只读范围中的值初始化 BigInteger 结构的新实例,并选择性地指示符号编码和字节排序方式顺序。

BigInteger(Byte[])

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
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[]

顺序为 little-endian 的字节值的数组。

属性

例外

valuenull

示例

以下示例实例化 BigInteger 5 元素字节数组中的 对象,该数组的值为 {5, 4, 3, 2, 1}。 然后, BigInteger 它将值(表示为十进制和十六进制数)显示到控制台。 将输入数组与文本输出进行比较,可以清楚地了解类构造函数BigIntegerBigInteger此重载创建其值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 单个字节应采用 little-endian 顺序,从最低顺序字节到最高顺序字节。 例如,数值 1,000,000,000,000 表示如下表所示:

十六进制字符串 E8D4A51000
字节数组 (索引第一) 最低 00 10 A5 D4 E8 00

将数值转换为字节数组的大多数方法(如 BigInteger.ToByteArrayBitConverter.GetBytes)以 little-endian 顺序返回字节数组。

构造函数要求字节数组中的正值使用符号和数量级表示形式,而负值则使用二的补补表示形式。 换句话说,如果设置了 中 value 最高阶字节的最高位,则生成的 BigInteger 值为负。 根据字节数组的源,这可能会导致正值被错误地解释为负值。 字节数组通常通过以下方式生成:

  • 通过调用 BigInteger.ToByteArray 方法。 由于此方法返回一个字节数组,该数组中将正值设置为零的最高顺序字节的最高位,因此不可能将正值错误地解释为负值。 当 ToByteArray 方法创建的未修改字节数组传递到 BigInteger(Byte[]) 构造函数时,它们始终会成功往返。

  • 调用 方法并将其 BitConverter.GetBytes 作为参数传递带符号整数。 由于有符号整数同时处理符号和数量级表示形式和二者的补数表示形式,因此不可能将正值错误地解释为负值。

  • 调用 方法并将其 BitConverter.GetBytes 作为参数传递无符号整数。 由于无符号整数仅用其数量级表示,因此正值可以错误地解释为负值。 若要防止这种错误解释,可以将零字节值添加到数组的末尾。 下一部分中的示例提供了一个插图。

  • 通过动态或静态方式创建字节数组,而无需调用上述任何方法,或者通过修改现有字节数组。 若要防止正值被错误解释为负值,可以将零字节值添加到数组的末尾。

如果 value 是空 Byte 数组,则新 BigInteger 对象初始化为 值 BigInteger.Zero。 如果 valuenull,则构造函数将 ArgumentNullException引发 。

另请参阅

适用于

BigInteger(Decimal)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
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.

注解

调用此构造函数的结果与向变量显式赋 DecimalBigInteger 的结果相同。

调用此构造函数可能会导致数据丢失;实例BigInteger化 对象时, 的任何value小数部分将被截断。

适用于

BigInteger(Double)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
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 数据类型时可能发生的精度损失。 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

注解

实例化 BigInteger 对象时,value参数的任何小数部分将被截断。

由于数据类型的精度 Double 不足,调用此构造函数可能会导致数据丢失。

BigInteger调用此构造函数产生的值与将值显式分配给 DoubleBigInteger产生的值相同。

适用于

BigInteger(Int32)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
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) 构造函数来实例化 BigInteger 32 位整数数组中的值。 它还使用隐式转换将每个 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为 、 Int16SByteUInt16的参数的构造函数。 但是, Int32 类型支持将 8 位和 16 位有符号整数和无符号整数隐式转换为有符号 32 位整数。 因此,如果 value 是这四个整型类型中的任何一种,则调用此构造函数。

适用于

BigInteger(Int64)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
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) 构造函数来实例化 BigInteger 64 位整数数组中的值。 它还使用隐式转换将每个 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:
//       -2147483648 = -2147483648: True
//       -10534 = -10534: True
//       -189 = -189: True
//       0 = 0: True
//       17 = 17: True
//       113439 = 113439: True
//       2147483647 = 2147483647: 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:
//       -2147483648 = -2147483648: True
//       -10534 = -10534: True
//       -189 = -189: True
//       0 = 0: True
//       17 = 17: True
//       113439 = 113439: True
//       2147483647 = 2147483647: 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:
'       -2147483648 = -2147483648: True
'       -10534 = -10534: True
'       -189 = -189: True
'       0 = 0: True
'       17 = 17: True
'       113439 = 113439: True
'       2147483647 = 2147483647: True

注解

使用此构造函数实例化 BigInteger 对象时不会丢失精度。

BigInteger调用此构造函数产生的值与将值Int64赋给 BigInteger后得到的值相同。

适用于

BigInteger(Single)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
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 数据类型时可能发生的精度损失。 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

注解

实例化 BigInteger 对象时,value参数的任何小数部分将被截断。

由于数据类型的精度 Single 不足,因此调用此构造函数可能会导致数据丢失。

BigInteger调用此构造函数产生的值与将值显式分配给 SingleBigInteger产生的值相同。

适用于

BigInteger(UInt32)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
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) 构造函数和赋值语句初始化 BigInteger 无符号 32 位整数数组中的值。 然后,它会比较这两个值,以证明初始化值的两种方法 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.

注解

使用此构造函数实例化 时 BigInteger 不会丢失精度。

BigInteger调用此构造函数产生的值与将值分配给 UInt32 后得到的值BigInteger相同。

适用于

BigInteger(UInt64)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

重要

此 API 不符合 CLS。

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

使用 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) 构造函数来实例化 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

注解

使用此构造函数实例化 时 BigInteger 不会丢失精度。

BigInteger调用此构造函数产生的值与将值分配给 UInt64 后得到的值BigInteger相同。

适用于

BigInteger(ReadOnlySpan<Byte>, Boolean, Boolean)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
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 为 big-endian 字节顺序,则为 ;否则, false (默认值) 。

适用于