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

fix(NODE-5586): Decimal128 fromString performs inexact rounding #620

Merged
merged 2 commits into from
Sep 11, 2023

Conversation

W-A-James
Copy link
Contributor

@W-A-James W-A-James commented Sep 8, 2023

Description

What is changing?

  • Corrected Decimal128.fromString algorithm to match that in mongo-c-driver
  • Unskipped Decimal128 spec tests
  • Update Decimal128 spec tests
Is there new documentation needed for these changes?
  • No

What is the motivation for this change?

NODE-5586

Release Highlight

Decimal128 constructor now throws when detecting loss of precision

Prior to this release, Decimal128 would round numbers with more than 34 significant digits and lose precision. Now, on detecting loss of precision, Decimal128's constructor and Decimal128.fromString will throw a BSONError. This behaviour should have been the default as the Decimal128 class was always intended to be high-precision floating point value. As such, silently rounding is undesirable behaviour.

// previous behaviour
> new Decimal128('10000000000000000000000000000000001')
new Decimal128("1.000000000000000000000000000000000E+34")

// new behaviour
> new Decimal128('10000000000000000000000000000000001')
Uncaught:
BSONError: "10000000000000000000000000000000001" is not a valid Decimal128 string - inexact rounding
    at invalidErr (/home/wajames/js-bson/lib/bson.cjs:1402:11)
    at Decimal128.fromString (/home/wajames/js-bson/lib/bson.cjs:1555:21)
    at new Decimal128 (/home/wajames/js-bson/lib/bson.cjs:1411:37)

Double check the following

  • Ran npm run check:lint script
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@W-A-James W-A-James changed the title fix(NODE-5546): decimal 128 fromString performs inexact rounding (#613) fix(NODE-5586): Decimal128 fromString performs inexact rounding Sep 8, 2023
@W-A-James W-A-James marked this pull request as ready for review September 8, 2023 19:37
@nbbeeken nbbeeken self-requested a review September 11, 2023 18:13
@nbbeeken nbbeeken self-assigned this Sep 11, 2023
@nbbeeken nbbeeken added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Sep 11, 2023
@nbbeeken nbbeeken merged commit 63fb316 into 5.x Sep 11, 2023
3 checks passed
@nbbeeken nbbeeken deleted the NODE-5586 branch September 11, 2023 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Primary Review In Review with primary reviewer, not yet ready for team's eyes
Projects
None yet
2 participants