Skip to content

API suggestion: Provide an overload of System.Numerics.BigInteger.ToString()/TryFormat() that allows precision greater than 99 #46440

Closed

Description

@assumenothing

Description

When using BigInteger.ToString(...) or BigInteger.TryFormat(...) with a format such as 'X' or 'G' using a length specifier, only up to 99 digits can be set for the total format length.

E.g., new BigInteger(42).ToString("X100") fails to properly generate a hexadecimal string with 100 digits, but using 99 works. It results in the following unexpected string instead: X142. And I haven't found any documentation indicating this arbitrary limitation.

Given the size of the numbers typically being used with a type like this, I believe that 99 is not enough for some applications. Especially when using TryFormat and attempting to reduce the number of string/array allocations. Otherwise, awkward or inefficient workarounds are needed (such as using something like new BigInteger(42).ToString("X").PadLeft(100, '0')).

Configuration

.NET 5.0.1 x64 on Windows 10 - does not appear to be specific to any platform.

Regression?

It appears that this issue has been present ever since the file src\libraries\System.Runtime.Numerics\src\System\Numerics\BigNumber.cs was added to this repository. The earliest public .NET reference code I could find (v4.5.1) indicates that it may also have this behavior as well.

Other information

The issue appears to be in the implementation of BigNumber.ParseFormatSpecifier. There is a test inside the parser loop which breaks when n >= 10 causing it to abort, thus only allowing integer values up to 99. Ideally this should be reworked so that any valid positive (or zero) 32-bit signed integer value should be allowed (or at least whatever the internal length limit in ValueStringBuilder is set to), as long as it doesn't overflow.

This does look like it would be a potentially breaking change, but I believe a change in the format parser implementation can be justified since current behavior already results in mangled outputs.

API suggestion

It has been suggested that new API overloads should be introduced to prevent breaking changes to the existing ToString/TryFormat methods.

namespace System.Numerics.BigInteger
{
    public struct BigInteger
    {
        public string ToString(char format, int precision);
        public string ToString(char format, int precision, IFormatProvider? provider);
        public bool TryFormat(Span<char> destination, out int charsWritten, char format, int precision, IFormatProvider? provider = default);
    }
}

Activity

  1. added
    untriaged
    on Dec 29, 2020
  2. pgovind commented on Dec 31, 2020

    @pgovind

    Just assigning to @tannergooding first for his thoughts on if this would be a regression. Otherwise this looks like a bug fix and I'll investigate it next week. Might even be a candidate for up-for-grabs depending on the complexity!

  3. tannergooding commented on Jan 4, 2021

    @tannergooding
    Member

    This isn't a regression. At least today, none of the built in ToString(string format) APIs support precisions over 99 digits. This is why, for example, we approved new variants that take an explicit precision for float and double: #30657

    It likely makes sense to propose a similar API for BigInteger so we don't need to take a breaking change here.

  4. tannergooding commented on Jan 4, 2021

    @tannergooding
    Member

    @assumenothing, if you could update the original post to include a proposal for an API similar to the one's approved for float/double, I can mark this as api-ready-for-review

  5. changed the title [-]System.Numerics.BigInteger ToString()/TryFormat() fails to format properly with high digit precision specifiers.[/-] [+]API suggestion: Provide an overload of System.Numerics.BigInteger.ToString()/TryFormat() that allows precision greater than 99[/+] on Jan 5, 2021
  6. assumenothing commented on Jan 5, 2021

    @assumenothing
    Author

    Alright, I just updated the title and contents of the original post to reflect those changes. Thanks for pointing out the related issue that I could use as a template.

    However, I am not sure if the TryFormat parameters should be placed before or after the out parameter. All I did was a simple parameter replace of the existing TryFormat method.

  7. added
    api-ready-for-review
    and removed
    untriaged
    on Jan 5, 2021
  8. tannergooding commented on Jan 5, 2021

    @tannergooding
    Member

    However, I am not sure if the TryFormat parameters should be placed before or after the out parameter. All I did was a simple parameter replace of the existing TryFormat method.

    Normally out parameters come last, but the other parameters are optional in this case, hence they need to come last.

    I'll leave the decision on where precision goes to API review 😄

  9. terrajobst commented on Jan 12, 2021

    @terrajobst
    Contributor

    Video

    • We approved similar APIs for float and double. But given that another type showed up, maybe we should change the parser and not add the APIs.
    • The rule would be: a format modifier with any number of digits is interpreted a standard format + precision. If they value doesn't fit into an int, we throw.
    • We should also un-approve the APIs for float and double.
    • This is a potential breaking change. We should try to get it into .NET 6 Preview 1 so that we have the longest lead time.
    namespace System.Numerics.BigInteger
    {
        public struct BigInteger
        {
            public string ToString(char format, int precision);
            public string ToString(char format, int precision, IFormatProvider? provider);
            public bool TryFormat(Span<char> destination, out int charsWritten, char format, int precision, IFormatProvider? provider = default);
        }
    }
  10. ghost locked as resolved and limited conversation to collaborators on Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-ready-for-reviewarea-System.Numerics

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions