BigInteger Konstruktory
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Inicializuje novou instanci BigInteger struktury.
Přetížení
| Name | Description |
|---|---|
| BigInteger(Byte[]) |
Inicializuje novou instanci BigInteger struktury pomocí hodnot v bajtovém poli. |
| BigInteger(Decimal) |
Inicializuje novou instanci BigInteger struktury pomocí Decimal hodnoty. |
| BigInteger(Double) |
Inicializuje novou instanci BigInteger struktury pomocí hodnoty s plovoucí desetinnou čárkou s dvojitou přesností. |
| BigInteger(Int32) |
Inicializuje novou instanci BigInteger struktury pomocí 32bitové celočíselné hodnoty. |
| BigInteger(Int64) |
Inicializuje novou instanci BigInteger struktury pomocí 64bitové celočíselné hodnoty. |
| BigInteger(Single) |
Inicializuje novou instanci BigInteger struktury pomocí hodnoty s plovoucí desetinnou čárkou s jednoduchou přesností. |
| BigInteger(UInt32) |
Inicializuje novou instanci BigInteger struktury pomocí 32bitové celočíselné hodnoty bez znaménka. |
| BigInteger(UInt64) |
Inicializuje novou instanci BigInteger struktury s nepodepsanou 64bitovou celočíselnou hodnotou. |
| BigInteger(ReadOnlySpan<Byte>, Boolean, Boolean) |
Inicializuje novou instanci BigInteger struktury pomocí hodnot v rozsahu jen pro čtení bajtů a volitelně označuje kódování podepisování a pořadí bajtů endianness. |
BigInteger(Byte[])
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
Důležité
Toto rozhraní API neodpovídá specifikaci CLS.
Inicializuje novou instanci BigInteger struktury pomocí hodnot v bajtovém poli.
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[]
Pole bajtů hodnot v minimálním pořadí endian.
- Atributy
Výjimky
value je null.
Příklady
Následující příklad vytvoří instanci objektu BigInteger z pole 5 prvků, jehož hodnota je {5, 4, 3, 2, 1}. Potom zobrazí BigInteger hodnotu vyjádřenou jako desetinná i šestnáctková čísla do konzoly. Porovnání vstupního pole s textovým výstupem jasně vyjasňuje, proč toto přetížení BigInteger konstruktoru třídy vytvoří BigInteger objekt, jehož hodnota je 4328719365 (nebo 0x102030405). První prvek bajtového pole, jehož hodnota je 5, definuje hodnotu nejnižšího pořadí bajtů objektu BigInteger , což je 0x05. Druhý prvek bajtového pole, jehož hodnota je 4, definuje hodnotu druhého bajtu objektu BigInteger , který je 0x04 atd.
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).
Následující příklad vytvoří instanci kladné a záporné BigInteger hodnoty, předá je metodě ToByteArray a potom obnoví původní BigInteger hodnoty z výsledného bajtového pole. Všimněte si, že tyto dvě hodnoty jsou reprezentovány identickými bajtovými poli. Jediný rozdíl mezi nimi je v nejvýznamnějším bitu posledního prvku v bajtovém poli. Tento bit je nastaven (hodnota bajtu je 0xFF), pokud je pole vytvořeno ze záporné BigInteger hodnoty. Bit není nastaven (hodnota bajtu je nula), pokud je pole vytvořeno z kladné BigInteger hodnoty.
// 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
Následující příklad ukazuje, jak zajistit, aby kladná hodnota nebyla nesprávně vytvořena jako záporná hodnota přidáním bajtu, jehož hodnota je nula na konec pole.
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.
Poznámky
Jednotlivé bajty v value poli by měly být v minimálním pořadí, od bajtu nejnižšího pořadí po bajt nejvyššího pořadí. Například číselná hodnota 1 000 000 000 000, jak je znázorněno v následující tabulce:
| Reprezentace | Hodnota |
|---|---|
| Šestnáctkový řetězec | E8D4A51000 |
| Pole bajtů (první nejnižší index) | 00 10 A5 D4 E8 00 |
Většina metod, které převádějí číselné hodnoty na bajtová pole, například BigInteger.ToByteArray a BitConverter.GetBytes, vrací bajtová pole v malém pořadí.
Konstruktor očekává, že kladná hodnota v bajtovém poli použije vyjádření znaménka a velikosti a záporné hodnoty budou používat reprezentaci dvou doplňků. Jinými slovy, pokud je nastaven bit nejvyššího pořadí bajtů value nejvyššího pořadí, výsledná hodnota je záporná BigInteger . V závislosti na zdroji bajtového pole to může způsobit, že se chybně interpretuje kladná hodnota jako záporná hodnota. Bajtová pole se obvykle generují následujícími způsoby:
Voláním BigInteger.ToByteArray metody. Vzhledem k tomu, že tato metoda vrací bajtové pole s bitem nejvyššího pořadí bajtů v matici nastavenou na nulu pro kladné hodnoty, neexistuje žádná šance, že by chybně interpretoval kladnou hodnotu jako negativní. Unmodified byte arrays created by the ToByteArray method always successfully round-trip when are passed to the BigInteger(Byte[]) constructor.
Voláním BitConverter.GetBytes metody a předáním podepsaného celého čísla jako parametru. Vzhledem k tomu, že celá čísla se znaménkem zpracovávají reprezentaci znaménka i velikostí a dvojúrovňovou reprezentaci, není možné chybně interpretovat kladnou hodnotu jako zápornou.
BitConverter.GetBytes Zavoláním metody a jejím předáním celé číslo bez znaménka jako parametru. Vzhledem k tomu, že celá čísla bez znaménka jsou reprezentována pouze jejich velikostí, lze kladné hodnoty chybně interpretovat jako záporné hodnoty. Chcete-li zabránit této chybné interpretaci, můžete na konec pole přidat nulovou bajtovou hodnotu. Příklad v další části obsahuje obrázek.
Vytvořením bajtového pole dynamicky nebo staticky, aniž by bylo nutné volat žádnou z předchozích metod, nebo úpravou existujícího bajtového pole. Pokud chcete zabránit nesprávné interpretaci kladných hodnot jako záporných hodnot, můžete na konec pole přidat nulovou bajtovou hodnotu.
Pokud value je prázdné Byte pole, nový BigInteger objekt je inicializován na hodnotu .BigInteger.Zero Pokud value je null, konstruktor vyvolá .ArgumentNullException
Viz také
Platí pro
BigInteger(Decimal)
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
Inicializuje novou instanci BigInteger struktury pomocí Decimal hodnoty.
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
Desetinné číslo.
Příklady
Následující příklad znázorňuje použití konstruktoru BigInteger(Decimal) k vytvoření instance objektu BigInteger . Definuje matici Decimal hodnot a pak předá každou hodnotu konstruktoru BigInteger(Decimal) . Všimněte si, že Decimal hodnota je zkrácena namísto zaokrouhlení při přiřazení k objektu 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.
Poznámky
Výsledek volání tohoto konstruktoru je identický s explicitním přiřazením DecimalBigInteger hodnoty proměnné.
Volání tohoto konstruktoru může způsobit ztrátu dat; jakákoli část zlomku value je zkrácena při vytváření instance objektu BigInteger .
Platí pro
BigInteger(Double)
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
Inicializuje novou instanci BigInteger struktury pomocí hodnoty s plovoucí desetinnou čárkou s dvojitou přesností.
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
Hodnota s plovoucí desetinnou čárkou s dvojitou přesností.
Výjimky
value je NaN, NegativeInfinitynebo PositiveInfinity.
Příklady
Následující příklad znázorňuje použití konstruktoru BigInteger(Double) k vytvoření instance objektu BigInteger . Ukazuje také ztrátu přesnosti, ke které může dojít při použití datového Double typu. A Double je přiřazena velká hodnota, která se pak přiřadí k objektu BigInteger . Jak ukazuje výstup, toto přiřazení zahrnuje ztrátu přesnosti. Obě hodnoty se pak zvýší o jednu. Výstup ukazuje, že BigInteger objekt odráží změněnou hodnotu, zatímco Double objekt ne.
// 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
Poznámky
Při vytváření instance objektu value se zkrátí jakákoli zlomková část parametru BigInteger .
Kvůli nedostatku přesnosti datového Double typu může volání tohoto konstruktoru způsobit ztrátu dat.
Hodnota BigInteger , která je výsledkem volání tohoto konstruktoru, je identická s hodnotou, která je výsledkem explicitního přiřazení Double hodnoty k objektu BigInteger.
Platí pro
BigInteger(Int32)
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
Inicializuje novou instanci BigInteger struktury pomocí 32bitové celočíselné hodnoty.
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
32bitové celé číslo se signedm.
Příklady
Následující příklad volá konstruktor, BigInteger(Int32) který vytvoří instanci BigInteger hodnot z pole 32bitových celých čísel. Používá také implicitní převod k přiřazení každé 32bitové celočíselné hodnoty proměnné BigInteger . Potom porovná dvě hodnoty a určí, že výsledné BigInteger hodnoty jsou stejné.
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
Poznámky
Při vytváření instance objektu pomocí tohoto konstruktoru nedojde ke ztrátě přesnosti BigInteger .
Hodnota BigInteger , která je výsledkem volání tohoto konstruktoru, je identická s hodnotou, která je výsledkem přiřazení Int32 hodnoty k objektu BigInteger.
Struktura BigInteger nezahrnuje konstruktory s parametrem typu Byte, Int16, , SBytenebo UInt16.
Int32 Tento typ však podporuje implicitní převod 8bitových a 16bitových znaménka a bez znaménka na 32bitové celá čísla. Výsledkem je, že tento konstruktor je volána, pokud value je některý z těchto čtyř integrálních typů.
Platí pro
BigInteger(Int64)
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
Inicializuje novou instanci BigInteger struktury pomocí 64bitové celočíselné hodnoty.
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
64bitové celé číslo se signedm.
Příklady
Následující příklad volá konstruktor, BigInteger(Int64) který vytvoří instanci BigInteger hodnot z pole 64bitových celých čísel. Používá také implicitní převod k přiřazení každé 64bitové celočíselné hodnoty proměnné BigInteger . Potom porovná dvě hodnoty a určí, že výsledné BigInteger hodnoty jsou stejné.
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
Poznámky
Při vytváření instance objektu pomocí tohoto konstruktoru nedojde ke ztrátě přesnosti BigInteger .
Hodnota BigInteger , která je výsledkem volání tohoto konstruktoru, je identická s hodnotou, která je výsledkem přiřazení Int64 hodnoty k objektu BigInteger.
Platí pro
BigInteger(Single)
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
Inicializuje novou instanci BigInteger struktury pomocí hodnoty s plovoucí desetinnou čárkou s jednoduchou přesností.
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
Hodnota s plovoucí desetinnou čárkou s jednoduchou přesností.
Výjimky
value je NaN, NegativeInfinitynebo PositiveInfinity.
Příklady
Následující příklad znázorňuje použití konstruktoru BigInteger(Single) k vytvoření instance objektu BigInteger . Ukazuje také ztrátu přesnosti, ke které může dojít při použití datového Single typu. A Single je přiřazena velká záporná hodnota, která se pak přiřadí k objektu BigInteger . Jak ukazuje výstup, toto přiřazení zahrnuje ztrátu přesnosti. Obě hodnoty se pak zvýší o jednu. Výstup ukazuje, že BigInteger objekt odráží změněnou hodnotu, zatímco Single objekt ne.
// 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
Poznámky
Při vytváření instance objektu value se zkrátí jakákoli zlomková část parametru BigInteger .
Kvůli nedostatku přesnosti datového Single typu může volání tohoto konstruktoru vést ke ztrátě dat.
Hodnota BigInteger , která je výsledkem volání tohoto konstruktoru, je identická s hodnotou, která je výsledkem explicitního přiřazení Single hodnoty k objektu BigInteger.
Platí pro
BigInteger(UInt32)
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
Důležité
Toto rozhraní API neodpovídá specifikaci CLS.
- Alternativa odpovídající specifikaci CLS
- System.Numerics.BigInteger.BigInteger(Int64)
Inicializuje novou instanci BigInteger struktury pomocí 32bitové celočíselné hodnoty bez znaménka.
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
32bitová celočíselná hodnota bez znaménka.
- Atributy
Příklady
Následující příklad používá BigInteger(UInt32) konstruktor a příkaz přiřazení k inicializaci BigInteger hodnot z pole bez znaménka 32bitové celé číslo. Potom porovná dvě hodnoty, aby ukázaly, že dvě metody inicializace BigInteger hodnoty mají identické výsledky.
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.
Poznámky
Při vytváření instance pomocí tohoto konstruktoru nedojde ke ztrátě přesnosti BigInteger .
Hodnota BigInteger , která je výsledkem volání tohoto konstruktoru, je identická s hodnotou, která je výsledkem přiřazení UInt32 hodnoty k objektu BigInteger.
Platí pro
BigInteger(UInt64)
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
Důležité
Toto rozhraní API neodpovídá specifikaci CLS.
- Alternativa odpovídající specifikaci CLS
- System.Numerics.BigInteger.BigInteger(Double)
Inicializuje novou instanci BigInteger struktury s nepodepsanou 64bitovou celočíselnou hodnotou.
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
Celé číslo bez znaménka.
- Atributy
Příklady
Následující příklad používá BigInteger(UInt64) konstruktor k vytvoření instance objektu BigInteger , jehož hodnota je rovna 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
Poznámky
Při vytváření instance pomocí tohoto konstruktoru nedojde ke ztrátě přesnosti BigInteger .
Hodnota BigInteger , která je výsledkem volání tohoto konstruktoru, je identická s hodnotou, která je výsledkem přiřazení UInt64 hodnoty k objektu BigInteger.
Platí pro
BigInteger(ReadOnlySpan<Byte>, Boolean, Boolean)
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
- Zdroj:
- BigInteger.cs
Inicializuje novou instanci BigInteger struktury pomocí hodnot v rozsahu jen pro čtení bajtů a volitelně označuje kódování podepisování a pořadí bajtů 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>
Rozsah bajtů představující velké celé číslo jen pro čtení.
- isUnsigned
- Boolean
true
value označuje použití kódování bez znaménka; false jinak (výchozí hodnota).
- isBigEndian
- Boolean
true označující value , že je v pořadí velkých bajtů; jinak false (výchozí hodnota).