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
Decimals lose precision when decoding. #116
Comments
Sekenre
added a commit
to Sekenre/cbor2
that referenced
this issue
Jul 21, 2021
Solution: Instead of construct the Decimal arithmetically, build an intermediate Decimal containing the sign and digits, then build the final result with a tuple of `(sign, digits, exponent)`. Since there is no calculation going on, the default precision in the Decimal context does not take effect. Unfortunately this does not work with bigfloat because the exponent in that case is a power of 2 instead of a power of 10.
Sekenre
added a commit
that referenced
this issue
Jul 23, 2021
Solution: Instead of construct the Decimal arithmetically, build an intermediate Decimal containing the sign and digits, then build the final result with a tuple of `(sign, digits, exponent)`. Since there is no calculation going on, the default precision in the Decimal context does not take effect. Unfortunately this does not work with bigfloat because the exponent in that case is a power of 2 instead of a power of 10.
vamega
pushed a commit
to vamega/cbor2
that referenced
this issue
Nov 15, 2023
Solution: Instead of construct the Decimal arithmetically, build an intermediate Decimal containing the sign and digits, then build the final result with a tuple of `(sign, digits, exponent)`. Since there is no calculation going on, the default precision in the Decimal context does not take effect. Unfortunately this does not work with bigfloat because the exponent in that case is a power of 2 instead of a power of 10.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you have a decimal fraction with more than the default 28 significant figures, it will be truncated on decode:
2 options to fix this, either set the context based on
math.log10(significand)
or build thedecimal.Decimal
object by unpacking the significand into a tuple of decimal digits.Found using Hypothesis!
The text was updated successfully, but these errors were encountered: