Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8271601

Math.floorMod(int, int) and Math.floorMod(long, long) differ in their logic

    XMLWordPrintable

Details

    • b11
    • generic
    • generic
    • Verified

    Description

      A DESCRIPTION OF THE PROBLEM :
      The current implementation of Math.floorMod(int, int) reads:

          public static int floorMod(int x, int y) {
              int mod = x % y;
              // if the signs are different and modulo not zero, adjust result
              if ((mod ^ y) < 0 && mod != 0) {
                  mod += y;
              }
              return mod;
          }

      while Math.floorMod(long, long) reads:

          public static int floorMod(long x, long y) {
              long mod = x % y;
              // if the signs are different and modulo not zero, adjust result
              if ((x ^ y) < 0 && mod != 0) {
                  mod += y;
              }
              return mod;
          }

      There's a difference in how the signs are checked.

      Prefer the floorMod(long, long) variant even for floorMod(int, int), that is, rewrite

          public static int floorMod(int x, int y) {
              int mod = x % y;
              // if the signs are different and modulo not zero, adjust result
              if ((x ^ y) < 0 && mod != 0) {
                  mod += y;
              }
              return mod;
          }




      Attachments

        Issue Links

          Activity

            People

              bpb Brian Burkhalter
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: