Reputation: 305
It's very strange for me, but you can try yourself this short code:
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
Suggestions?
Upvotes: 4
Views: 138
Reputation: 8071
When parsing a BigInteger
in exponential form, the .NET FX code limits the exponent to 1000. If a larger exponent than that is found, an exponent of 9999
(!!) is substituted instead. See FormatProvider.Number.cs, from line 495. You can see for yourself:
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
Even though this is not a limit of BigInteger
itself, just a limit of the parser:
Console.WriteLine((BigInteger.Parse("1e+1000", NumberStyles.AllowExponent) * 10).ToString("E", CultureInfo.InvariantCulture));
1.000000E+1001
I’d call such a surprising and incoherent behavior an obvious bug, but the behavior is caused by an explicitly added piece of code and I am unable to find an exact specification regarding BigInteger
limits apart from “arbitrarily large” and “whose value in theory has no upper or lower bounds”.
Upvotes: 5