2sqrt (example 3.1)

Percentage Accurate: 6.8% → 99.1%
Time: 7.5s
Alternatives: 6
Speedup: 1.2×

Specification

?
\[x > 1 \land x < 10^{+308}\]
\[\begin{array}{l} \\ \sqrt{x + 1} - \sqrt{x} \end{array} \]
(FPCore (x) :precision binary64 (- (sqrt (+ x 1.0)) (sqrt x)))
double code(double x) {
	return sqrt((x + 1.0)) - sqrt(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = sqrt((x + 1.0d0)) - sqrt(x)
end function
public static double code(double x) {
	return Math.sqrt((x + 1.0)) - Math.sqrt(x);
}
def code(x):
	return math.sqrt((x + 1.0)) - math.sqrt(x)
function code(x)
	return Float64(sqrt(Float64(x + 1.0)) - sqrt(x))
end
function tmp = code(x)
	tmp = sqrt((x + 1.0)) - sqrt(x);
end
code[x_] := N[(N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\sqrt{x + 1} - \sqrt{x}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 6 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 6.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{x + 1} - \sqrt{x} \end{array} \]
(FPCore (x) :precision binary64 (- (sqrt (+ x 1.0)) (sqrt x)))
double code(double x) {
	return sqrt((x + 1.0)) - sqrt(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = sqrt((x + 1.0d0)) - sqrt(x)
end function
public static double code(double x) {
	return Math.sqrt((x + 1.0)) - Math.sqrt(x);
}
def code(x):
	return math.sqrt((x + 1.0)) - math.sqrt(x)
function code(x)
	return Float64(sqrt(Float64(x + 1.0)) - sqrt(x))
end
function tmp = code(x)
	tmp = sqrt((x + 1.0)) - sqrt(x);
end
code[x_] := N[(N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\sqrt{x + 1} - \sqrt{x}
\end{array}

Alternative 1: 99.1% accurate, 0.0× speedup?

\[\begin{array}{l} \\ \frac{\mathsf{fma}\left(\sqrt{{\left({x}^{5}\right)}^{-1}}, -0.0390625, \mathsf{fma}\left(\sqrt{{\left({x}^{3}\right)}^{-1}}, 0.0625, \mathsf{fma}\left(\sqrt{{x}^{-1}}, -0.125, 0.5 \cdot \sqrt{x}\right)\right)\right)}{x} \end{array} \]
(FPCore (x)
 :precision binary64
 (/
  (fma
   (sqrt (pow (pow x 5.0) -1.0))
   -0.0390625
   (fma
    (sqrt (pow (pow x 3.0) -1.0))
    0.0625
    (fma (sqrt (pow x -1.0)) -0.125 (* 0.5 (sqrt x)))))
  x))
double code(double x) {
	return fma(sqrt(pow(pow(x, 5.0), -1.0)), -0.0390625, fma(sqrt(pow(pow(x, 3.0), -1.0)), 0.0625, fma(sqrt(pow(x, -1.0)), -0.125, (0.5 * sqrt(x))))) / x;
}
function code(x)
	return Float64(fma(sqrt(((x ^ 5.0) ^ -1.0)), -0.0390625, fma(sqrt(((x ^ 3.0) ^ -1.0)), 0.0625, fma(sqrt((x ^ -1.0)), -0.125, Float64(0.5 * sqrt(x))))) / x)
end
code[x_] := N[(N[(N[Sqrt[N[Power[N[Power[x, 5.0], $MachinePrecision], -1.0], $MachinePrecision]], $MachinePrecision] * -0.0390625 + N[(N[Sqrt[N[Power[N[Power[x, 3.0], $MachinePrecision], -1.0], $MachinePrecision]], $MachinePrecision] * 0.0625 + N[(N[Sqrt[N[Power[x, -1.0], $MachinePrecision]], $MachinePrecision] * -0.125 + N[(0.5 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}

\\
\frac{\mathsf{fma}\left(\sqrt{{\left({x}^{5}\right)}^{-1}}, -0.0390625, \mathsf{fma}\left(\sqrt{{\left({x}^{3}\right)}^{-1}}, 0.0625, \mathsf{fma}\left(\sqrt{{x}^{-1}}, -0.125, 0.5 \cdot \sqrt{x}\right)\right)\right)}{x}
\end{array}
Derivation
  1. Initial program 6.8%

    \[\sqrt{x + 1} - \sqrt{x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf

    \[\leadsto \color{blue}{\frac{\frac{-1}{8} \cdot \sqrt{\frac{1}{x}} + \left(\frac{-5}{128} \cdot \sqrt{\frac{1}{{x}^{5}}} + \left(\frac{1}{16} \cdot \sqrt{\frac{1}{{x}^{3}}} + \frac{1}{2} \cdot \sqrt{x}\right)\right)}{x}} \]
  4. Step-by-step derivation
    1. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{\frac{-1}{8} \cdot \sqrt{\frac{1}{x}} + \left(\frac{-5}{128} \cdot \sqrt{\frac{1}{{x}^{5}}} + \left(\frac{1}{16} \cdot \sqrt{\frac{1}{{x}^{3}}} + \frac{1}{2} \cdot \sqrt{x}\right)\right)}{x}} \]
  5. Applied rewrites99.0%

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\sqrt{\frac{1}{{x}^{5}}}, -0.0390625, \mathsf{fma}\left(\sqrt{\frac{1}{{x}^{3}}}, 0.0625, \mathsf{fma}\left(\sqrt{\frac{1}{x}}, -0.125, 0.5 \cdot \sqrt{x}\right)\right)\right)}{x}} \]
  6. Final simplification99.0%

    \[\leadsto \frac{\mathsf{fma}\left(\sqrt{{\left({x}^{5}\right)}^{-1}}, -0.0390625, \mathsf{fma}\left(\sqrt{{\left({x}^{3}\right)}^{-1}}, 0.0625, \mathsf{fma}\left(\sqrt{{x}^{-1}}, -0.125, 0.5 \cdot \sqrt{x}\right)\right)\right)}{x} \]
  7. Add Preprocessing

Alternative 2: 97.9% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \sqrt{{x}^{-1}} \cdot 0.5 \end{array} \]
(FPCore (x) :precision binary64 (* (sqrt (pow x -1.0)) 0.5))
double code(double x) {
	return sqrt(pow(x, -1.0)) * 0.5;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = sqrt((x ** (-1.0d0))) * 0.5d0
end function
public static double code(double x) {
	return Math.sqrt(Math.pow(x, -1.0)) * 0.5;
}
def code(x):
	return math.sqrt(math.pow(x, -1.0)) * 0.5
function code(x)
	return Float64(sqrt((x ^ -1.0)) * 0.5)
end
function tmp = code(x)
	tmp = sqrt((x ^ -1.0)) * 0.5;
end
code[x_] := N[(N[Sqrt[N[Power[x, -1.0], $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]
\begin{array}{l}

\\
\sqrt{{x}^{-1}} \cdot 0.5
\end{array}
Derivation
  1. Initial program 6.8%

    \[\sqrt{x + 1} - \sqrt{x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf

    \[\leadsto \color{blue}{\frac{1}{2} \cdot \sqrt{\frac{1}{x}}} \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \color{blue}{\sqrt{\frac{1}{x}} \cdot \frac{1}{2}} \]
    2. lower-*.f64N/A

      \[\leadsto \color{blue}{\sqrt{\frac{1}{x}} \cdot \frac{1}{2}} \]
    3. lower-sqrt.f64N/A

      \[\leadsto \color{blue}{\sqrt{\frac{1}{x}}} \cdot \frac{1}{2} \]
    4. lower-/.f6497.9

      \[\leadsto \sqrt{\color{blue}{\frac{1}{x}}} \cdot 0.5 \]
  5. Applied rewrites97.9%

    \[\leadsto \color{blue}{\sqrt{\frac{1}{x}} \cdot 0.5} \]
  6. Final simplification97.9%

    \[\leadsto \sqrt{{x}^{-1}} \cdot 0.5 \]
  7. Add Preprocessing

Alternative 3: 99.0% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \frac{\mathsf{fma}\left(\sqrt{x}, \frac{0.0625}{x \cdot x}, \mathsf{fma}\left(0.5, \sqrt{x}, \frac{-0.125}{\sqrt{x}}\right)\right)}{x} \end{array} \]
(FPCore (x)
 :precision binary64
 (/
  (fma (sqrt x) (/ 0.0625 (* x x)) (fma 0.5 (sqrt x) (/ -0.125 (sqrt x))))
  x))
double code(double x) {
	return fma(sqrt(x), (0.0625 / (x * x)), fma(0.5, sqrt(x), (-0.125 / sqrt(x)))) / x;
}
function code(x)
	return Float64(fma(sqrt(x), Float64(0.0625 / Float64(x * x)), fma(0.5, sqrt(x), Float64(-0.125 / sqrt(x)))) / x)
end
code[x_] := N[(N[(N[Sqrt[x], $MachinePrecision] * N[(0.0625 / N[(x * x), $MachinePrecision]), $MachinePrecision] + N[(0.5 * N[Sqrt[x], $MachinePrecision] + N[(-0.125 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}

\\
\frac{\mathsf{fma}\left(\sqrt{x}, \frac{0.0625}{x \cdot x}, \mathsf{fma}\left(0.5, \sqrt{x}, \frac{-0.125}{\sqrt{x}}\right)\right)}{x}
\end{array}
Derivation
  1. Initial program 6.8%

    \[\sqrt{x + 1} - \sqrt{x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf

    \[\leadsto \color{blue}{\frac{\frac{-1}{8} \cdot \sqrt{\frac{1}{x}} + \left(\frac{1}{16} \cdot \sqrt{\frac{1}{{x}^{3}}} + \frac{1}{2} \cdot \sqrt{x}\right)}{x}} \]
  4. Step-by-step derivation
    1. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{\frac{-1}{8} \cdot \sqrt{\frac{1}{x}} + \left(\frac{1}{16} \cdot \sqrt{\frac{1}{{x}^{3}}} + \frac{1}{2} \cdot \sqrt{x}\right)}{x}} \]
  5. Applied rewrites98.8%

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\sqrt{\frac{1}{{x}^{3}}}, 0.0625, \mathsf{fma}\left(\sqrt{\frac{1}{x}}, -0.125, 0.5 \cdot \sqrt{x}\right)\right)}{x}} \]
  6. Step-by-step derivation
    1. Applied rewrites98.8%

      \[\leadsto \frac{\mathsf{fma}\left(\frac{\sqrt{x}}{x \cdot x}, 0.0625, \mathsf{fma}\left(\sqrt{\frac{1}{x}}, -0.125, 0.5 \cdot \sqrt{x}\right)\right)}{x} \]
    2. Step-by-step derivation
      1. Applied rewrites98.8%

        \[\leadsto \frac{\mathsf{fma}\left(\sqrt{x}, \frac{0.0625}{x \cdot x}, \mathsf{fma}\left(0.5, \sqrt{x}, \frac{-0.125}{\sqrt{x}}\right)\right)}{x} \]
      2. Add Preprocessing

      Alternative 4: 98.6% accurate, 0.6× speedup?

      \[\begin{array}{l} \\ \frac{\mathsf{fma}\left(\sqrt{x}, 0.5, \frac{-0.125}{\sqrt{x}}\right)}{x} \end{array} \]
      (FPCore (x) :precision binary64 (/ (fma (sqrt x) 0.5 (/ -0.125 (sqrt x))) x))
      double code(double x) {
      	return fma(sqrt(x), 0.5, (-0.125 / sqrt(x))) / x;
      }
      
      function code(x)
      	return Float64(fma(sqrt(x), 0.5, Float64(-0.125 / sqrt(x))) / x)
      end
      
      code[x_] := N[(N[(N[Sqrt[x], $MachinePrecision] * 0.5 + N[(-0.125 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      \frac{\mathsf{fma}\left(\sqrt{x}, 0.5, \frac{-0.125}{\sqrt{x}}\right)}{x}
      \end{array}
      
      Derivation
      1. Initial program 6.8%

        \[\sqrt{x + 1} - \sqrt{x} \]
      2. Add Preprocessing
      3. Taylor expanded in x around inf

        \[\leadsto \color{blue}{\frac{\frac{-1}{8} \cdot \sqrt{\frac{1}{x}} + \frac{1}{2} \cdot \sqrt{x}}{x}} \]
      4. Step-by-step derivation
        1. lower-/.f64N/A

          \[\leadsto \color{blue}{\frac{\frac{-1}{8} \cdot \sqrt{\frac{1}{x}} + \frac{1}{2} \cdot \sqrt{x}}{x}} \]
        2. *-commutativeN/A

          \[\leadsto \frac{\color{blue}{\sqrt{\frac{1}{x}} \cdot \frac{-1}{8}} + \frac{1}{2} \cdot \sqrt{x}}{x} \]
        3. lower-fma.f64N/A

          \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\sqrt{\frac{1}{x}}, \frac{-1}{8}, \frac{1}{2} \cdot \sqrt{x}\right)}}{x} \]
        4. lower-sqrt.f64N/A

          \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\sqrt{\frac{1}{x}}}, \frac{-1}{8}, \frac{1}{2} \cdot \sqrt{x}\right)}{x} \]
        5. lower-/.f64N/A

          \[\leadsto \frac{\mathsf{fma}\left(\sqrt{\color{blue}{\frac{1}{x}}}, \frac{-1}{8}, \frac{1}{2} \cdot \sqrt{x}\right)}{x} \]
        6. lower-*.f64N/A

          \[\leadsto \frac{\mathsf{fma}\left(\sqrt{\frac{1}{x}}, \frac{-1}{8}, \color{blue}{\frac{1}{2} \cdot \sqrt{x}}\right)}{x} \]
        7. lower-sqrt.f6498.5

          \[\leadsto \frac{\mathsf{fma}\left(\sqrt{\frac{1}{x}}, -0.125, 0.5 \cdot \color{blue}{\sqrt{x}}\right)}{x} \]
      5. Applied rewrites98.5%

        \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\sqrt{\frac{1}{x}}, -0.125, 0.5 \cdot \sqrt{x}\right)}{x}} \]
      6. Step-by-step derivation
        1. Applied rewrites98.5%

          \[\leadsto \frac{\mathsf{fma}\left(\sqrt{x}, 0.5, \frac{-0.125}{\sqrt{x}}\right)}{\color{blue}{x}} \]
        2. Add Preprocessing

        Alternative 5: 97.7% accurate, 1.2× speedup?

        \[\begin{array}{l} \\ \frac{0.5}{\sqrt{x}} \end{array} \]
        (FPCore (x) :precision binary64 (/ 0.5 (sqrt x)))
        double code(double x) {
        	return 0.5 / sqrt(x);
        }
        
        real(8) function code(x)
            real(8), intent (in) :: x
            code = 0.5d0 / sqrt(x)
        end function
        
        public static double code(double x) {
        	return 0.5 / Math.sqrt(x);
        }
        
        def code(x):
        	return 0.5 / math.sqrt(x)
        
        function code(x)
        	return Float64(0.5 / sqrt(x))
        end
        
        function tmp = code(x)
        	tmp = 0.5 / sqrt(x);
        end
        
        code[x_] := N[(0.5 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
        
        \begin{array}{l}
        
        \\
        \frac{0.5}{\sqrt{x}}
        \end{array}
        
        Derivation
        1. Initial program 6.8%

          \[\sqrt{x + 1} - \sqrt{x} \]
        2. Add Preprocessing
        3. Taylor expanded in x around inf

          \[\leadsto \color{blue}{\frac{1}{2} \cdot \sqrt{\frac{1}{x}}} \]
        4. Step-by-step derivation
          1. *-commutativeN/A

            \[\leadsto \color{blue}{\sqrt{\frac{1}{x}} \cdot \frac{1}{2}} \]
          2. lower-*.f64N/A

            \[\leadsto \color{blue}{\sqrt{\frac{1}{x}} \cdot \frac{1}{2}} \]
          3. lower-sqrt.f64N/A

            \[\leadsto \color{blue}{\sqrt{\frac{1}{x}}} \cdot \frac{1}{2} \]
          4. lower-/.f6497.9

            \[\leadsto \sqrt{\color{blue}{\frac{1}{x}}} \cdot 0.5 \]
        5. Applied rewrites97.9%

          \[\leadsto \color{blue}{\sqrt{\frac{1}{x}} \cdot 0.5} \]
        6. Step-by-step derivation
          1. Applied rewrites97.8%

            \[\leadsto \color{blue}{\frac{0.5}{\sqrt{x}}} \]
          2. Add Preprocessing

          Alternative 6: 5.3% accurate, 2.5× speedup?

          \[\begin{array}{l} \\ \sqrt{x} \end{array} \]
          (FPCore (x) :precision binary64 (sqrt x))
          double code(double x) {
          	return sqrt(x);
          }
          
          real(8) function code(x)
              real(8), intent (in) :: x
              code = sqrt(x)
          end function
          
          public static double code(double x) {
          	return Math.sqrt(x);
          }
          
          def code(x):
          	return math.sqrt(x)
          
          function code(x)
          	return sqrt(x)
          end
          
          function tmp = code(x)
          	tmp = sqrt(x);
          end
          
          code[x_] := N[Sqrt[x], $MachinePrecision]
          
          \begin{array}{l}
          
          \\
          \sqrt{x}
          \end{array}
          
          Derivation
          1. Initial program 6.8%

            \[\sqrt{x + 1} - \sqrt{x} \]
          2. Add Preprocessing
          3. Taylor expanded in x around 0

            \[\leadsto \color{blue}{1} - \sqrt{x} \]
          4. Step-by-step derivation
            1. Applied rewrites1.6%

              \[\leadsto \color{blue}{1} - \sqrt{x} \]
            2. Step-by-step derivation
              1. rem-square-sqrtN/A

                \[\leadsto 1 - \color{blue}{\sqrt{\sqrt{x}} \cdot \sqrt{\sqrt{x}}} \]
              2. sqrt-prodN/A

                \[\leadsto 1 - \color{blue}{\sqrt{\sqrt{x} \cdot \sqrt{x}}} \]
              3. sqr-neg-revN/A

                \[\leadsto 1 - \sqrt{\color{blue}{\left(\mathsf{neg}\left(\sqrt{x}\right)\right) \cdot \left(\mathsf{neg}\left(\sqrt{x}\right)\right)}} \]
              4. sqrt-prodN/A

                \[\leadsto 1 - \color{blue}{\sqrt{\mathsf{neg}\left(\sqrt{x}\right)} \cdot \sqrt{\mathsf{neg}\left(\sqrt{x}\right)}} \]
              5. rem-square-sqrtN/A

                \[\leadsto 1 - \color{blue}{\left(\mathsf{neg}\left(\sqrt{x}\right)\right)} \]
              6. lower-neg.f645.4

                \[\leadsto 1 - \color{blue}{\left(-\sqrt{x}\right)} \]
            3. Applied rewrites5.4%

              \[\leadsto 1 - \color{blue}{\left(-\sqrt{x}\right)} \]
            4. Taylor expanded in x around 0

              \[\leadsto \color{blue}{1 + \sqrt{x}} \]
            5. Step-by-step derivation
              1. +-commutativeN/A

                \[\leadsto \color{blue}{\sqrt{x} + 1} \]
              2. lower-+.f64N/A

                \[\leadsto \color{blue}{\sqrt{x} + 1} \]
              3. lower-sqrt.f645.4

                \[\leadsto \color{blue}{\sqrt{x}} + 1 \]
            6. Applied rewrites5.4%

              \[\leadsto \color{blue}{\sqrt{x} + 1} \]
            7. Taylor expanded in x around -inf

              \[\leadsto -1 \cdot \color{blue}{\left(\sqrt{x} \cdot {\left(\sqrt{-1}\right)}^{2}\right)} \]
            8. Step-by-step derivation
              1. Applied rewrites5.4%

                \[\leadsto \sqrt{x} \]
              2. Add Preprocessing

              Developer Target 1: 98.1% accurate, 0.3× speedup?

              \[\begin{array}{l} \\ 0.5 \cdot {x}^{-0.5} \end{array} \]
              (FPCore (x) :precision binary64 (* 0.5 (pow x -0.5)))
              double code(double x) {
              	return 0.5 * pow(x, -0.5);
              }
              
              real(8) function code(x)
                  real(8), intent (in) :: x
                  code = 0.5d0 * (x ** (-0.5d0))
              end function
              
              public static double code(double x) {
              	return 0.5 * Math.pow(x, -0.5);
              }
              
              def code(x):
              	return 0.5 * math.pow(x, -0.5)
              
              function code(x)
              	return Float64(0.5 * (x ^ -0.5))
              end
              
              function tmp = code(x)
              	tmp = 0.5 * (x ^ -0.5);
              end
              
              code[x_] := N[(0.5 * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision]
              
              \begin{array}{l}
              
              \\
              0.5 \cdot {x}^{-0.5}
              \end{array}
              

              Reproduce

              ?
              herbie shell --seed 2024337 
              (FPCore (x)
                :name "2sqrt (example 3.1)"
                :precision binary64
                :pre (and (> x 1.0) (< x 1e+308))
              
                :alt
                (! :herbie-platform default (* 1/2 (pow x -1/2)))
              
                (- (sqrt (+ x 1.0)) (sqrt x)))