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

Math.pow yields different results upon repeated calls

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 9
    • 7u80, 8u25, 9
    • hotspot
    • b03
    • b48
    • x86_64
    • linux

        FULL PRODUCT VERSION :
        java version "1.8.0_25"
        Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
        Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)


        FULL OS VERSION :
        Linux host 3.13.0-39-generic #66-Ubuntu SMP Tue Oct 28 13:30:27 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

        A DESCRIPTION OF THE PROBLEM :
        The function Math.pow() returns slightly different results upon repeated calls with exactly the same input parameters. It seems as if the implementation (floating point precision) is switched internally at some point during execution.

        A mathematical function must return consistent and reproducible results within one VM instance.

        also see http://stackoverflow.com/questions/26746623/math-pow-yields-different-results-upon-repeated-calls



        THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: No

        THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: Yes

        REGRESSION. Last worked in version 8u11

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the example code given below.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        It should print nothing, but it prints:
        Math.pow()
        i=15573
        x=2.862738293257239E7
        p=2.8627382932572395E7

        REPRODUCIBILITY :
        This bug can be reproduced often.

        ---------- BEGIN SOURCE ----------
        import java.util.function.BiFunction;

        public class PowerTest {

            private static final int N = 1000000;
            private static final double base = 5350.456329377186;
            private static final double exp = 2.0;

            private static double eval(final BiFunction<Double, Double, Double> f) {
                return f.apply(base, exp);
            }

            private void loop(final String s, final BiFunction<Double, Double, Double> f) {
                double x = eval(f);
                for (int i = 0; i < N; i++) {
                    final double p = eval(f);
                    if (x != p) {
                        System.out.println(s + "\ni=" + i + "\nx=" + x + "\np=" + p);
                    }
                    x = p;
                }
            }

            public void mathPow() {
                loop("Math.pow()", Math::pow);
            }

            public void strictMathPow() {
                loop("StrictMath.pow()", StrictMath::pow);
            }

            public static void main(final String[] args) {
                final PowerTest pt = new PowerTest();
                pt.mathPow();
                pt.strictMathPow();
            }
        }

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

              roland Roland Westrelin
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

                Created:
                Updated:
                Resolved: