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

Integer division returns incorrect results in some cases

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 6u14
    • hotspot
    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.6.0_17"
      Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
      Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01, mixed mode)

      FULL OS VERSION :
      Linux greycloud 2.6.31-17-generic #54-Ubuntu SMP Thu Dec 10 17:01:44 UTC 2009 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      See the test code below. Basically, sometimes simple integer multiplication and division fails.

      THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: No

      THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: Yes

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the JavaTest class given below (note that I've pasted two classes in there -- there's also a helper class). It only fails some of the time. Sometimes you have to run it several times (as many as 10) before you see the error.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Expected:
      64 * 64 / 8 == 512

      Actual:
      64 * 64 / 8 == 519
      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No error message, just wrong results.

      REPRODUCIBILITY :
      This bug can be reproduced often.

      ---------- BEGIN SOURCE ----------
      package com.keith;

      import java.io.IOException;

      public class JavaTest
      {
      public static void main(String[] args) throws Exception
      {
      JavaTest macTest = new JavaTest();
      for(long i = 0; i < 100000000l; i++)
      {
      macTest.doStuff();
      }
      }

      private void doStuff() throws IOException {
      JavaTestHelper dbbm = new JavaTestHelper();
      dbbm.doMultiplicationAndDivision();
      }

      }


      package com.keith;

      import java.io.IOException;

      public class JavaTestHelper
      {
      public int dimension = 4096;

      public void doMultiplicationAndDivision() throws IOException
      {
      int length = (this.dimension + 63) / 64;
      int arraySize = length * 64 / 8;
      if(arraySize %2 == 1)
      {
      throw new RuntimeException("bad division!!");
      }
      }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      If I do the division before the multiplication, I don't get the problem. That is, if I do:
        int arraySize = length * (64 / 8);
      instead of:
        int arraySize = length * 64 / 8;
      then the problem doesn't show up.
      But I can't realistically track down and test every place in our application where we do multiplication and division. I basically have zero confidence in our application because of this.

      Release Regression From : 6u13-rev
      The above release value was the last known release where this
      bug was not reproducible. Since then there has been a regression.

      Release Regression From : 6u13-rev
      The above release value was the last known release where this
      bug was not reproducible. Since then there has been a regression.

            kvn Vladimir Kozlov
            dmcox David Cox (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: