[SERVER-15926] Add $round and/or $integer floating-point functions Created: 03/Nov/14 Updated: 31/May/19 Resolved: 14/Feb/19 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | None |
| Fix Version/s: | 4.1.9 |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Rod Adams | Assignee: | Patrick Meredith |
| Resolution: | Done | Votes: | 12 |
| Labels: | aggregation, expression, neweng | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||
| Backwards Compatibility: | Fully Compatible | ||||||||||||||||
| Backport Requested: |
v4.0
|
||||||||||||||||
| Participants: | |||||||||||||||||
| Description |
|
Inclusion of an $int or $round operator, which can reliably group numbers which are semantically the same is thus desired. Example:
Since this is all floating point math it is subject to errors such as 1.000000001 vs 0.999999999, and therefor less accurate. |
| Comments |
| Comment by Nicholas Zolnierz [ 31/May/19 ] | ||||||||||||||
|
patrick.meredith asya What are your thoughts on backporting this? There was a subtle behavior change with $trunc and Infinity, where in 4.0 we would return NaN but in master we return Infinity. Spoke with Geert and the latter seems to be the desired behavior. | ||||||||||||||
| Comment by Githook User [ 14/Feb/19 ] | ||||||||||||||
|
Author: {'name': 'Patrick Meredith', 'email': 'pmeredit@gmail.com', 'username': 'pmeredit'}Message: Closes #1297 Signed-off-by: Charlie Swanson <charlie.swanson@mongodb.com> | ||||||||||||||
| Comment by Asya Kamsky [ 30/Jun/15 ] | ||||||||||||||
|
I'm pretty sure my intention was to provide a "close enough" functionality to rounding to a specific number of decimal places - it's true that it hits this edge case at 0. Maybe I should change it to not accept rounding to 0 significant digits | ||||||||||||||
| Comment by Geert Bosch [ 30/Jun/15 ] | ||||||||||||||
|
The round function, as you posted it, has a classical floating-point error:
The input value was less than 0.5, so why is it rounded to 1? Answer: we should implement the $round function (with an optional argument indicating number of places, defaulting to 0) that also handles the edge cases (unlike adding 0.5 and truncating) | ||||||||||||||
| Comment by Asya Kamsky [ 03/Nov/14 ] | ||||||||||||||
|
For completeness I also have a function that truncates:
| ||||||||||||||
| Comment by Asya Kamsky [ 03/Nov/14 ] | ||||||||||||||
|
I have a pre-defined function for this in the shell - similar technique could be used in other contexts until the proper feature is implemented:
Given a value and number of digits to keep after decimal, it will round it. Example:
|