Average Error: 20.0 → 0.2
Time: 8.7s
Precision: binary64
Cost: 13892
\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
\[\begin{array}{l} \mathbf{if}\;x \leq 95000:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x} \cdot \frac{1}{\sqrt{\frac{1}{x}} \cdot 1.5 + 2 \cdot \sqrt{x}}\\ \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x)
 :precision binary64
 (if (<= x 95000.0)
   (- (pow x -0.5) (pow (+ 1.0 x) -0.5))
   (* (/ 1.0 x) (/ 1.0 (+ (* (sqrt (/ 1.0 x)) 1.5) (* 2.0 (sqrt x)))))))
double code(double x) {
	return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
double code(double x) {
	double tmp;
	if (x <= 95000.0) {
		tmp = pow(x, -0.5) - pow((1.0 + x), -0.5);
	} else {
		tmp = (1.0 / x) * (1.0 / ((sqrt((1.0 / x)) * 1.5) + (2.0 * sqrt(x))));
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 / sqrt(x)) - (1.0d0 / sqrt((x + 1.0d0)))
end function
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= 95000.0d0) then
        tmp = (x ** (-0.5d0)) - ((1.0d0 + x) ** (-0.5d0))
    else
        tmp = (1.0d0 / x) * (1.0d0 / ((sqrt((1.0d0 / x)) * 1.5d0) + (2.0d0 * sqrt(x))))
    end if
    code = tmp
end function
public static double code(double x) {
	return (1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((x + 1.0)));
}
public static double code(double x) {
	double tmp;
	if (x <= 95000.0) {
		tmp = Math.pow(x, -0.5) - Math.pow((1.0 + x), -0.5);
	} else {
		tmp = (1.0 / x) * (1.0 / ((Math.sqrt((1.0 / x)) * 1.5) + (2.0 * Math.sqrt(x))));
	}
	return tmp;
}
def code(x):
	return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
def code(x):
	tmp = 0
	if x <= 95000.0:
		tmp = math.pow(x, -0.5) - math.pow((1.0 + x), -0.5)
	else:
		tmp = (1.0 / x) * (1.0 / ((math.sqrt((1.0 / x)) * 1.5) + (2.0 * math.sqrt(x))))
	return tmp
function code(x)
	return Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0))))
end
function code(x)
	tmp = 0.0
	if (x <= 95000.0)
		tmp = Float64((x ^ -0.5) - (Float64(1.0 + x) ^ -0.5));
	else
		tmp = Float64(Float64(1.0 / x) * Float64(1.0 / Float64(Float64(sqrt(Float64(1.0 / x)) * 1.5) + Float64(2.0 * sqrt(x)))));
	end
	return tmp
end
function tmp = code(x)
	tmp = (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= 95000.0)
		tmp = (x ^ -0.5) - ((1.0 + x) ^ -0.5);
	else
		tmp = (1.0 / x) * (1.0 / ((sqrt((1.0 / x)) * 1.5) + (2.0 * sqrt(x))));
	end
	tmp_2 = tmp;
end
code[x_] := N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_] := If[LessEqual[x, 95000.0], N[(N[Power[x, -0.5], $MachinePrecision] - N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / x), $MachinePrecision] * N[(1.0 / N[(N[(N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision] * 1.5), $MachinePrecision] + N[(2.0 * N[Sqrt[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
\mathbf{if}\;x \leq 95000:\\
\;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{x} \cdot \frac{1}{\sqrt{\frac{1}{x}} \cdot 1.5 + 2 \cdot \sqrt{x}}\\


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original20.0
Target0.7
Herbie0.2
\[\frac{1}{\left(x + 1\right) \cdot \sqrt{x} + x \cdot \sqrt{x + 1}} \]

Derivation

  1. Split input into 2 regimes
  2. if x < 95000

    1. Initial program 0.4

      \[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
    2. Applied egg-rr0.1

      \[\leadsto \color{blue}{\left({x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\right) + \mathsf{fma}\left(-1, {\left(1 + x\right)}^{-0.5}, {\left(1 + x\right)}^{-0.5}\right)} \]
    3. Simplified0.1

      \[\leadsto \color{blue}{{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}} \]
      Proof
      (-.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= +-rgt-identity_binary64 (+.f64 (-.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)) 0)): 0 points increase in error, 0 points decrease in error
      (+.f64 (-.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)) (Rewrite<= mul0-lft_binary64 (*.f64 0 (pow.f64 (+.f64 1 x) -1/2)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (-.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)) (*.f64 (Rewrite<= metadata-eval (+.f64 -1 1)) (pow.f64 (+.f64 1 x) -1/2))): 0 points increase in error, 0 points decrease in error
      (+.f64 (-.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)) (Rewrite<= distribute-lft1-in_binary64 (+.f64 (*.f64 -1 (pow.f64 (+.f64 1 x) -1/2)) (pow.f64 (+.f64 1 x) -1/2)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (-.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)) (Rewrite<= fma-udef_binary64 (fma.f64 -1 (pow.f64 (+.f64 1 x) -1/2) (pow.f64 (+.f64 1 x) -1/2)))): 0 points increase in error, 0 points decrease in error

    if 95000 < x

    1. Initial program 40.4

      \[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
    2. Applied egg-rr40.3

      \[\leadsto \color{blue}{\frac{\frac{1}{x} - \frac{1}{1 + x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}} \]
    3. Applied egg-rr10.0

      \[\leadsto \frac{\color{blue}{\frac{\frac{1 + \left(x - x\right)}{x}}{1 + x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    4. Applied egg-rr0.3

      \[\leadsto \color{blue}{\frac{1}{x} \cdot \frac{1}{\left(1 + x\right) \cdot \left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right)}} \]
    5. Taylor expanded in x around inf 0.3

      \[\leadsto \frac{1}{x} \cdot \frac{1}{\color{blue}{-0.5 \cdot \sqrt{\frac{1}{x}} + \left(2 \cdot \sqrt{x} + 2 \cdot \sqrt{\frac{1}{x}}\right)}} \]
    6. Simplified0.3

      \[\leadsto \frac{1}{x} \cdot \frac{1}{\color{blue}{\sqrt{\frac{1}{x}} \cdot 1.5 + 2 \cdot \sqrt{x}}} \]
      Proof
      (+.f64 (*.f64 (sqrt.f64 (/.f64 1 x)) 3/2) (*.f64 2 (sqrt.f64 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 (sqrt.f64 (/.f64 1 x)) (Rewrite<= metadata-eval (+.f64 -1/2 2))) (*.f64 2 (sqrt.f64 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= distribute-rgt-out_binary64 (+.f64 (*.f64 -1/2 (sqrt.f64 (/.f64 1 x))) (*.f64 2 (sqrt.f64 (/.f64 1 x))))) (*.f64 2 (sqrt.f64 x))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-+r+_binary64 (+.f64 (*.f64 -1/2 (sqrt.f64 (/.f64 1 x))) (+.f64 (*.f64 2 (sqrt.f64 (/.f64 1 x))) (*.f64 2 (sqrt.f64 x))))): 0 points increase in error, 2 points decrease in error
      (+.f64 (*.f64 -1/2 (sqrt.f64 (/.f64 1 x))) (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 2 (sqrt.f64 x)) (*.f64 2 (sqrt.f64 (/.f64 1 x)))))): 0 points increase in error, 0 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification0.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 95000:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x} \cdot \frac{1}{\sqrt{\frac{1}{x}} \cdot 1.5 + 2 \cdot \sqrt{x}}\\ \end{array} \]

Alternatives

Alternative 1
Error0.4
Cost13888
\[\frac{1}{x} \cdot \frac{1}{\left(1 + x\right) \cdot \left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right)} \]
Alternative 2
Error0.3
Cost13380
\[\begin{array}{l} \mathbf{if}\;x \leq 64000000:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-1.5} \cdot 0.5\\ \end{array} \]
Alternative 3
Error1.0
Cost7172
\[\begin{array}{l} \mathbf{if}\;x \leq 1.7:\\ \;\;\;\;{x}^{-0.5} - \frac{1}{1 + x \cdot 0.5}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-1.5} \cdot 0.5\\ \end{array} \]
Alternative 4
Error1.0
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 1:\\ \;\;\;\;\left({x}^{-0.5} + x \cdot 0.5\right) + -1\\ \mathbf{else}:\\ \;\;\;\;{x}^{-1.5} \cdot 0.5\\ \end{array} \]
Alternative 5
Error2.2
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.5:\\ \;\;\;\;{x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-1.5} \cdot 0.5\\ \end{array} \]
Alternative 6
Error1.2
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.7:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;{x}^{-1.5} \cdot 0.5\\ \end{array} \]
Alternative 7
Error31.7
Cost6528
\[{x}^{-0.5} \]
Alternative 8
Error61.5
Cost192
\[x \cdot 0.5 \]
Alternative 9
Error62.8
Cost64
\[-1 \]

Error

Reproduce

herbie shell --seed 2022332 
(FPCore (x)
  :name "2isqrt (example 3.6)"
  :precision binary64

  :herbie-target
  (/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0)))))

  (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))