Repository navigation
Conversation
Motivation: Foundation stores trailing zeros in a Decimal as a positive exponent, so even Decimal(100) has significand 1 and exponent 2. The encoder passed that exponent straight to PseudoDecimal, which only accepts exponents from -3 to 0, so encoding it hit a precondition failure. Any other out-of-range Decimal trapped the same way rather than throwing. Modifications: Move the three copies of the Decimal to PseudoDecimal conversion into a single throwing initializer. It folds a positive exponent into the significand and validates the result, throwing invalidIntegerOrDecimal when the value cannot be represented. Expose PseudoDecimal.validate at package level so the encoder can use it. Add an encoder test covering items, lists and out-of-range values. Result: Decimals with positive exponents encode correctly, and unrepresentable Decimals throw instead of crashing.
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.
Motivation:
Foundation keeps trailing zeros in the exponent, so
Decimal(100)is stored as significand 1, exponent 2. The encoder passed that straight toPseudoDecimal(mantissa:exponent:), which only accepts exponents from -3 to 0, so encoding a plain100crashed:Any other Decimal that doesn't fit (too many digits, more than three decimal places) crashed the same way instead of throwing.
Modifications:
PseudoDecimal.validatepackageso the encoder can reuse it. No public API change.Result:
Decimal(100)encodes as100.0, and values that don't fit throwinvalidIntegerOrDecimalrather than taking the process down.Tested locally on macOS with Swift 6.4.1,
swift testpasses.