-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Fix BigInteger.Parse returning incorrect values for exponents above 1000 #73643
Changes from all commits
3de6672
614fbce
318fa97
9401318
f5a15da
4264827
16c134b
0221eec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -439,6 +439,42 @@ public static void CustomFormatPerMille() | |
RunCustomFormatToStringTests(s_random, "#\u2030000000", CultureInfo.CurrentCulture.NumberFormat.NegativeSign, 6, PerMilleSymbolFormatter); | ||
} | ||
|
||
public static IEnumerable<object[]> RunFormatScientificNotationToBigIntegerAndViceVersaData() | ||
{ | ||
yield return new object[] { "1E+1000", "1E+1000" }; | ||
yield return new object[] { "1E+1001", "1E+1001" }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tannergooding I was not able to get a test with "1E+2147483639" to work locally, I think it ran for too long. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's fine, as long as we have some basic tests that values over 1E+1000 work it should be good enough Testing the extreme edge cases is going to run into lots of issues. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a couple bigger test cases. |
||
yield return new object[] { "1E+10001", "1E+10001" }; | ||
yield return new object[] { "1E+100001", "1E+100001" }; | ||
yield return new object[] { "1E+99999", "1E+99999" }; | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(RunFormatScientificNotationToBigIntegerAndViceVersaData))] | ||
public static void RunFormatScientificNotationToBigIntegerAndViceVersa(string testingValue, string expectedResult) | ||
{ | ||
BigInteger parsedValue; | ||
string actualResult; | ||
|
||
parsedValue = BigInteger.Parse(testingValue, NumberStyles.AllowExponent); | ||
actualResult = parsedValue.ToString("E0"); | ||
|
||
Assert.Equal(expectedResult, actualResult); | ||
} | ||
|
||
public static IEnumerable<object[]> RunFormatScientificNotationToBigIntegerThrowsExceptionData() | ||
{ | ||
yield return new object[] { "1E+1000000000" }; | ||
yield return new object[] { "1E+2147483647" }; | ||
yield return new object[] { "1E+21474836492" }; | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(RunFormatScientificNotationToBigIntegerThrowsExceptionData))] | ||
public static void RunFormatScientificNotationToBigIntegerThrowsException(string testingValue) | ||
{ | ||
Assert.Throws<OverflowException>(() => BigInteger.Parse(testingValue, NumberStyles.AllowExponent)); | ||
} | ||
|
||
[Fact] | ||
public static void ToString_InvalidFormat_ThrowsFormatException() | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tannergooding negative exponents already had another in-use code path, so we have to use
int.MaxValue
as our Overflow signifier instead of-1
. Additonally,number.scale
has to be set to0
because sometimes it's non-zero at this point and the addition on line 533 will overflow.