36
votes

Incohérences BigInt dans PowerShell et C #

Selon la documentation Microsoft, le type de données [BigInt] semble n'avoir aucune valeur maximale définie et peut théoriquement contenir un nombre infiniment grand, mais j'ai trouvé qu'après le 28e chiffre, des choses étranges commencent à se produire:

99999999999...

Comme vous pouvez le voir, sur la première commande 1 , le BigInt fonctionne comme prévu, mais avec un chiffre de plus, une certaine baisse semble se produire où elle traduit 99999999999999999999999999999 en 99999999999999991433150857216 cependant, l'invite ne génère aucune erreur et vous pouvez continuer à ajouter d'autres chiffres jusqu'à le 310e chiffre

namespace Test
{
    class Test
    {
        static void Main()
        {
            System.Numerics.BigInteger TestInput;
            TestInput = System.Numerics.BigInteger.Parse(System.Console.ReadLine());
            System.Console.WriteLine(TestInput);
        }
    }
}

qui jettera l'erreur

Integral constant is too large

Ce qui, je crois, est un problème de console plutôt qu'un problème BigInt car l'erreur ne mentionne pas le type de données [BigInt] contrairement aux nombres trop gros pour d'autres types de données comme

namespace Test
{
    class Test
    {
        static void Main()
        {
            System.Numerics.BigInteger TestInput;
            System.Numerics.BigInteger Test = 99999999999999999999;
            System.Console.WriteLine(Test);
        }
    }
}

En ce qui concerne C #, System.Numerics.BigInt commencera à lancer une erreur au 20e chiffre, 99999999999999999999 lorsqu'il est codé en dur:

PS C:\Users\Neko> [UInt64]18446744073709551615
18446744073709551615
PS C:\Users\Neko> [UInt64]18446744073709551616
Cannot convert value "18446744073709551616" to type "System.UInt64". Error: "Value was either too large or too small
for a UInt64."
At line:1 char:1
+ [UInt64]18446744073709551616
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastIConvertible

Lorsque j'essaie de créer dans Visual Studio, j'obtiens l'erreur

At line:1 char:318
+ ... 999999999999999999999999999999999999999999999999999999999999999999999
+                                                                          ~
The numeric constant 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 is not valid.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : BadNumericConstant

Cependant, je peux entrer un nombre plus grand dans ReadLine sans provoquer d'erreur

PS C:\Users\Neko> [BigInt]99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
100000000000000001097906362944045541740492309677311846336810682903157585404911491537163328978494688899061249669721172515611590283743140088328307009198146046031271664502933027185697489699588559043338384466165001178426897626212945177628091195786707458122783970171784415105291802893207873272974885715430223118336
PS C:\Users\Neko\> [BigInt]999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999

Ce qui semble en effet être infini. L'entrée

PS C:\Users\Neko> [BigInt]9999999999999999999999999999
9999999999999999999999999999
PS C:\Users\Neko> [BigInt]99999999999999999999999999999
99999999999999991433150857216

(24720 caractères au total) fonctionne bien 2

Alors, qu'est-ce qui cause toute cette activité étrange avec [BigInt] ?


1 qui ([Char[]]"$([BigInt]9999999999999999999999999999)").count 28 chiffres selon ([Char[]]"$([BigInt]9999999999999999999999999999)").count

2 Je suis trop paresseux pour compter les chiffres et essayer d'analyser les chiffres dans PowerShell provoque une erreur. Selon cela, c'est 24720 caractères


2 commentaires

Ce que je voudrais savoir (en plus) est pourquoi [BigInt]'99999999999999999999999999999' (raccourci pour [BigInt]::New('99999999999999999999999999999') , qui ne fonctionne pas et n'est mentionné comme solution de contournement dans aucun des réponses - encore ) fonctionne correctement (dans PowerShell Core 7). Surtout en sachant que le type [BigInt] n'a aucun constructeur qui accepte une chaîne (voir: [BigInt]::new ) comme entrée.


@iRon J'ai abordé ce problème dans une mise à jour.


4 Réponses :


10
votes

Malheureusement, C # n'a pas de littéral pour BigInteger. Il existe deux façons d'instancier BigInteger:

  1. Convertir à partir d'un type primitif comme int, long (cast ou en utilisant un constructeur)
  2. Analyser à partir d'une chaîne à l'aide de BigInteger.Parse

BigInteger test = BigInteger.Parse("32439845934875938475398457938457389475983475893475389457839475");
Console.WriteLine(test.ToString());
// output: 32439845934875938475398457938457389475983475893475389457839475

Découvrez comment PowerShell analyse les littéraux numériques


3 commentaires

Désolé, je n'ai peut-être pas été clair, mais cela aide à répondre à la question mais ne répond pas entièrement à la question. Je recherche les causes de l'activité étrange dans PowerShell et les incohérences entre C # et PowerShell


Cela est dû aux différences de langues. Découvrez comment PowerShell analyse les littéraux numériques docs.microsoft.com/en-us/powershell/module/… )


le plus grand littéral entier en C # (type décimal) est décimal m = 79_228_162_514_264_337_593_543_950_335m;



39
votes

TLDR : utilisez [BigInt]::Parse ou 'literal' syntaxe 'literal' avant Powershell Core 7.0; sinon utilisez le suffixe n .

Le problème - double littéraux

Lorsqu'il s'agit de littéraux sans suffixe, Powershell utilisera le premier type dans lequel la valeur s'inscrit. L'ordre des littéraux intégraux est int , long , decimal puis double . D'après la documentation de Powershell 5.1 (le mien en gras; ce paragraphe est le même pour Powershell Core):

Pour un littéral entier sans suffixe de type:

  • Si la valeur peut être représentée par le type [int] , c'est son type.
  • Sinon, si la valeur peut être représentée par type [long] , c'est son type.
  • Sinon, si la valeur peut être représentée par le type [decimal] , c'est son type.
  • Sinon, il est représenté par le type [double] .

Dans votre cas, la valeur dépasse celle de decimal.MaxValue donc votre littéral est par défaut un double littéral. Cette valeur double n'est pas exactement représentable et est "convertie" au double représentable le plus proche.

99999999999999991433150857216

Les sorties

[bigint]::new('99999999999999999999999999999') 

De toute évidence, ce n'est pas le nombre exact , juste une représentation sous forme de chaîne. Mais cela vous donne une idée de ce qui se passe. Maintenant, nous prenons cette double valeur inexacte et nous la BigInt en BigInt . La perte de précision initiale est transférée et aggravée par l' opérateur de conversion . C'est ce qui se passe réellement dans Powershell (notez le casting de BigInt ):

99999999999999999999999999999

Les sorties

99999999999999999999999999999n 

Il s'agit en fait de la valeur double représentable la plus proche. Si vous pouviez imprimer la valeur exacte du double du premier exemple, c'est ce qu'il imprimerait. Lorsque vous ajoutez les chiffres supplémentaires supplémentaires, vous dépassez la plus grande valeur d'un littéral numérique, donc l'autre exception que vous avez reçue.

Incohérences C #

Contrairement à Powershell, C # utilise des littéraux intégraux par défaut, c'est pourquoi vous obtenez l'exception pour beaucoup moins de chiffres. L'ajout du suffixe D en C # vous donnera une plus grande plage. Ce qui suit fonctionne bien et sera un double .

99999999999999999999999999999

L'ajout d'un chiffre supplémentaire soulèvera l'erreur suivante:

erreur CS0594: la constante à virgule flottante est en dehors de la plage de type 'double'

Notez que dans Powershell, le suffixe D est utilisé pour decimal littéraux decimal et non pour le double . Il n'y a pas de suffixe explicite pour double est supposé être la valeur par défaut.

Solutions

Revenez à votre problème d'origine, en fonction de votre version de Powershell, la solution peut varier:

[BigInt]::Parse

Si vous utilisez Windows Powershell ou Powershell Core <= v6.2, une option consiste à utiliser BigInteger.Parse :

[bigint]'99999999999999999999999999999' 

Les sorties:

99999999999999999999999999999

Littéraux de grande valeur

Comme indiqué dans les commentaires, une autre option qui fonctionne est de mettre le littéral entre guillemets.

[bigint]::Parse("99999999999999999999999999999") 

Les sorties

var h = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999D;

Malgré son apparence, ce n'est pas un raccourci pour [bigint]::new([string]) (voir ci-dessous). C'est plutôt un moyen de s'assurer que le littéral n'est pas traité comme un double mais plutôt comme un littéral intégral avec de nombreux chiffres, un soi-disant «littéral de grande valeur». Consultez cette section de la documentation.

N Suffixe intégral (v7.0 +)

Powershell Core 6.2 a introduit de nombreux nouveaux suffixes littéraux pour les types intégraux tels que unsigned, short et byte mais n'en a pas introduit un pour bigint . Cela est venu dans Powershell Core 7.0 via le suffixe n . Cela signifie que vous pouvez maintenant effectuer les opérations suivantes:

99999999999999991433150857216

Les sorties:

$h = [BigInt][double]99999999999999999999999999999
"{0:G}" -f $h

Consultez la documentation pour plus d'informations sur les suffixes disponibles dans Powershell Core.

[BigInt]::new

Si vous deviez essayer [bigint]::new('literal') Powershell reconnaît que vous avez l'intention d'utiliser la valeur comme un littéral . Il n'y a en fait aucun constructeur pour BigInt qui accepte une string (nous utilisons Parse pour cela) ni de constructeur qui accepte un autre BigInt . Il existe cependant un constructeur qui prend un double . Notre littéral de grande valeur commencera comme un BigInt , Powershell le convertira alors implicitement en un double (perte de précision) et le passera ensuite à [bigint]::new([double]) comme meilleure correspondance, donnant à nouveau une erreur résultat:

99999999999999991000000000000

Les sorties:

$h = [double]99999999999999999999999999999
"{0:G29}" -f $h


1 commentaires

PowerShell v7.0 a changé la façon dont les littéraux numériques sont analysés pour activer les nouvelles fonctionnalités. docs.microsoft.com/en-us/powershell/module/…



8
votes

Pour compléter les réponses existantes et utiles - notamment celles de pinkfloydx33 - avec un résumé succinct :

Par défaut, toutes les versions de PowerShell jusqu'à au moins v7.0 utilisent [double] comme type de données pour les littéraux numériques supérieurs à [decimal]::MaxValue , ce qui entraîne invariablement une perte de précision .

  • Jusqu'à la v6.x (qui inclut Windows PowerShell ), le seul moyen d'éviter cela est d' utiliser [bigint]::Parse() avec le nombre représenté sous forme de chaîne :
99999999999999999999999999999n  # v7+; parses as a [bigint], due to suffix 'n'
  • Dans la version 7 + , vous pouvez utiliser le suffixe n pour désigner un littéral numérique comme [bigint] :
[bigint]::Parse('99999999999999999999999999999')

# A *cast* works too, as also shown in pinkfloydx33's answer:
[bigint] '99999999999999999999999999999'

Remarque: On peut soutenir que, étant donné que PowerShell choisit généralement automatiquement un type de nombre approprié, il devrait supposer n dans ce cas, c'est-à-dire qu'il devrait analyser le 99999999999999999999999999999 suffixe comme un [bigint] , pas comme un [double] - voir cette proposition GitHub .


Lectures complémentaires:

  • Consultez about_Numeric_Literals , qui affiche tous les suffixes de type numérique, y compris la version de PowerShell dans laquelle ils ont été introduits.

  • Cette réponse résume la façon dont les littéraux numériques sont interprétés dans PowerShell.

    • En bref: pour les littéraux entiers , [int] est le plus petit type jamais choisi, avec [long] ou [decimal] choisi selon les besoins pour accueillir des valeurs plus grandes, avec [double] utilisé pour les valeurs au-delà de [decimal]::MaxValue .


0 commentaires

1
votes

Un peu plus d'informations à ce sujet du point de vue des métadonnées,

PowerShell et C # utiliseront tous deux le même module dans .NET Core pour obtenir le BigInt données BigInt , et vous pouvez afficher les métadonnées pour la structure BigInteger dans

System.Numerics.Biginteger test = System.Numerics.BigInteger.Parse("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999")

Cela inclut les constructeurs pour

99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999N

Ce qui signifie la base

[BigInt]::Parse("99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999")

Essaiera par inadvertance de convertir la valeur transmise au constructeur en l'un de ces types de données dont la valeur la plus élevée pourrait prendre, la valeur maximale de double , cependant C # n'essaiera pas de convertir un nombre en double sauf si spécifié avec le suffixe D donc le max la valeur qui n'a pas besoin d'un suffixe serait ULong qui a la valeur maximale de 18446744073709551615 (selon ULong.MaxValue ) qui se trouve être inférieure à 99999999999999999999 , le nombre qui a causé l'erreur de constante intégrale, mais supérieure à 9999999999999999999 , un caractère de moins . C'est pourquoi faire simplement

179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368

aura une erreur. Cependant, la méthode Parse() dans BigInteger peut prendre des valeurs autres que les types entiers du constructeur, elle prend des chaînes:

[BigInt]([double]::MaxValue)

Comme vous pouvez le voir, la méthode Parse() pour System.Numerics.BigInteger prendra des arguments de chaîne qui n'ont pas de limites car ils ne sont pas des entiers mais des mots. Voilà pourquoi la Parse() méthode en effet vous donner de l' espace de valeur illimitée.

Comme pour PowerShell, comme dans d'autres réponses, tout ce qui dépasse la valeur maximale [decimal] tentera d'être converti dans le type de données [double] qui a de nombreuses inexactitudes et la valeur maximale du type de données [double] est de 309 chiffres et c'est pourquoi les erreurs seulement apparaît après le 309e chiffre.

[BigInt][double]9999999999999999999999
10000000000000000000000
[BigInt][double]9999999999999999999999999
10000000000000000905969664
[double]9999999999999999999
1E+19

Pour convertir cela en valeur normale pas en notation scientifique, nous déplacerons le point décimal plus de 308 fois, ce qui entraînera environ

179769313486232000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

étant le double le plus élevé possible. Le type de données `[double] dans PowerShell a également des propriétés très étranges qui sont également très inexactes comme

[double]::MaxValue
1.79769313486232E+308

Étant donné que PowerShell obtient le type BigInt également à partir de la même structure que C #, les méthodes et les constructeurs sont les mêmes et Parse() fonctionnera également de la même manière dans PowerShell qu'en C #. Il y a eu des modifications dans PowerShell 7 permettant au suffixe N d'être automatiquement converti directement en BigInt .

BigInt n'utilise pas non plus la notation scientifique, donc il obtiendra toujours la valeur exacte si un autre type de données est utilisé afin d'obtenir la valeur exacte que vous pouvez utiliser

public static BigInteger Parse(ReadOnlySpan<char> value, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null);
public static BigInteger Parse(string value);
public static BigInteger Parse(string value, NumberStyles style);
public static BigInteger Parse(string value, NumberStyles style, IFormatProvider provider);
public static BigInteger Parse(string value, IFormatProvider provider);

Ce qui vous fera

System.Numerics.BigInteger test = 99999999999999999999;

étant le double le plus élevé possible.


Commandes que vous voulez

PowerShell 6.x et inférieur

System.Numerics.BigInteger test = 12345;

PowerShell 7.0 et supérieur

public BigInteger(byte[] value);
public BigInteger(decimal value);
public BigInteger(double value);
public BigInteger(int value);
public BigInteger(long value);
public BigInteger(float value);
public BigInteger(uint value);
public BigInteger(ulong value);
public BigInteger(ReadOnlySpan<byte> value, bool isUnsigned = false, bool isBigEndian = false);

C #

C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\3.1.0\ref\netcoreapp3.1\System.Runtime.Numerics.dll


0 commentaires