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

Should decimals with different precisions be incompatible? #28

Closed
qzb opened this issue Nov 18, 2019 · 3 comments
Closed

Should decimals with different precisions be incompatible? #28

qzb opened this issue Nov 18, 2019 · 3 comments

Comments

@qzb
Copy link

qzb commented Nov 18, 2019

Since there are a lot of issues around storing precision alongside actual value (#11, #12, #13), maybe we should make rules stricter and make decimals with different precisions incompatible:

  • creating BigDecimal requires specifying precision: BigDecimal(5, 3) === 5.000m;
  • two decimals with different precision are not equal: 1.0m !== 1.00m;
  • all arithmetic operators return decimal with the same precision as operands:
    0.7m + 0.3m === 1.0m
    0.7m - 0.3m === 0.4m
    0.7m * 0.3m === 0.2m // rounded from 0.21
    0.7m / 0.3m === 2.3m // rounded from 2.3333...
    
  • arithmetic operations on decimals with different precision throw TypeError;
  • .toString() preserves trailing zeros.

Pros

  • Clear rules for all cases.
  • Division works 🎉
  • Probably easier to optimize by using Decimal128 for decimals with low-precision, but it's not granted since Decimal128 is a floating point format.
  • Precision has to be explicitly specified during deserialization from formats which don't support it.

Cons

  • Some operations loose information, even though they can be represented with finite precision.
  • Still much more complicated than ditching idea of storing precision altogether (Should BigDecimal always be normalized? #26).
@ljharb
Copy link
Member

ljharb commented Nov 18, 2019

"Precision has to be explicitly specified" is a con, not a pro.

@littledan
Copy link
Member

I'm not really convinced that this is the behavior people want from division. Some new thoughts on the problem space at #29.

@qzb
Copy link
Author

qzb commented Nov 20, 2019

I'm closing issue, it's not relevant anymore after merging #29

@qzb qzb closed this as completed Nov 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants