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

$round is rounding down to even numbers even when it's above the .5 threshold #347

Closed
praoshealth opened this issue Jun 23, 2023 · 4 comments

Comments

@praoshealth
Copy link
Contributor

 const pipeline = [
  {
    $project: {
      "10_4": { $round: [10.4, 0] },
      "10_5": { $round: [10.5, 0] },
      "10_6": { $round: [10.6, 0] },
      "10_7": { $round: [10.7, 0] },
      "11_4": { $round: [11.4, 0] },
      "11_5": { $round: [11.5, 0] },
      "11_6": { $round: [11.9, 0] },
  }
} ];

const result = new mingo.Aggregator(pipeline).run([{}]);

console.log(result);

Actual Results

[
  {
    '10_4': 10,
    '10_5': 10,
    '10_6': 10,
    '10_7': 10,
    '11_4': 11,
    '11_5': 12,
    '11_6': 12
  }
]

Expected Results

[
  {
    '10_4': 10,
    '10_5': 10,
    '10_6': 11,
    '10_7': 11,
    '11_4': 11,
    '11_5': 12,
    '11_6': 12
  }
]

Also MongoDb seems to have an undocumented feature where if you pass in a numerical value instead of an array it defaults to a precision of 0

 const pipeline = [
  {
    $project: {
      "10_4": { $round: 10.4 }
  }
} ];

const result = new mingo.Aggregator(pipeline).run([{}]);

console.log(result);

Actual Results

[{}]

Expected Results

[
  {
    '10_4': 10
  }
]
@kofrasa
Copy link
Owner

kofrasa commented Jun 25, 2023

Thanks for the report @praoshealth.

Unfortunately, this is one of those problems that is hard to fix comprehensibly given how floating point arithmetic works in JS. See #305 and the question posed on the MongoDB forum.

I think we can find an implementation that will be accurate for the given values, but I suspect there will still be a range of values that will round incorrectly with different decimal places. That said, I am happy to switch to an implementation which we think is better if the error rate is lowest for 4 or fewer decimal places which is reasonable in my view.

The relevant code is here if you wanna take a stab at it.

@praoshealth
Copy link
Contributor Author

#348

@kofrasa
Copy link
Owner

kofrasa commented Jun 27, 2023

Thanks for the PR. Happy to merge it with some expanded test coverage. Please include your examples.

    // https://github.com/kofrasa/mingo/issues/347
    [[10.4, 0], 10],
    [[10.5, 0], 10],
    [[10.6, 0], 11],
    [[10.7, 0], 11],
    [[11.4, 0], 11],
    [[11.5, 0], 12],
    [[11.9, 0], 12]

@kofrasa
Copy link
Owner

kofrasa commented Jun 28, 2023

Resolved in 6.4.3. Thanks for the contribution.

@kofrasa kofrasa closed this as completed Jun 28, 2023
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

2 participants