Skip to content

BigInteger.Parse returns bogus value for exponents above 1000 #17296

Closed

Description

@mormegil-cz

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.)

Activity

  1. stephentoub commented on May 16, 2016

    @stephentoub
    Member
  2. mellinoe commented on May 16, 2016

    @mellinoe
    Contributor

    @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:

    1. 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.

    2. Triage the issue for the .NET Framework

    3. 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.

  3. mellinoe commented on Mar 1, 2017

    @mellinoe
    Contributor

    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.

  4. mormegil-cz commented on Mar 1, 2017

    @mormegil-cz
    Author

    @mellinoe Where is that “behavior quirk” documented, please?

  5. mellinoe commented on Mar 1, 2017

    @mellinoe
    Contributor

    @mormegil-cz Very sorry about that: it actually isn't documented anywhere that I can find. I had mistaken this for another issue.

  6. mellinoe commented on Mar 27, 2017

    @mellinoe
    Contributor

    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.

  7. ptoonen commented on Apr 29, 2017

    @ptoonen
    Contributor

    I'll take on this challenge.

  8. ptoonen commented on Apr 29, 2017

    @ptoonen
    Contributor

    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.

  9. kellypleahy commented on Jun 9, 2017

    @kellypleahy
    Contributor
  10. dfederm commented on Sep 23, 2017

    @dfederm

    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);
    }
    
  11. dfederm commented on Sep 23, 2017

    @dfederm

    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.

  12. 23 remaining items

  13. self-assigned this
    on Jul 28, 2022
  14. ghost added
    in-pr
    on Aug 9, 2022
  15. ghost removed
    in-pr
    on Aug 12, 2022
  16. ghost locked as resolved and limited conversation to collaborators on Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Cost:Sarea-System.Numericsbughelp wanted

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions