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

divideExact is missing in java.lang.Math

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 9
    • Fix Version/s: 18
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b08
    • CPU:
      x86
    • OS:
      windows_8

      Description

      A DESCRIPTION OF THE REQUEST :
      For instance Integer.MIN_VALUE / -1 will overflow, but there is no method divideExact in java.lang.Math. A standardized divideExact would be most helpful, because it would allow to write programs which use well defined methods and shows the overflow is critical without the need to clutter it with (probable undocumented) if statements for this special case.

      JUSTIFICATION :
      addExact, subtractExact, multiplyExact, incrementExact, decrementExact, and negateExact have been added to java.lang.Math to support arithmetic operations which support overflow detection (even though the documentation doesn't say this for the latter three in the Math documentation). This might suggest that there is no overflow risk with the divide operation. Alone for symmetric reasons the divideExact method should be added.
      Additionally the JVM might be able to optimize this method by using a single processor instruction.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Add the following methods to java.lang.Math

      int divideExact(int, int)
      long divideExact(long, long)


      ACTUAL -
      The requested methods don't exist.

      CUSTOMER SUBMITTED WORKAROUND :
      int x = …, y = …, z;

      if (x == Integer.MIN_VALUE && y == -1) {
          throw new ArithmeticException("integer overflow");
      }

      z = x / y;

      // The other case y == 0 has been omitted, because it is already covered by the JVM
      // (with no special case for 0/0).
      // See also: Henry S. Warren, Jr.:Hacker's Delight (2nd Ed.), Page 34.


        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: