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

$mod has inconsistent rounding/truncation behavior

    • Minor Change
    • ALL
    • v4.4, v4.2, v4.0, v3.6
    • Query 2020-11-30
    • 44

      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.

            Assignee:
            nikita.lapkov@mongodb.com Nikita Lapkov (Inactive)
            Reporter:
            charlie.swanson@mongodb.com Charlie Swanson
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: