2016-05-12 13 views
3

の作品は私にとって非常に奇妙だが、あなたは自分でこの短いコード試すことができます。BigInteger.Parseが誤っ

var num1 = BigInteger.Parse("1e+9999", NumberStyles.AllowExponent); 
var num2 = BigInteger.Parse("1e+9998", NumberStyles.AllowExponent); 
var div = num1/num2; // is 1, but must be 10 
var eq = num1 == num2; // true, but must be false 

提案を?

+1

dotnet BigIntegerの文書化された範囲は何ですか? – LutzL

+0

無制限!おそらく、RAMのサイズ。 – TimeCoder

答えて

4

BigIntegerを指数関数形式で解析すると、.NET FXコードは指数を1000に制限します。指数が大きい場合は、代わりに9999(!!)の指数が代入されます。 FormatProvider.Number.cs, from line 495を参照してください。あなたは自分で見ることができます。

Console.WriteLine(BigInteger.Parse("1e+1000", NumberStyles.AllowExponent).ToString("E", CultureInfo.InvariantCulture)); 
Console.WriteLine(BigInteger.Parse("1e+1001", NumberStyles.AllowExponent).ToString("E", CultureInfo.InvariantCulture)); 

1.000000E + 1000年

1.000000E + 9999

これはBigInteger自体の限界、パーサの単なる上限ではないにもかかわらず、 :

Console.WriteLine((BigInteger.Parse("1e+1000", NumberStyles.AllowExponent) * 10).ToString("E", CultureInfo.InvariantCulture)); 

1.000000E + 1001

私は明白なバグなど意外や支離滅裂行動を呼びたいが、動作は、コードの明示的に追加一片によって引き起こされ、私は別に BigInteger限界に関する正確な仕様を見つけることができません

“arbitrarily large” and “whose value in theory has no upper or lower bounds”

+0

ありがとう!いくつかの回避策を提案できますか? BigIntegerの値を初期化するために何ができますか? 10^100000? – TimeCoder

+0

@TimeCoder '10^100000'の16進値が何であるかを把握し、[byte []'を受け入れるコンストラクタに適切な値を渡すこともできます(https://msdn.microsoft.com/en) -us/library/dd268207(v = vs.110).aspx)。 –

+0

@ScottChamberlainこれは、 "BigIntegerクラスなしで' 10^100000'の値を計算するパラドックスのようなものです。 – Eser