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

floating-point remainder operation produces wrong results

    XMLWordPrintable

Details

    • x86
    • windows_95, windows_2000

    Description

      When calculating z=x%y in a loop shown below, sometimes z>y is produced.
      This is obviously a bug, by the definition of the remainder operation.

      I have reproduced this bug on win32 platform, jdk1.2b4K, and jdk1.2fcsE,
      both with symcjit and without any jit.

      Moreover, while:
          Double.MAX_VALUE % 0.5 produces 2.0927882525703688E298 in a loop,
      the similar expression:
          Double.MAX_VALUE % 0.5 results in a correct value 0.0 when runs separetely.

      JCK test tests/lang/FP/fpl037/fpl03701m2/fpl03701m2.html fails because of this bug.

      ===================================================================

      class hrem {

          double dValue[] = {
      0.0, 1.0, 2.0, 1.0/2, 1.0/3, 2.0/3, 5.0/7, 7.0/5,
      Math.E, Math.PI,
      Double.NaN, Double.MIN_VALUE, Double.MAX_VALUE,
      Double.POSITIVE_INFINITY
          };

          float fValue[] = {
      0f, 1f, 2f, 1f/2, 1f/3, 2f/3, 5f/7, 7f/5,
      (float)Math.E, (float)Math.PI,
      Float.NaN, Float.MIN_VALUE, Float.MAX_VALUE,
      Float.POSITIVE_INFINITY
          };

          public static void main (String args[]) {
      new hrem().run_many_floats();
      new hrem().run_many_doubles();
          };

          void run_many_doubles () {
      println("\nMany doubles:");

      for (int i=0; i<dValue.length; i++)
      for (int j=0; j<dValue.length; j++) {
      double x, y, z;

      x = dValue[i];
      y = dValue[j];
      z = x % y;

      if (z >= y)
      reportDouble("Fail: ",x,y,z);
      };
          };

          void run_many_floats () {
      println("\nMany floats:");

      for (int i=0; i<fValue.length; i++)
      for (int j=0; j<fValue.length; j++) {
      float x, y, z;

      x = fValue[i];
      y = fValue[j];
      z = x % y;

      if (z >= y)
      reportFloat("Fail: ",x,y,z);
      };
          };

          static void reportFloat (String s, float x, float y, float z) {
      System.out.println(s + x + " % " + y + " = " + z);
          };

          static void reportDouble (String s, double x, double y, double z) {
      System.out.println(s + x + " % " + y + " = " + z);
          };

          static void println (Object x) {
      System.out.println( x );
          };
      }

      --------------------------------------------- OUTPUTS:

      Many floats:
      Fail: 3.4028235E38 % 0.33333334 = 9.066944E23
      Fail: 3.4028235E38 % 0.6666667 = 9.066944E23
      Fail: 3.4028235E38 % 0.71428573 = 3.6219266E28
      Fail: 3.4028235E38 % 1.4 = 5.1157109E18
      Fail: 3.4028235E38 % 2.7182817 = 2.6316415E19
      Fail: 3.4028235E38 % 3.1415927 = 5.7866527E19

      Many doubles:
      Fail: 0.3333333333333333 % 4.9E-324 = 2.7755575615628914E-16
      Fail: 0.6666666666666666 % 4.9E-324 = 5.551115123125783E-16
      Fail: 0.7142857142857143 % 4.9E-324 = 7.771561172376096E-16
      Fail: 1.4 % 4.9E-324 = 4.440892098500626E-16
      Fail: 2.718281828459045 % 4.9E-324 = 4.440892098500626E-16
      Fail: 1.7976931348623157E308 % 0.5 = 2.0927882525703688E298
      Fail: 1.7976931348623157E308 % 0.3333333333333333 = 1.3951925010202974E298
      Fail: 1.7976931348623157E308 % 0.6666666666666666 = 2.7903859999607496E298
      Fail: 1.7976931348623157E308 % 0.7142857142857143 = 1.1958777469405876E298
      Fail: 1.7976931348623157E308 % 1.4 = 6.682501036388028E288
      Fail: 1.7976931348623157E308 % 2.718281828459045 = 5.773296883374853E288
      Fail: 1.7976931348623157E308 % 3.141592653589793 = 3.018577802618292E289
      Fail: 1.7976931348623157E308 % 4.9E-324 = 1.3970882166743039E293

      ===================================================================


      Name: yyT116575 Date: 08/24/2001


      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


      bug in modula operator

      System.out.println( 10.0 % 0.1 );

      output:
      0.09999999999999995

      the result should be "0"
      because 0.1 fits exactly 100 times in 10.0
      (Review ID: 130602)
      ======================================================================

      Attachments

        Activity

          People

            collins Gary Collins (Inactive)
            dkhukhrosunw Dmitry Khukhro (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: