runtime: BigInteger.Parse returns bogus value for exponents above 1000
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.)
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (12 by maintainers)
Commits related to this issue
- Removing needless parameters. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Extracting some methods of FormatProvider and little other refactoring. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Extracting some methods of FormatProvider and little other refactoring. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Extracting some methods of FormatProvider and little other refactoring. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Extracting some methods of FormatProvider and little other refactoring. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Moving buffers to "System.Globalization.Buffers.NumberBuffer". (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Extracting some methods of FormatProvider and little other refactoring. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Moving buffers to "System.Globalization.Buffers.NumberBuffer". (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Removing needless parameters. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Extracting some methods of FormatProvider and little other refactoring. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Moving buffers to "System.Globalization.Buffers.NumberBuffer". (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Extracting contract for number buffer. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Changing number buffer for BigInteger. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Extracting some methods of FormatProvider and little other refactoring. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Extracting some methods of FormatProvider and little other refactoring. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Fixing problem with invalid parse scientific form of numbers. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Fixes by review. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Fixing problem with invalid parse scientific form of numbers. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Fixing problem with invalid parse scientific form of numbers. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
- Preventing OverflowException when parsing scientific form of numbers. (#17296) — committed to Maximys/runtime by Maximys 3 years ago
@WhiteBlackGoose, I think, your suggestion can fix exactly this issue, but for correctly fixing all cases, we should use another way for parsing. Currently I try to make it.