Repository navigation
Conversation
Motivation: Double(PseudoDecimal) multiplied the mantissa by pow(10, exponent). For negative exponents that power of ten is not exactly representable, so the result is rounded twice and can be off by one ULP. Decoding "0.3" as a Double produced 0.30000000000000004. Modifications: Divide the mantissa by the exactly representable power of ten instead, so the result is rounded once. Add a decoder test covering the case. Result: Double(PseudoDecimal) returns the nearest Double to the decimal value.
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:
Double(PseudoDecimal)computesmantissa * pow(10, exponent). For negative exponents, values likepow(10, -1)aren't exactly representable, so the result is rounded twice and often lands one ULP off. Decoding0.3as aDoublegives0.30000000000000004, and-19.993gives-19.993000000000002.Modifications:
pow(10, -exponent)instead. 10, 100 and 1000 are exact, and the mantissa always fits in a Double's 53 bits, so there's only a single rounding step.Result:
Decimals convert to the nearest
Double. As a sanity check I comparedDouble(decimal)withDouble(String(decimal)): before the fix about 16% of a sweep of mantissas between -20,000 and 20,000 disagreed, and after it a wider sweep of 1.6 million values had no mismatches.Tested locally on macOS with Swift 6.4.1,
swift testpasses.