Average Error: 19.6 → 0.6
Time: 10.5s
Precision: binary64
Cost: 26692
\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
\[\begin{array}{l} \mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 10^{-22}:\\ \;\;\;\;{x}^{-1.5} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x)
 :precision binary64
 (if (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 (sqrt (+ 1.0 x)))) 1e-22)
   (* (pow x -1.5) 0.5)
   (- (pow x -0.5) (pow (+ 1.0 x) -0.5))))
double code(double x) {
	return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
double code(double x) {
	double tmp;
	if (((1.0 / sqrt(x)) + (-1.0 / sqrt((1.0 + x)))) <= 1e-22) {
		tmp = pow(x, -1.5) * 0.5;
	} else {
		tmp = pow(x, -0.5) - pow((1.0 + x), -0.5);
	}
	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 (((1.0d0 / sqrt(x)) + ((-1.0d0) / sqrt((1.0d0 + x)))) <= 1d-22) then
        tmp = (x ** (-1.5d0)) * 0.5d0
    else
        tmp = (x ** (-0.5d0)) - ((1.0d0 + x) ** (-0.5d0))
    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 (((1.0 / Math.sqrt(x)) + (-1.0 / Math.sqrt((1.0 + x)))) <= 1e-22) {
		tmp = Math.pow(x, -1.5) * 0.5;
	} else {
		tmp = Math.pow(x, -0.5) - Math.pow((1.0 + x), -0.5);
	}
	return tmp;
}
def code(x):
	return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
def code(x):
	tmp = 0
	if ((1.0 / math.sqrt(x)) + (-1.0 / math.sqrt((1.0 + x)))) <= 1e-22:
		tmp = math.pow(x, -1.5) * 0.5
	else:
		tmp = math.pow(x, -0.5) - math.pow((1.0 + x), -0.5)
	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 (Float64(Float64(1.0 / sqrt(x)) + Float64(-1.0 / sqrt(Float64(1.0 + x)))) <= 1e-22)
		tmp = Float64((x ^ -1.5) * 0.5);
	else
		tmp = Float64((x ^ -0.5) - (Float64(1.0 + x) ^ -0.5));
	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 (((1.0 / sqrt(x)) + (-1.0 / sqrt((1.0 + x)))) <= 1e-22)
		tmp = (x ^ -1.5) * 0.5;
	else
		tmp = (x ^ -0.5) - ((1.0 + x) ^ -0.5);
	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[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1e-22], N[(N[Power[x, -1.5], $MachinePrecision] * 0.5), $MachinePrecision], N[(N[Power[x, -0.5], $MachinePrecision] - N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 10^{-22}:\\
\;\;\;\;{x}^{-1.5} \cdot 0.5\\

\mathbf{else}:\\
\;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original19.6
Target0.6
Herbie0.6
\[\frac{1}{\left(x + 1\right) \cdot \sqrt{x} + x \cdot \sqrt{x + 1}} \]

Derivation

  1. Split input into 2 regimes
  2. if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 1e-22

    1. Initial program 39.9

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

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

      \[\leadsto \color{blue}{\frac{0.5}{e^{-0.5 \cdot \left(-1 \cdot \log \left(\frac{-1}{x}\right) + \log -1\right)} \cdot {x}^{2}}} \]
    4. Simplified10.9

      \[\leadsto \color{blue}{\frac{{\left(\frac{-1}{\frac{-1}{x}}\right)}^{0.5}}{\frac{x \cdot x}{0.5}}} \]
      Proof
      (/.f64 (pow.f64 (/.f64 -1 (/.f64 -1 x)) 1/2) (/.f64 (*.f64 x x) 1/2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (pow.f64 (/.f64 (Rewrite<= rem-exp-log_binary64 (exp.f64 (log.f64 -1))) (/.f64 -1 x)) 1/2) (/.f64 (*.f64 x x) 1/2)): 198 points increase in error, 0 points decrease in error
      (/.f64 (pow.f64 (/.f64 (exp.f64 (log.f64 -1)) (Rewrite<= rem-exp-log_binary64 (exp.f64 (log.f64 (/.f64 -1 x))))) 1/2) (/.f64 (*.f64 x x) 1/2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (pow.f64 (Rewrite<= exp-diff_binary64 (exp.f64 (-.f64 (log.f64 -1) (log.f64 (/.f64 -1 x))))) 1/2) (/.f64 (*.f64 x x) 1/2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (pow.f64 (exp.f64 (Rewrite<= unsub-neg_binary64 (+.f64 (log.f64 -1) (neg.f64 (log.f64 (/.f64 -1 x)))))) 1/2) (/.f64 (*.f64 x x) 1/2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (pow.f64 (exp.f64 (+.f64 (log.f64 -1) (Rewrite<= mul-1-neg_binary64 (*.f64 -1 (log.f64 (/.f64 -1 x)))))) 1/2) (/.f64 (*.f64 x x) 1/2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (pow.f64 (exp.f64 (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)))) 1/2) (/.f64 (*.f64 x x) 1/2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (Rewrite<= exp-prod_binary64 (exp.f64 (*.f64 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)) 1/2))) (/.f64 (*.f64 x x) 1/2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (exp.f64 (*.f64 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)) (Rewrite<= metadata-eval (neg.f64 -1/2)))) (/.f64 (*.f64 x x) 1/2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (exp.f64 (Rewrite<= distribute-rgt-neg-in_binary64 (neg.f64 (*.f64 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)) -1/2)))) (/.f64 (*.f64 x x) 1/2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (exp.f64 (neg.f64 (Rewrite<= *-commutative_binary64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)))))) (/.f64 (*.f64 x x) 1/2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (Rewrite<= rec-exp_binary64 (/.f64 1 (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)))))) (/.f64 (*.f64 x x) 1/2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (/.f64 1 (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1))))) (/.f64 (Rewrite<= unpow2_binary64 (pow.f64 x 2)) 1/2)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 (/.f64 1 (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1))))) 1/2) (pow.f64 x 2))): 0 points increase in error, 0 points decrease in error
      (/.f64 (Rewrite=> associate-*l/_binary64 (/.f64 (*.f64 1 1/2) (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)))))) (pow.f64 x 2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (/.f64 (Rewrite=> metadata-eval 1/2) (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1))))) (pow.f64 x 2)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-/r*_binary64 (/.f64 1/2 (*.f64 (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)))) (pow.f64 x 2)))): 0 points increase in error, 0 points decrease in error
    5. Applied egg-rr0.0

      \[\leadsto \color{blue}{{x}^{-1.5} \cdot 0.5} \]

    if 1e-22 < (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1))))

    1. Initial program 1.4

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

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

      \[\leadsto \color{blue}{{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.6

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

Alternatives

Alternative 1
Error0.4
Cost13760
\[\frac{\frac{\frac{1}{x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}}{1 + x} \]
Alternative 2
Error1.0
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 0.004647017308178906:\\ \;\;\;\;\left({x}^{-0.5} + x \cdot 0.5\right) + -1\\ \mathbf{else}:\\ \;\;\;\;{x}^{-1.5} \cdot 0.5\\ \end{array} \]
Alternative 3
Error21.1
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.004647017308178906:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\frac{1 + \left(x - x\right)}{x}}{1 + x}}{x \cdot -0.5}\\ \end{array} \]
Alternative 4
Error1.1
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.004647017308178906:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;{x}^{-1.5} \cdot 0.5\\ \end{array} \]
Alternative 5
Error21.4
Cost6660
\[\begin{array}{l} \mathbf{if}\;x \leq 3.4502982727075905 \cdot 10^{+120}:\\ \;\;\;\;{x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\frac{1 + \left(x - x\right)}{x}}{1 + x}}{x \cdot -0.5}\\ \end{array} \]
Alternative 6
Error49.6
Cost1092
\[\begin{array}{l} \mathbf{if}\;x \leq 3.624772434032581 \cdot 10^{+153}:\\ \;\;\;\;\frac{1}{x + 0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\frac{1 + \left(x - x\right)}{x}}{1 + x}}{x \cdot -0.5}\\ \end{array} \]
Alternative 7
Error59.2
Cost320
\[\frac{1}{x + 0.5} \]
Alternative 8
Error59.3
Cost192
\[\frac{1}{x} \]

Error

Reproduce

herbie shell --seed 2022317 
(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)))))