Skip to content
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

Merged
merged 8 commits into from
Aug 12, 2022

Conversation

dakersnar
Copy link
Contributor

Fixes #17296
Wrapping up the effort that was started here: #55397, I added the review suggestions the original author did not get to.

@ghost
Copy link

ghost commented Aug 9, 2022

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes #17296
Wrapping up the effort that was started here: #55397, I added the review suggestions the original author did not get to.

Author: dakersnar
Assignees: -
Labels:

area-System.Numerics

Milestone: -

public static IEnumerable<object[]> RunFormatScientificNotationToBigIntegerAndViceVersaData()
{
yield return new object[] { "1E+1000", "1E+1000" };
yield return new object[] { "1E+1001", "1E+1001" };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a couple bigger test cases.

}
}
} while (char.IsAsciiDigit(ch));
} while (char.IsAsciiDigit(ch) && (exp < int.MaxValue / 10));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to limit this to 999,999,999 like we did for ToString("G#########")?

Copy link
Contributor Author

@dakersnar dakersnar Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be safer in case there is any incrementing that happens elsewhere that I hadn't noticed. Would that require updating documentation somewhere?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely a similar breaking change doc to what we did for ToString.

Copy link
Member

@tannergooding tannergooding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just left a question on limits

@dakersnar
Copy link
Contributor Author

/azp run runtime

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Comment on lines +521 to +523
// Set exp to Int.MaxValue to signify the requested exponent is too large. This will lead to an OverflowException later.
exp = int.MaxValue;
number.scale = 0;
Copy link
Contributor Author

@dakersnar dakersnar Aug 12, 2022

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 to 0 because sometimes it's non-zero at this point and the addition on line 533 will overflow.

@@ -34,7 +34,7 @@ public static IEnumerable<object[]> Cultures
[Theory]
[MemberData(nameof(Cultures))]
[OuterLoop]
public static void RunParseToStringTests(CultureInfo culture)
public static void RunParseToStringTests(CultureInfo culture)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static void RunParseToStringTests(CultureInfo culture)
public static void RunParseToStringTests(CultureInfo culture)

@dakersnar dakersnar merged commit 9553069 into dotnet:main Aug 12, 2022
@dakersnar dakersnar deleted the fix-17296 branch August 12, 2022 19:57
@kunalspathak
Copy link
Member

Improvements on dotnet/perf-autofiling-issues#7327

@ghost ghost locked as resolved and limited conversation to collaborators Sep 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BigInteger.Parse returns bogus value for exponents above 1000
5 participants