BigInteger Konstruktory

Definicja

Inicjuje nowe wystąpienie BigInteger struktury.

Przeciążenia

Nazwa Opis
BigInteger(Byte[])

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu wartości w tablicy bajtów.

BigInteger(Decimal)

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu Decimal wartości.

BigInteger(Double)

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu wartości zmiennoprzecinkowej o podwójnej precyzji.

BigInteger(Int32)

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu 32-bitowej wartości całkowitej ze znakiem.

BigInteger(Int64)

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu 64-bitowej wartości całkowitej ze znakiem.

BigInteger(Single)

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu wartości zmiennoprzecinkowej o pojedynczej precyzji.

BigInteger(UInt32)

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu niepodpisanej 32-bitowej wartości całkowitej.

BigInteger(UInt64)

Inicjuje nowe wystąpienie BigInteger struktury z niepodpisaną 64-bitową wartością całkowitą.

BigInteger(ReadOnlySpan<Byte>, Boolean, Boolean)

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu wartości w zakresie tylko do odczytu bajtów i opcjonalnie wskazuje kodowanie podpisywania i kolejność bajtów endianness.

BigInteger(Byte[])

Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs

Ważne

Ten interfejs API nie jest zgodny ze specyfikacją CLS.

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu wartości w tablicy bajtów.

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())

Parametry

value
Byte[]

Tablica wartości bajtów w kolejności mało endianu.

Atrybuty

Wyjątki

Parametr value ma wartość null.

Przykłady

Poniższy przykład tworzy wystąpienie BigInteger obiektu z tablicy bajtów 5 elementów, której wartość to {5, 4, 3, 2, 1}. Następnie wyświetla BigInteger wartość reprezentowaną zarówno jako liczby dziesiętne, jak i szesnastkowe, do konsoli. Porównanie tablicy wejściowej z danymi wyjściowymi tekstu wyjaśnia, dlaczego to przeciążenie BigInteger konstruktora klasy tworzy BigInteger obiekt, którego wartość jest 4328719365 (lub 0x102030405). Pierwszy element tablicy bajtów, którego wartość to 5, definiuje wartość bajtu BigInteger o najniższej kolejności obiektu, który jest 0x05. Drugi element tablicy bajtów, którego wartość to 4, definiuje wartość drugiego bajtu BigInteger obiektu, który jest 0x04 itd.

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).

Poniższy przykład tworzy wystąpienie dodatniej i ujemnej BigInteger wartości, przekazuje je do ToByteArray metody, a następnie przywraca oryginalne BigInteger wartości z wynikowej tablicy bajtów. Należy pamiętać, że dwie wartości są reprezentowane przez identyczne tablice bajtów. Jedyną różnicą między nimi jest najważniejszy bit ostatniego elementu w tablicy bajtów. Ten bit jest ustawiany (wartość bajtu jest 0xFF), jeśli tablica jest tworzona na podstawie wartości ujemnej BigInteger . Bit nie jest ustawiony (wartość bajtu wynosi zero), jeśli tablica jest tworzona na podstawie wartości dodatniej 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

W poniższym przykładzie pokazano, jak upewnić się, że wartość dodatnia nie jest niepoprawnie utworzona jako wartość ujemna, dodając bajt, którego wartość jest równa zero na końcu tablicy.

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.

Uwagi

Pojedyncze bajty w value tablicy powinny być w kolejności małej, od najniższej kolejności bajtów do bajtów o najwyższej kolejności. Na przykład wartość liczbowa 1 000 000 000 000 jest reprezentowana, jak pokazano w poniższej tabeli:

Reprezentacja Wartość
Ciąg szesnastkowy E8D4A51000
Tablica bajtów (pierwszy najniższy indeks) 00 10 A5 D4 E8 00

Większość metod, które konwertują wartości liczbowe na tablice bajtów, takie jak BigInteger.ToByteArray i BitConverter.GetBytes, zwracają tablice bajtów w kolejności małej liczby.

Konstruktor oczekuje wartości dodatnich w tablicy bajtów w celu użycia reprezentacji znaku i wielkości, a wartości ujemne do użycia reprezentacji uzupełniającej dwóch. Innymi słowy, jeśli ustawiono bit najwyższego porządku bajtu najwyższego porządku, value wynikowa BigInteger wartość jest ujemna. W zależności od źródła tablicy bajtów może to spowodować błędną interpretację wartości dodatniej jako wartości ujemnej. Tablice bajtów są zwykle generowane na następujące sposoby:

  • Wywołując metodę BigInteger.ToByteArray . Ponieważ ta metoda zwraca tablicę bajtów z bitem najwyższego porządku bajtu w tablicy ustawionej na zero dla wartości dodatnich, nie ma szans na błędne interpretowanie wartości dodatniej jako ujemnej. Niezmodyfikowane tablice bajtów utworzone przez ToByteArray metodę zawsze pomyślnie zaokrąglają się, gdy są przekazywane do konstruktora BigInteger(Byte[]) .

  • Wywołując metodę BitConverter.GetBytes i przekazując ją podpisaną liczbę całkowitą jako parametr. Ze względu na to, że podpisane liczby całkowite obsługują reprezentację znaku i wielkości oraz reprezentację dwuwymiarową, nie ma szans na błędną interpretację wartości dodatniej jako ujemnej.

  • Wywołując metodę BitConverter.GetBytes i przekazując jej niepodpisaną liczbę całkowitą jako parametr. Ponieważ liczby całkowite bez znaku są reprezentowane tylko przez ich wielkość, wartości dodatnie mogą być błędnie interpretowane jako wartości ujemne. Aby zapobiec tej błędnej interpretacji, możesz dodać wartość zero-bajtową na końcu tablicy. Przykład w następnej sekcji zawiera ilustrację.

  • Tworząc tablicę bajtów dynamicznie lub statycznie bez konieczności wywoływania żadnej z poprzednich metod lub modyfikując istniejącą tablicę bajtów. Aby zapobiec błędnej interpretacji wartości dodatnich jako wartości ujemnych, możesz dodać wartość zero-bajtową na końcu tablicy.

Jeśli value jest pustą Byte tablicą, nowy BigInteger obiekt jest inicjowany do wartości BigInteger.Zero. Jeśli value parametr ma nullwartość , konstruktor zgłasza błąd ArgumentNullException.

Zobacz też

Dotyczy

BigInteger(Decimal)

Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu Decimal wartości.

public:
 BigInteger(System::Decimal value);
public BigInteger(decimal value);
new System.Numerics.BigInteger : decimal -> System.Numerics.BigInteger
Public Sub New (value As Decimal)

Parametry

value
Decimal

Liczba dziesiętna.

Przykłady

Poniższy przykład ilustruje użycie konstruktora BigInteger(Decimal) do utworzenia BigInteger wystąpienia obiektu. Definiuje tablicę Decimal wartości, a następnie przekazuje każdą wartość do konstruktora BigInteger(Decimal) . Należy pamiętać, że Decimal wartość jest obcinana zamiast zaokrąglona, gdy jest przypisana BigInteger do obiektu.

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.

Uwagi

Wynik wywołania tego konstruktora jest identyczny, aby jawnie przypisać Decimal wartość do zmiennej BigInteger .

Wywołanie tego konstruktora może spowodować utratę danych; każda część ułamkowa obiektu value jest obcinana podczas tworzenia BigInteger wystąpienia obiektu.

Dotyczy

BigInteger(Double)

Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu wartości zmiennoprzecinkowej o podwójnej precyzji.

public:
 BigInteger(double value);
public BigInteger(double value);
new System.Numerics.BigInteger : double -> System.Numerics.BigInteger
Public Sub New (value As Double)

Parametry

value
Double

Wartość zmiennoprzecinkowa o podwójnej precyzji.

Wyjątki

Przykłady

Poniższy przykład ilustruje użycie konstruktora BigInteger(Double) do utworzenia BigInteger wystąpienia obiektu. Ilustruje również utratę dokładności, która może wystąpić podczas korzystania z Double typu danych. Do Double obiektu jest przypisana duża wartość, która jest następnie przypisywana BigInteger do obiektu. Jak pokazują dane wyjściowe, to przypisanie wiąże się z utratą precyzji. Obie wartości są następnie zwiększane o jeden. Dane wyjściowe pokazują, że BigInteger obiekt odzwierciedla zmienioną wartość, natomiast Double obiekt nie.

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

Uwagi

Każda część ułamkowa parametru value jest obcinana podczas tworzenia wystąpienia BigInteger obiektu.

Ze względu na brak dokładności Double typu danych wywołanie tego konstruktora może spowodować utratę danych.

Wartość BigInteger , która wynika z wywołania tego konstruktora, jest identyczna z wartością, która wynika z jawnego przypisania Double wartości do klasy BigInteger.

Dotyczy

BigInteger(Int32)

Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu 32-bitowej wartości całkowitej ze znakiem.

public:
 BigInteger(int value);
public BigInteger(int value);
new System.Numerics.BigInteger : int -> System.Numerics.BigInteger
Public Sub New (value As Integer)

Parametry

value
Int32

32-bitowa liczba całkowita ze znakiem.

Przykłady

W poniższym przykładzie BigInteger(Int32) konstruktor wywołuje wystąpienie BigInteger wartości z tablicy 32-bitowych liczb całkowitych. Używa również niejawnej konwersji w celu przypisania każdej 32-bitowej wartości całkowitej do zmiennej BigInteger . Następnie porównuje te dwie wartości, aby ustalić, że wynikowe BigInteger wartości są takie same.

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

Uwagi

Nie ma utraty dokładności podczas tworzenia wystąpienia BigInteger obiektu przy użyciu tego konstruktora.

BigInteger Wartość, która wynika z wywołania tego konstruktora, jest identyczna z wartością, która wynika z przypisania Int32 wartości do klasy BigInteger.

Struktura BigInteger nie zawiera konstruktorów z parametrem typu Byte, Int16, SBytelub UInt16. Int32 Jednak typ obsługuje niejawną konwersję 8-bitowych i 16-bitowych z podpisem liczb całkowitych bez znaku do podpisanych 32-bitowych liczb całkowitych. W rezultacie ten konstruktor jest wywoływany, jeśli value jest jednym z tych czterech typów całkowitych.

Dotyczy

BigInteger(Int64)

Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu 64-bitowej wartości całkowitej ze znakiem.

public:
 BigInteger(long value);
public BigInteger(long value);
new System.Numerics.BigInteger : int64 -> System.Numerics.BigInteger
Public Sub New (value As Long)

Parametry

value
Int64

64-bitowa liczba całkowita ze znakiem.

Przykłady

W poniższym przykładzie BigInteger(Int64) konstruktor wywołuje wystąpienie BigInteger wartości z tablicy 64-bitowych liczb całkowitych. Używa również niejawnej konwersji w celu przypisania każdej 64-bitowej wartości całkowitej do zmiennej BigInteger . Następnie porównuje te dwie wartości, aby ustalić, że wynikowe BigInteger wartości są takie same.

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

Uwagi

Nie ma utraty dokładności podczas tworzenia wystąpienia BigInteger obiektu przy użyciu tego konstruktora.

BigInteger Wartość, która wynika z wywołania tego konstruktora, jest identyczna z wartością, która wynika z przypisania Int64 wartości do klasy BigInteger.

Dotyczy

BigInteger(Single)

Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu wartości zmiennoprzecinkowej o pojedynczej precyzji.

public:
 BigInteger(float value);
public BigInteger(float value);
new System.Numerics.BigInteger : single -> System.Numerics.BigInteger
Public Sub New (value As Single)

Parametry

value
Single

Wartość zmiennoprzecinkowa o pojedynczej precyzji.

Wyjątki

Przykłady

Poniższy przykład ilustruje użycie konstruktora BigInteger(Single) do utworzenia BigInteger wystąpienia obiektu. Ilustruje również utratę dokładności, która może wystąpić podczas korzystania z Single typu danych. Do Single obiektu jest przypisana duża wartość ujemna, która jest następnie przypisywana BigInteger do obiektu. Jak pokazują dane wyjściowe, to przypisanie wiąże się z utratą precyzji. Obie wartości są następnie zwiększane o jeden. Dane wyjściowe pokazują, że BigInteger obiekt odzwierciedla zmienioną wartość, natomiast Single obiekt nie.

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

Uwagi

Każda część ułamkowa parametru value jest obcinana podczas tworzenia wystąpienia BigInteger obiektu.

Ze względu na brak dokładności Single typu danych wywołanie tego konstruktora może spowodować utratę danych.

Wartość BigInteger , która wynika z wywołania tego konstruktora, jest identyczna z wartością, która wynika z jawnego przypisania Single wartości do klasy BigInteger.

Dotyczy

BigInteger(UInt32)

Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs

Ważne

Ten interfejs API nie jest zgodny ze specyfikacją CLS.

Alternatywa zgodna ze specyfikacją CLS
System.Numerics.BigInteger.BigInteger(Int64)

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu niepodpisanej 32-bitowej wartości całkowitej.

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)

Parametry

value
UInt32

Niepodpisane 32-bitowe wartości całkowite.

Atrybuty

Przykłady

W poniższym przykładzie użyto konstruktora BigInteger(UInt32) i instrukcji BigInteger przypisania, aby zainicjować wartości z tablicy niepodpisanych liczb całkowitych 32-bitowych. Następnie porównuje te dwie wartości, aby zademonstrować, że dwie metody inicjowania BigInteger wartości generują identyczne wyniki.

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.

Uwagi

Nie ma utraty dokładności podczas tworzenia wystąpienia BigInteger przy użyciu tego konstruktora.

Wartość BigInteger , która wynika z wywołania tego konstruktora, jest identyczna z wartością, która wynika z przypisania UInt32 wartości do klasy BigInteger.

Dotyczy

BigInteger(UInt64)

Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs

Ważne

Ten interfejs API nie jest zgodny ze specyfikacją CLS.

Alternatywa zgodna ze specyfikacją CLS
System.Numerics.BigInteger.BigInteger(Double)

Inicjuje nowe wystąpienie BigInteger struktury z niepodpisaną 64-bitową wartością całkowitą.

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)

Parametry

value
UInt64

Bez znaku 64-bitowa liczba całkowita.

Atrybuty

Przykłady

W poniższym przykładzie użyto konstruktora BigInteger(UInt64) do utworzenia BigInteger wystąpienia obiektu, którego wartość jest równa 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

Uwagi

Nie ma utraty dokładności podczas tworzenia wystąpienia BigInteger przy użyciu tego konstruktora.

Wartość BigInteger , która wynika z wywołania tego konstruktora, jest identyczna z wartością, która wynika z przypisania UInt64 wartości do klasy BigInteger.

Dotyczy

BigInteger(ReadOnlySpan<Byte>, Boolean, Boolean)

Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs
Źródło:
BigInteger.cs

Inicjuje nowe wystąpienie BigInteger struktury przy użyciu wartości w zakresie tylko do odczytu bajtów i opcjonalnie wskazuje kodowanie podpisywania i kolejność bajtów endianness.

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)

Parametry

value
ReadOnlySpan<Byte>

Zakres bajtów tylko do odczytu reprezentujący dużą liczbę całkowitą.

isUnsigned
Boolean

true aby wskazać value , false że używa niepodpisanego kodowania; w przeciwnym razie (wartość domyślna).

isBigEndian
Boolean

true w celu wskazania value jest w kolejności bajtów big-endian; w przeciwnym razie false (wartość domyślna).

Dotyczy