Repository navigation
Parse values and zeros at the edges of the exponent range [patch] - #180
Merged
Merged
Conversation
Parse subtracted every written fraction digit from the exponent in checked
int arithmetic before normalizing, and read the exponent text as an int even
when the significand was zero. So "2.500E-2147483647" and "1.0e-2147483648"
threw OverflowException although the values fit, ToString("E") output near
int.MinValue could not be parsed back, and "0e99999999999" threw where decimal
and double return 0.
Parse now drops trailing zeros before applying the exponent, does that
arithmetic in long and range-checks only the result, and returns Zero for a
zero significand whatever its well-formed exponent.
Fixes #178
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5ShapPU8BXcXvikJm
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW5ShapPU8BXcXvikJm
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #178
What was wrong
Parseread the exponent text as anintand then ranchecked(exponent - decimalDigits). That subtraction counted every written fraction digit, padding zeros included, and it happened before the value was normalized. So:"2.500E-2147483647"and"1.0e-2147483648"threwOverflowException, even though25e-2147483648and1e-2147483648are representable.ToString("E")output for values nearint.MinValuecould not be parsed back. At those magnitudes"E"is the only text form that fits in a string, so this broke round-tripping."0e99999999999"threw.decimalanddoubleboth return 0 for it.The fix
The tail of
Parsemoves into a newFromParsedPartshelper. The move keepsParseunder the CA1502 complexity limit. The helper:long, applies the fraction and trailing-zero adjustments in checkedlongarithmetic, and throwsOverflowExceptiononly if the final exponent falls outsideintZerowhen every digit is zero, whatever the exponent says, including one too large forlong. A malformed exponent ("0e","0ex","0e 5") still throwsFormatException.TryParsestill reports a genuine overflow asfalse.Tests
The new tests are in a new file,
PreciseNumberParseExponentTests.cs:Parse_AppliesTheExponentAfterDroppingPaddingZeroscovers each input from the issue, plus negative and near-int.MaxValuecases, through bothParseandTryParse.Parse_ReadsZeroWhateverItsWrittenExponentchecks, for each input, thatdecimalreturns 0 and thatPreciseNumberreturnsZero.Parse_StillOverflowsForAValueOutsideTheExponentRangechecks that genuine overflow still throws, or returnsfalsefromTryParse.Parse_RejectsAMalformedExponentEvenOnZeroParse_RoundTripsScientificTextAtTheEdgesOfTheExponentRangecoversParse(x.ToString("E"))for exponents within 10 ofint.MinValueandint.MaxValue, which is the issue's acceptance criterion.Results:
'0'characters that were already collected.Open PR #176 also edits
Parse, for NBSP separators, and PR #179 (#177) adds a style check at its top. Neither touches the exponent tail this PR changes, but whichever lands second may need a rebase.Generated by Claude Code