-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BigInteger.Parse returns bogus value for exponents above 1000 #17296
Comments
@mormegil-cz Thanks for the bug report. This behavior seems to be inherited from the .NET Framework. Usually for bugs like this we will be able to:
|
Given that this is a documented behavior quirk and is in line with what .NET Framework does, I'm going to close this. We would still most likely consider a fix for the bug, just taking into account the caveats I described above. |
@mellinoe Where is that “behavior quirk” documented, please? |
@mormegil-cz Very sorry about that: it actually isn't documented anywhere that I can find. I had mistaken this for another issue. |
I don't believe this is a crucial enough bug to block our 2.0 release, especially given that it is a pre-existing and inherited issue from .NET Framework. I'm still open to a contribution of someone wants to fix it in that timeframe, though. |
I'll take on this challenge. |
After looking at this issue for a bit, I don't see any reason why it would be limited to 9999. Can anyone at Microsoft see any reason why this limit is imposed? I suggest making the max Int32.MaxValue as @mormegil-cz already suggested. |
FYI - relevant code is now here: https://github.com/dotnet/corefx/blob/master/src/Common/src/System/Globalization/FormatProvider.Number.cs#L478 |
I have a quick and dirty fix for those looking for one. It allows parsing strings like "7.295232207972646e1336" and "7e1336". This does does some manual parsing of the number (gross) and then does multiplication for the exponent part. It's in no way a proper fix though, just works around this bug :)
|
Also, I think the reason for 999 being the max exponent is mainly die to string length. When you do something like I'm not sure that's a great reason for the restriction, but it may explain why it's there in the first place; for consistent string lengths. |
I can still reproduce this issue today. As far as I can tell, the limit is not documented, either in code as a comment or elsewhere; the restriction appears arbitrary to me. My thoughts on what we should do:
I would certainly like to hear the rationale for this limit, because it appears its purpose is not obvious to anyone here. Regarding any potential change, my understanding of the breaking change guidelines is that it would be a subtle corner case correction, which means a change isn't rendered impossible on backwards compatibility grounds per se. I'm willing to work on this issue but I'd need guidance from Microsoft on which course to take. re @dfederm: |
@tannergooding, has this been addressed by any of your changes? |
No, this has not been addressed. We should likely remove this limitation and allow |
As far as I can see, the problem is as simple as increasing this from 1000 to |
@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. |
…000 (#73643) * Fixing problem with invalid parse scientific form of numbers. (#17296) * Preventing OverflowException when parsing scientific form of numbers. (#17296) * Suggestions from review * Added bigger test cases * Adjusted cutoff to 9 digits * Accidental commit Co-authored-by: Maksim Golev <[email protected]>
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: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:
If
BigInteger.Parse
needs to have a limitation (e.g. currently it uses anint
to store the exponent, so it might need to have a limit aroundInt32.MaxValue
until this gets reimplemented), it should probably rather die with aFormatException
orNotSupportedException
or somesuch when the limit is reached instead of delivering a completely bogus value.(This was discovered in a question on StackOverflow.)
The text was updated successfully, but these errors were encountered: