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

Math.exp() extremely slow for some exponents

XMLWordPrintable

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      The Math.exp() function is extremely slow for some exponents.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the applied test case. Change the base from 500 to 800 and see that the code is 1000x slower.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The time should be roughly the same. It is the same when running a similar C program using the libm library.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.concurrent.TimeUnit;

      public class MathExpTest {
          private static final int BASE = 500; // change to 800 and 1000x slower

          public static void main(String[] args){
              for(int i=0;i<=10;i++) {
                  long start = System.nanoTime();
                  long total = 0;
                  for(int j=0;j<10000000;j++) {
                      total+=testExp(i+BASE);
                  }
                  long diff= TimeUnit.NANOSECONDS.toMicros(System.nanoTime()-start);
                  System.out.printf("time is %f\n",diff/10000000.0);
                  System.out.println("total is "+total);
              }
          }

         static int testExp(int i) {
              return (int) (Math.sqrt(Math.exp(i)));
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      write our own exp function, which is slower in most cases

        1. MathExpTest.java
          2 kB
        2. MathExpTest.c
          0.5 kB
        3. JI9050771.java
          0.6 kB
        4. JDK9ea_181.log
          2 kB
        5. JDK8u144-output.log
          2 kB
        6. JDK8u144-output.log
          2 kB

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

              Created:
              Updated:
              Resolved: