|
Cet article a fait l'objet d'une traduction manuelle. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte.
|
Traduction
Source
|
BigInteger, structure
Assembly : System.Numerics (dans System.Numerics.dll)
Le type BigInteger expose les membres suivants.
| Nom | Description | |
|---|---|---|
![]() | BigInteger(Byte[]) | |
![]() | BigInteger(Decimal) | |
![]() | BigInteger(Double) | |
![]() | BigInteger(Int32) | |
![]() | BigInteger(Int64) | |
![]() | BigInteger(Single) | |
![]() | BigInteger(UInt32) | |
![]() | BigInteger(UInt64) |
| Nom | Description | |
|---|---|---|
![]() | IsEven | |
![]() | IsOne | |
![]() | IsPowerOfTwo | |
![]() | IsZero | |
![]() ![]() | MinusOne | |
![]() ![]() | One | |
![]() | Sign | |
![]() ![]() | Zero |
| Nom | Description | |
|---|---|---|
![]() ![]() | Abs | |
![]() ![]() | Add | |
![]() ![]() | Compare | |
![]() | CompareTo(BigInteger) | |
![]() | CompareTo(Int64) | |
![]() | CompareTo(Object) | |
![]() | CompareTo(UInt64) | |
![]() ![]() | Divide | |
![]() ![]() | DivRem | |
![]() | Equals(BigInteger) | |
![]() | Equals(Int64) | |
![]() | Equals(Object) | |
![]() | Equals(UInt64) | |
![]() | Finalize | |
![]() | GetHashCode | |
![]() | GetType | |
![]() ![]() | GreatestCommonDivisor | |
![]() ![]() | Log(BigInteger) | |
![]() ![]() | Log(BigInteger, Double) | |
![]() ![]() | Log10 | |
![]() ![]() | Max | |
![]() | MemberwiseClone | |
![]() ![]() | Min | |
![]() ![]() | ModPow | |
![]() ![]() | Multiply | |
![]() ![]() | Negate | |
![]() ![]() | Parse(String) | |
![]() ![]() | Parse(String, NumberStyles) | |
![]() ![]() | Parse(String, IFormatProvider) | |
![]() ![]() | Parse(String, NumberStyles, IFormatProvider) | |
![]() ![]() | Pow | |
![]() ![]() | Remainder | |
![]() ![]() | Subtract | |
![]() | ToByteArray | |
![]() | ToString() | |
![]() | ToString(IFormatProvider) | |
![]() | ToString(String) | |
![]() | ToString(String, IFormatProvider) | |
![]() ![]() | TryParse(String, BigInteger) | |
![]() ![]() | TryParse(String, NumberStyles, IFormatProvider, BigInteger) |
| Nom | Description | |
|---|---|---|
![]() ![]() | Addition | |
![]() ![]() | BitwiseAnd | |
![]() ![]() | BitwiseOr | |
![]() ![]() | Decrement | |
![]() ![]() | Division | |
![]() ![]() | Equality(BigInteger, Int64) | |
![]() ![]() | Equality(BigInteger, BigInteger) | |
![]() ![]() | Equality(BigInteger, UInt64) | |
![]() ![]() | Equality(Int64, BigInteger) | |
![]() ![]() | Equality(UInt64, BigInteger) | |
![]() ![]() | ExclusiveOr | |
![]() ![]() | Explicit(BigInteger to Byte) | |
![]() ![]() | Explicit(BigInteger to Int32) | |
![]() ![]() | Explicit(BigInteger to UInt64) | |
![]() ![]() | Explicit(BigInteger to Single) | |
![]() ![]() | Explicit(BigInteger to Double) | |
![]() ![]() | Explicit(BigInteger to Int64) | |
![]() ![]() | Explicit(BigInteger to Int16) | |
![]() ![]() | Explicit(BigInteger to SByte) | |
![]() ![]() | Explicit(BigInteger to UInt32) | |
![]() ![]() | Explicit(BigInteger to UInt16) | |
![]() ![]() | Explicit(BigInteger to Decimal) | |
![]() ![]() | Explicit(Decimal to BigInteger) | |
![]() ![]() | Explicit(Double to BigInteger) | |
![]() ![]() | Explicit(Single to BigInteger) | |
![]() ![]() | GreaterThan(BigInteger, Int64) | |
![]() ![]() | GreaterThan(BigInteger, BigInteger) | |
![]() ![]() | GreaterThan(BigInteger, UInt64) | |
![]() ![]() | GreaterThan(Int64, BigInteger) | |
![]() ![]() | GreaterThan(UInt64, BigInteger) | |
![]() ![]() | GreaterThanOrEqual(BigInteger, Int64) | |
![]() ![]() | GreaterThanOrEqual(BigInteger, BigInteger) | |
![]() ![]() | GreaterThanOrEqual(BigInteger, UInt64) | |
![]() ![]() | GreaterThanOrEqual(Int64, BigInteger) | |
![]() ![]() | GreaterThanOrEqual(UInt64, BigInteger) | |
![]() ![]() | Implicit(Byte to BigInteger) | |
![]() ![]() | Implicit(Int16 to BigInteger) | |
![]() ![]() | Implicit(Int32 to BigInteger) | |
![]() ![]() | Implicit(Int64 to BigInteger) | |
![]() ![]() | Implicit(SByte to BigInteger) | |
![]() ![]() | Implicit(UInt16 to BigInteger) | |
![]() ![]() | Implicit(UInt32 to BigInteger) | |
![]() ![]() | Implicit(UInt64 to BigInteger) | |
![]() ![]() | Increment | |
![]() ![]() | Inequality(BigInteger, Int64) | |
![]() ![]() | Inequality(BigInteger, BigInteger) | |
![]() ![]() | Inequality(BigInteger, UInt64) | |
![]() ![]() | Inequality(Int64, BigInteger) | |
![]() ![]() | Inequality(UInt64, BigInteger) | |
![]() ![]() | LeftShift | |
![]() ![]() | LessThan(BigInteger, Int64) | |
![]() ![]() | LessThan(BigInteger, BigInteger) | |
![]() ![]() | LessThan(BigInteger, UInt64) | |
![]() ![]() | LessThan(Int64, BigInteger) | |
![]() ![]() | LessThan(UInt64, BigInteger) | |
![]() ![]() | LessThanOrEqual(BigInteger, Int64) | |
![]() ![]() | LessThanOrEqual(BigInteger, BigInteger) | |
![]() ![]() | LessThanOrEqual(BigInteger, UInt64) | |
![]() ![]() | LessThanOrEqual(Int64, BigInteger) | |
![]() ![]() | LessThanOrEqual(UInt64, BigInteger) | |
![]() ![]() | Modulus | |
![]() ![]() | Multiply | |
![]() ![]() | OnesComplement | |
![]() ![]() | RightShift | |
![]() ![]() | Subtraction | |
![]() ![]() | UnaryNegation | |
![]() ![]() | UnaryPlus |
Remarque |
|---|
Instanciation d'un objet BigInteger
Vous pouvez utiliser le mot clé new et fournir n'importe quelle valeur intégrale ou à virgule flottante comme paramètre au constructeur BigInteger. (Les valeurs à virgule flottante sont tronquées avant qu'elles soient assignées au BigInteger.) L'exemple suivant illustre comment utiliser le mot clé new pour instancier des valeurs BigInteger. Vous pouvez déclarer une variable BigInteger et lui assigner une valeur tout comme n'importe quel type numérique, tant que cette valeur est de type intégral. L'exemple suivant utilise une assignation pour créer une valeur BigInteger à partir d'un Int64. Vous pouvez assigner une valeur décimale ou à virgule flottante à un objet BigInteger si vous effectuez un cast sur la valeur ou la convertissez en premier lieu. L'exemple suivant caste (en C#) ou convertit (en Visual Basic) explicitement une valeur Double et Decimal en BigInteger.
Vous pouvez utiliser le mot clé new et fournir un tableau d'octets de n'importe quelle taille au constructeur BigInteger.BigInteger. Ce constructeur peut être utilisé pour créer une valeur BigInteger positive uniquement. Par exemple : byte[] byteArray = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; BigInteger newBigInt = new BigInteger(byteArray); Console.WriteLine("The value of newBigInt is {0} (or 0x{0:x}).", newBigInt); // The example displays the following output: // The value of newBigInt is 4759477275222530853130 (or 0x102030405060708090a).
Vous pouvez appeler les méthodes Parse ou TryParse pour convertir la représentation sous forme de chaîne d'un nombre en un BigInteger. Par exemple : string positiveString = "91389681247993671255432112000000"; string negativeString = "-90315837410896312071002088037140000"; BigInteger posBigInt = 0; BigInteger negBigInt = 0; try { posBigInt = BigInteger.Parse(positiveString); Console.WriteLine(posBigInt); } catch (FormatException) { Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.", positiveString); } if (BigInteger.TryParse(negativeString, out negBigInt)) Console.WriteLine(negBigInt); else Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.", negativeString); // The example displays the following output: // 9.1389681247993671255432112E+31 // -9.0315837410896312071002088037E+34
Vous pouvez appeler une méthode static (Shared dans Visual Basic) BigInteger qui effectue certaines opérations sur une expression numérique et retourne un résultat BigInteger calculé. Pour ce faire, l'exemple suivant cube UInt64.MaxValue et assigne le résultat à BigInteger.
Exécution d'opérations sur des valeurs BigInteger
Sign , qui retourne une valeur qui indique le signe d'une valeur BigInteger. Abs , qui retourne la valeur absolue d'une valeur BigInteger. DivRem , qui retourne à la fois le quotient et reste d'une opération de division. GreatestCommonDivisor , qui retourne le plus grand diviseur commun de deux valeurs BigInteger.
Mutabilité et structure BigInteger
Remarque |
|---|
BigInteger number = Int64.MaxValue ^ 5; int repetitions = 1000000; // Perform some repetitive operation 1 million times. for (int ctr = 0; ctr <= repetitions; ctr++) { // Perform some operation. If it fails, exit the loop. if (! SomeOperationSucceeds()) break; // The following code executes if the operation succeeds. number++; }
BigInteger number = Int64.MaxValue ^ 5; int repetitions = 1000000; int actualRepetitions = 0; // Perform some repetitive operation 1 million times. for (int ctr = 0; ctr <= repetitions; ctr++) { // Perform some operation. If it fails, exit the loop. if (! SomeOperationSucceeds()) break; // The following code executes if the operation succeeds. actualRepetitions++; } number += actualRepetitions;
Utilisation de tableaux d'octets et de chaînes hexadécimales
BigInteger number = BigInteger.Pow(Int64.MaxValue, 2); Console.WriteLine(number); // Write the BigInteger value to a byte array. byte[] bytes = number.ToByteArray(); // Display the byte array. foreach (byte byteValue in bytes) Console.Write("0x{0:X2} ", byteValue); Console.WriteLine(); // Restore the BigInteger value from a Byte array. BigInteger newNumber = new BigInteger(bytes); Console.WriteLine(newNumber); // The example displays the following output: // 8.5070591730234615847396907784E+37 // 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0x3F // // 8.5070591730234615847396907784E+37
short originalValue = 30000; Console.WriteLine(originalValue); // Convert the Int16 value to a byte array. byte[] bytes = BitConverter.GetBytes(originalValue); // Display the byte array. foreach (byte byteValue in bytes) Console.Write("0x{0} ", byteValue.ToString("X2")); Console.WriteLine(); // Pass byte array to the BigInteger constructor. BigInteger number = new BigInteger(bytes); Console.WriteLine(number); // The example displays the following output: // 30000 // 0x30 0x75 // 30000
int negativeNumber = -1000000; uint positiveNumber = 4293967296; byte[] negativeBytes = BitConverter.GetBytes(negativeNumber); BigInteger negativeBigInt = new BigInteger(negativeBytes); Console.WriteLine(negativeBigInt.ToString("N0")); byte[] tempPosBytes = BitConverter.GetBytes(positiveNumber); byte[] positiveBytes = new byte[tempPosBytes.Length + 1]; Array.Copy(tempPosBytes, positiveBytes, tempPosBytes.Length); BigInteger positiveBigInt = new BigInteger(positiveBytes); Console.WriteLine(positiveBigInt.ToString("N0")); // The example displays the following output: // -1,000,000 // 4,293,967,296
BigInteger positiveValue = 15777216; BigInteger negativeValue = -1000000; Console.WriteLine("Positive value: " + positiveValue.ToString("N0")); byte[] bytes = positiveValue.ToByteArray(); foreach (byte byteValue in bytes) Console.Write("{0:X2} ", byteValue); Console.WriteLine(); positiveValue = new BigInteger(bytes); Console.WriteLine("Restored positive value: " + positiveValue.ToString("N0")); Console.WriteLine(); Console.WriteLine("Negative value: " + negativeValue.ToString("N0")); bytes = negativeValue.ToByteArray(); foreach (byte byteValue in bytes) Console.Write("{0:X2} ", byteValue); Console.WriteLine(); negativeValue = new BigInteger(bytes); Console.WriteLine("Restored negative value: " + negativeValue.ToString("N0")); // The example displays the following output: // Positive value: 15,777,216 // C0 BD F0 00 // Restored positive value: 15,777,216 // // Negative value: -1,000,000 // C0 BD F0 // Restored negative value: -1,000,000
BigInteger negativeNumber = -1000000; BigInteger positiveNumber = 15777216; string negativeHex = negativeNumber.ToString("X"); string positiveHex = positiveNumber.ToString("X"); BigInteger negativeNumber2, positiveNumber2; negativeNumber2 = BigInteger.Parse(negativeHex, NumberStyles.HexNumber); positiveNumber2 = BigInteger.Parse(positiveHex, NumberStyles.HexNumber); Console.WriteLine("Converted {0:N0} to {1} back to {2:N0}.", negativeNumber, negativeHex, negativeNumber2); Console.WriteLine("Converted {0:N0} to {1} back to {2:N0}.", positiveNumber, positiveHex, positiveNumber2); // The example displays the following output: // Converted -1,000,000 to F0BDC0 back to -1,000,000. // Converted 15,777,216 to 0F0BDC0 back to 15,777,216.
using System; using System.Globalization; using System.Numerics; public struct HexValue { public int Sign; public string Value; } public class Example { public static void Main() { uint positiveNumber = 4039543321; int negativeNumber = -255423975; // Convert the numbers to hex strings. HexValue hexValue1, hexValue2; hexValue1.Value = positiveNumber.ToString("X"); hexValue1.Sign = Math.Sign(positiveNumber); hexValue2.Value = Convert.ToString(negativeNumber, 16); hexValue2.Sign = Math.Sign(negativeNumber); // Round-trip the hexadecimal values to BigInteger values. string hexString; BigInteger positiveBigInt, negativeBigInt; hexString = (hexValue1.Sign == 1 ? "0" : "") + hexValue1.Value; positiveBigInt = BigInteger.Parse(hexString, NumberStyles.HexNumber); Console.WriteLine("Converted {0} to {1} and back to {2}.", positiveNumber, hexValue1.Value, positiveBigInt); hexString = (hexValue2.Sign == 1 ? "0" : "") + hexValue2.Value; negativeBigInt = BigInteger.Parse(hexString, NumberStyles.HexNumber); Console.WriteLine("Converted {0} to {1} and back to {2}.", negativeNumber, hexValue2.Value, negativeBigInt); } } // The example displays the following output: // Converted 4039543321 to F0C68A19 and back to 4039543321. // Converted -255423975 to f0c68a19 and back to -255423975.
Windows 7, Windows Vista SP1 ou ultérieur, Windows XP SP3, Windows Server 2008 (installation minimale non prise en charge), Windows Server 2008 R2 (installation minimale prise en charge avec SP1 ou version ultérieure), Windows Server 2003 SP2
Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.
