-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
When parsing a BigInteger in exponential form, the FX code artificially 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 or test it 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
The explicit coding seems like this was intentional, but it is hard to accept. The behavior is surprising and inconsistent; also, this exponent is not in any way a limit of BigInteger itself, only in the parsing:
Console.WriteLine((BigInteger.Parse("1e+1000", NumberStyles.AllowExponent) * 10).ToString("E", CultureInfo.InvariantCulture));
1.000000E+1001
If BigInteger.Parse needs to have a limitation (e.g. currently it uses an int to store the exponent, so it might need to have a limit around Int32.MaxValue until this gets reimplemented), it should probably rather die with a FormatException or NotSupportedException or somesuch when the limit is reached instead of delivering a completely bogus value.
(This was discovered in a question on StackOverflow.)