2sqrt (example 3.1)

Percentage Accurate: 6.8% → 99.3%
Time: 7.2s
Alternatives: 8
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);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    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 8 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);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    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.3% accurate, 0.0× speedup?

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

\\
\begin{array}{l}
t_0 := \sqrt{{x}^{-1}}\\
\mathsf{fma}\left(t\_0, 0.5, \frac{\mathsf{fma}\left(t\_0, 0.125, \frac{\mathsf{fma}\left(0.0390625, \sqrt{{\left({x}^{3}\right)}^{-1}}, -0.0625 \cdot t\_0\right)}{x}\right)}{-x}\right)
\end{array}
\end{array}
Derivation
  1. Initial program 7.0%

    \[\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 rewrites98.9%

    \[\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. Step-by-step derivation
    1. Applied rewrites98.9%

      \[\leadsto \frac{\mathsf{fma}\left({x}^{-2.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} \]
    2. Applied rewrites98.9%

      \[\leadsto \mathsf{fma}\left(-0.0390625, \color{blue}{{x}^{-3.5}}, \frac{\mathsf{fma}\left(\sqrt{x}, 0.5, \mathsf{fma}\left(0.0625, {x}^{-1.5}, \frac{-0.125}{\sqrt{x}}\right)\right)}{x}\right) \]
    3. Taylor expanded in x around -inf

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

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

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

    Alternative 2: 98.9% accurate, 0.1× speedup?

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

      \[\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 rewrites98.9%

      \[\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. Taylor expanded in x around inf

      \[\leadsto \frac{-1}{8} \cdot \sqrt{\frac{1}{{x}^{3}}} + \color{blue}{\frac{1}{2} \cdot \sqrt{\frac{1}{x}}} \]
    7. Step-by-step derivation
      1. Applied rewrites98.7%

        \[\leadsto \mathsf{fma}\left(\sqrt{\frac{1}{x}}, \color{blue}{0.5}, \sqrt{\frac{1}{{x}^{3}}} \cdot -0.125\right) \]
      2. Final simplification98.7%

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

      Alternative 3: 99.0% accurate, 0.2× speedup?

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

        \[\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.7%

        \[\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.7%

          \[\leadsto \frac{\mathsf{fma}\left({x}^{-1.5}, 0.0625, \mathsf{fma}\left(\sqrt{\frac{1}{x}}, -0.125, 0.5 \cdot \sqrt{x}\right)\right)}{x} \]
        2. Applied rewrites98.7%

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

        Alternative 4: 99.0% accurate, 0.2× speedup?

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

          \[\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.7%

          \[\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.7%

            \[\leadsto \frac{\mathsf{fma}\left({x}^{-1.5}, 0.0625, \mathsf{fma}\left(\sqrt{\frac{1}{x}}, -0.125, 0.5 \cdot \sqrt{x}\right)\right)}{x} \]
          2. Applied rewrites98.7%

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

          Alternative 5: 97.8% 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;
          }
          
          module fmin_fmax_functions
              implicit none
              private
              public fmax
              public fmin
          
              interface fmax
                  module procedure fmax88
                  module procedure fmax44
                  module procedure fmax84
                  module procedure fmax48
              end interface
              interface fmin
                  module procedure fmin88
                  module procedure fmin44
                  module procedure fmin84
                  module procedure fmin48
              end interface
          contains
              real(8) function fmax88(x, y) result (res)
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
              end function
              real(4) function fmax44(x, y) result (res)
                  real(4), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
              end function
              real(8) function fmax84(x, y) result(res)
                  real(8), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
              end function
              real(8) function fmax48(x, y) result(res)
                  real(4), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
              end function
              real(8) function fmin88(x, y) result (res)
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
              end function
              real(4) function fmin44(x, y) result (res)
                  real(4), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
              end function
              real(8) function fmin84(x, y) result(res)
                  real(8), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
              end function
              real(8) function fmin48(x, y) result(res)
                  real(4), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
              end function
          end module
          
          real(8) function code(x)
          use fmin_fmax_functions
              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 7.0%

            \[\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.7

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

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

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

          Alternative 6: 98.7% 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 7.0%

            \[\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)}{x} \]
            2. Add Preprocessing

            Alternative 7: 97.6% 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);
            }
            
            module fmin_fmax_functions
                implicit none
                private
                public fmax
                public fmin
            
                interface fmax
                    module procedure fmax88
                    module procedure fmax44
                    module procedure fmax84
                    module procedure fmax48
                end interface
                interface fmin
                    module procedure fmin88
                    module procedure fmin44
                    module procedure fmin84
                    module procedure fmin48
                end interface
            contains
                real(8) function fmax88(x, y) result (res)
                    real(8), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                end function
                real(4) function fmax44(x, y) result (res)
                    real(4), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                end function
                real(8) function fmax84(x, y) result(res)
                    real(8), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                end function
                real(8) function fmax48(x, y) result(res)
                    real(4), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                end function
                real(8) function fmin88(x, y) result (res)
                    real(8), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                end function
                real(4) function fmin44(x, y) result (res)
                    real(4), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                end function
                real(8) function fmin84(x, y) result(res)
                    real(8), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                end function
                real(8) function fmin48(x, y) result(res)
                    real(4), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                end function
            end module
            
            real(8) function code(x)
            use fmin_fmax_functions
                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 7.0%

              \[\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.7%

              \[\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. Applied rewrites97.4%

              \[\leadsto \frac{\frac{\mathsf{fma}\left(\sqrt{x}, 0.5, \mathsf{fma}\left(0.0625, {x}^{-1.5}, \frac{0.125}{\sqrt{x}}\right)\right)}{\sqrt{x}}}{\color{blue}{\sqrt{x}}} \]
            7. Taylor expanded in x around inf

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

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

              Alternative 8: 5.4% accurate, 2.5× speedup?

              \[\begin{array}{l} \\ \sqrt{x} \end{array} \]
              (FPCore (x) :precision binary64 (sqrt x))
              double code(double x) {
              	return sqrt(x);
              }
              
              module fmin_fmax_functions
                  implicit none
                  private
                  public fmax
                  public fmin
              
                  interface fmax
                      module procedure fmax88
                      module procedure fmax44
                      module procedure fmax84
                      module procedure fmax48
                  end interface
                  interface fmin
                      module procedure fmin88
                      module procedure fmin44
                      module procedure fmin84
                      module procedure fmin48
                  end interface
              contains
                  real(8) function fmax88(x, y) result (res)
                      real(8), intent (in) :: x
                      real(8), intent (in) :: y
                      res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                  end function
                  real(4) function fmax44(x, y) result (res)
                      real(4), intent (in) :: x
                      real(4), intent (in) :: y
                      res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                  end function
                  real(8) function fmax84(x, y) result(res)
                      real(8), intent (in) :: x
                      real(4), intent (in) :: y
                      res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                  end function
                  real(8) function fmax48(x, y) result(res)
                      real(4), intent (in) :: x
                      real(8), intent (in) :: y
                      res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                  end function
                  real(8) function fmin88(x, y) result (res)
                      real(8), intent (in) :: x
                      real(8), intent (in) :: y
                      res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                  end function
                  real(4) function fmin44(x, y) result (res)
                      real(4), intent (in) :: x
                      real(4), intent (in) :: y
                      res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                  end function
                  real(8) function fmin84(x, y) result(res)
                      real(8), intent (in) :: x
                      real(4), intent (in) :: y
                      res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                  end function
                  real(8) function fmin48(x, y) result(res)
                      real(4), intent (in) :: x
                      real(8), intent (in) :: y
                      res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                  end function
              end module
              
              real(8) function code(x)
              use fmin_fmax_functions
                  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 7.0%

                \[\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.0% 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);
                  }
                  
                  module fmin_fmax_functions
                      implicit none
                      private
                      public fmax
                      public fmin
                  
                      interface fmax
                          module procedure fmax88
                          module procedure fmax44
                          module procedure fmax84
                          module procedure fmax48
                      end interface
                      interface fmin
                          module procedure fmin88
                          module procedure fmin44
                          module procedure fmin84
                          module procedure fmin48
                      end interface
                  contains
                      real(8) function fmax88(x, y) result (res)
                          real(8), intent (in) :: x
                          real(8), intent (in) :: y
                          res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                      end function
                      real(4) function fmax44(x, y) result (res)
                          real(4), intent (in) :: x
                          real(4), intent (in) :: y
                          res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                      end function
                      real(8) function fmax84(x, y) result(res)
                          real(8), intent (in) :: x
                          real(4), intent (in) :: y
                          res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                      end function
                      real(8) function fmax48(x, y) result(res)
                          real(4), intent (in) :: x
                          real(8), intent (in) :: y
                          res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                      end function
                      real(8) function fmin88(x, y) result (res)
                          real(8), intent (in) :: x
                          real(8), intent (in) :: y
                          res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                      end function
                      real(4) function fmin44(x, y) result (res)
                          real(4), intent (in) :: x
                          real(4), intent (in) :: y
                          res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                      end function
                      real(8) function fmin84(x, y) result(res)
                          real(8), intent (in) :: x
                          real(4), intent (in) :: y
                          res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                      end function
                      real(8) function fmin48(x, y) result(res)
                          real(4), intent (in) :: x
                          real(8), intent (in) :: y
                          res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                      end function
                  end module
                  
                  real(8) function code(x)
                  use fmin_fmax_functions
                      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 2024357 
                  (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)))