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
API suggestion: Provide an overload of System.Numerics.BigInteger.ToString()/TryFormat() that allows precision greater than 99 #46440
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.
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!
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.
@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
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
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.
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 😄
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.
Activity
pgovind commentedon Dec 31, 2020
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!
tannergooding commentedon Jan 4, 2021
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 forfloatanddouble: #30657It likely makes sense to propose a similar API for
BigIntegerso we don't need to take a breaking change here.tannergooding commentedon Jan 4, 2021
@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 asapi-ready-for-review[-]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[/+]assumenothing commentedon Jan 5, 2021
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
outparameter. All I did was a simple parameter replace of the existing TryFormat method.tannergooding commentedon Jan 5, 2021
Normally
outparameters come last, but the other parameters are optional in this case, hence they need to come last.I'll leave the decision on where
precisiongoes to API review 😄terrajobst commentedon Jan 12, 2021
Video
floatanddouble. But given that another type showed up, maybe we should change the parser and not add the APIs.int, we throw.floatanddouble.