Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-32960

$mod has inconsistent rounding/truncation behavior

    XMLWordPrintable

    Details

    • Backwards Compatibility:
      Minor Change
    • Operating System:
      ALL
    • Backport Requested:
      v4.4, v4.2, v4.0, v3.6
    • Sprint:
      Query 2020-11-30
    • Linked BF Score:
      44

      Description

      See DOCS-7792 - $mod will truncate a double (e.g. 2.7 -> 2), but will round decimals, ties to even (e.g. 3.5 -> 4).

      We should be consistent here, probably truncating decimals as well as doubles.

      Also, $mod treats it's divisor and remainder arguments as int values. This could lead to unexpected behaviour when these arguments are larger than int. For example:

      > db.createCollection('test')
      > db.test.insert([

      {a: 4000000000}

      ])
      > db.test.find({a: {$mod: [4000000000, 0]}})

      The last query should return the single record from the test collection, but returns nothing instead.

      The suggestion is to truncate decimals and doubles consistently towards zero into long long type.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              nikita.lapkov Nikita Lapkov
              Reporter:
              charlie.swanson Charlie Swanson
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: