[DOCS-7792] $mod silently truncates decimal arguments to integers Created: 03/May/16 Updated: 30/Oct/23 Resolved: 31/Oct/22 |
|
| Status: | Closed |
| Project: | Documentation |
| Component/s: | manual, Server |
| Affects Version/s: | mongodb-2.6, mongodb-3.0 |
| Fix Version/s: | Server_Docs_20231030 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | William Byrne III | Assignee: | Emet Ozar |
| Resolution: | Won't Fix | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
||||||||||||||||
| Issue Links: |
|
||||||||||||||||
| Participants: | |||||||||||||||||
| Days since reply: | 1 year, 14 weeks, 2 days ago | ||||||||||||||||
| Epic Link: | DOCSP-1769 | ||||||||||||||||
| Story Points: | 0.25 | ||||||||||||||||
| Description |
|
The $mod conditional operator casts all its inputs into LONG's before executing the modulo calculation and comparison. This truncates any decimal values back to their integer parts, which causing some users to get unexpected results. For example, if db.collName.f1 holds [0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, ... 9.0, 9.5, 10] then these statements:
both return [2.0, 2.5, 7.0, 7.5] because we don't return the documents where ((f1 % 5.5) == 2.5), we return those where ((floor(f1) % floor(5.5)) == floor(2.5)). While I accept that MongoDB does this "integer division" because it stores numbers in binary floating point and floating point calculations are prone to rounding errors, I think that we should make this clearer in the manual entry for $mod (https://docs.mongodb.org/manual/reference/operator/query/mod/). Most users expect that mod will behave the same way as it does in their other databases/applications/programming environments/etc because it is a generic mathematical term. |
| Comments |
| Comment by Education Bot [ 31/Oct/22 ] |
|
Hello! This ticket has been closed due to inactivity. If you believe this ticket is still important, please reopen it and leave a comment to explain why. Thank you! |
| Comment by Andrew Aldridge [ 29/Jan/18 ] |
|
Pending resolution to |
| Comment by William Byrne III [ 04/May/16 ] |
|
I meant to note that in 3.4 we will add support for DECIMAL and $mod will be reworked to use it where appropriate: https://jira.mongodb.org/browse/SERVER-1393 So this DOC change would need to be of the form "up until version 3.2, $mod performs integer division..." |