语言

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
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[]

以小字节顺序排列的字节值的数组。

属性

例外

value 是 null。

示例

以下示例从值为 {5、 4、 3、 2、 1} 的 5 元素字节数组实例 BigInteger 化对象。 然后,它会向 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)

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

注解

调用此构造函数的结果与向变量显式分配 Decimal 值 BigInteger 相同。

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

适用于

BigInteger(Double)

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

适用于

BigInteger(Int32)

Source:
BigInteger.cs
Source:
BigInteger.cs
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给 a BigInteger值的值相同。

该BigInteger结构不包含具有类型Byte、Int16SByte或UInt16参数的构造函数。 但是,该 Int32 类型支持将 8 位和 16 位有符号整数和无符号整数隐式转换为有符号 32 位整数。 因此,如果 value 此构造函数是这四个整型类型中的任何一个,则调用此构造函数。

适用于

BigInteger(Int64)

Source:
BigInteger.cs
Source:
BigInteger.cs
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:
//       -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给 a BigInteger值的值相同。

适用于

BigInteger(Single)

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

适用于

BigInteger(UInt32)

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

如果为 big-endian 字节顺序,则为 ;否则为 />(默认值)。

适用于