You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BigInteger.Parse returns bogus value for exponents above 1000 #17296
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 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.
@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:
Fix the bug here in corefx such that projects using the NuGet package for System.Runtime.Numerics (which contains BigInteger) will have the fix immediately. This means ".NET Core" projects could get the fix immediately.
Triage the issue for the .NET Framework
Based on number 2., we will either:
a. Fix the bug normally in the .NET Framework
b. Fix the bug behind a "quirk" in the .NET Framework to maintain compatibility. This means older code will not get the fix unless they recompile or retarget to the newer version of the framework.
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.
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.
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.
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 :)
var pieces = str.Split("e");
var exponent = int.Parse(pieces[1]);
if (exponent > 1000)
{
var decimalPieces = pieces[0].Split('.');
var baseNumber = decimalPieces[0];
if (decimalPieces.Length == 2)
{
baseNumber += decimalPieces[1];
exponent -= decimalPieces[1].Length;
}
var bigInt = BigInteger.Parse(baseNumber, NumberStyles.Float);
bigInt *= BigInteger.Pow(10, exponent);
return bigInt;
}
else
{
return BigInteger.Parse(str, NumberStyles.Float);
}
Also, I think the reason for 999 being the max exponent is mainly die to string length. When you do something like BigInteger.Zero.ToString("e18") then the result is 0.000000000000000000e+000. It looks like when using exponential formatting, it just always uses exactly 3 characters, no matter what the actual number of the exponent is.
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.
Activity
stephentoub commentedon May 16, 2016
cc: @mellinoe, @axelheer
mellinoe commentedon May 16, 2016
@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:
Fix the bug here in corefx such that projects using the NuGet package for System.Runtime.Numerics (which contains
BigInteger) will have the fix immediately. This means ".NET Core" projects could get the fix immediately.Triage the issue for the .NET Framework
Based on number 2., we will either:
a. Fix the bug normally in the .NET Framework
b. Fix the bug behind a "quirk" in the .NET Framework to maintain compatibility. This means older code will not get the fix unless they recompile or retarget to the newer version of the framework.
mellinoe commentedon Mar 1, 2017
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.
mormegil-cz commentedon Mar 1, 2017
@mellinoe Where is that “behavior quirk” documented, please?
mellinoe commentedon Mar 1, 2017
@mormegil-cz Very sorry about that: it actually isn't documented anywhere that I can find. I had mistaken this for another issue.
mellinoe commentedon Mar 27, 2017
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.
ptoonen commentedon Apr 29, 2017
I'll take on this challenge.
ptoonen commentedon Apr 29, 2017
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.
kellypleahy commentedon Jun 9, 2017
FYI - relevant code is now here: https://github.com/dotnet/corefx/blob/master/src/Common/src/System/Globalization/FormatProvider.Number.cs#L478
dfederm commentedon Sep 23, 2017
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 :)
dfederm commentedon Sep 23, 2017
Also, I think the reason for 999 being the max exponent is mainly die to string length. When you do something like
BigInteger.Zero.ToString("e18")then the result is 0.000000000000000000e+000. It looks like when using exponential formatting, it just always uses exactly 3 characters, no matter what the actual number of the exponent is.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.
23 remaining items
Extracting some methods of FormatProvider and little other refactorin…
Fixing problem with invalid parse scientific form of numbers. (dotnet…
Preventing OverflowException when parsing scientific form of numbers. (…
Fixing problem with invalid parse scientific form of numbers. (dotnet…
Preventing OverflowException when parsing scientific form of numbers. (…
Fix BigInteger.Parse returning incorrect values for exponents above 1…