?

Average Error: 30.29% → 0.31%
Time: 11.0s
Precision: binary64
Cost: 27076

?

\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
\[\begin{array}{l} \mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{1 + x}} \leq 5 \cdot 10^{-9}:\\ \;\;\;\;\frac{\frac{1}{x}}{\sqrt{\frac{1}{x}} \cdot 1.5 + \sqrt{x} \cdot 2}\\ \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)))) 5e-9)
   (/ (/ 1.0 x) (+ (* (sqrt (/ 1.0 x)) 1.5) (* (sqrt x) 2.0)))
   (- (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)))) <= 5e-9) {
		tmp = (1.0 / x) / ((sqrt((1.0 / x)) * 1.5) + (sqrt(x) * 2.0));
	} 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)))) <= 5d-9) then
        tmp = (1.0d0 / x) / ((sqrt((1.0d0 / x)) * 1.5d0) + (sqrt(x) * 2.0d0))
    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)))) <= 5e-9) {
		tmp = (1.0 / x) / ((Math.sqrt((1.0 / x)) * 1.5) + (Math.sqrt(x) * 2.0));
	} 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)))) <= 5e-9:
		tmp = (1.0 / x) / ((math.sqrt((1.0 / x)) * 1.5) + (math.sqrt(x) * 2.0))
	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)))) <= 5e-9)
		tmp = Float64(Float64(1.0 / x) / Float64(Float64(sqrt(Float64(1.0 / x)) * 1.5) + Float64(sqrt(x) * 2.0)));
	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)))) <= 5e-9)
		tmp = (1.0 / x) / ((sqrt((1.0 / x)) * 1.5) + (sqrt(x) * 2.0));
	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], 5e-9], N[(N[(1.0 / x), $MachinePrecision] / N[(N[(N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision] * 1.5), $MachinePrecision] + N[(N[Sqrt[x], $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]), $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 5 \cdot 10^{-9}:\\
\;\;\;\;\frac{\frac{1}{x}}{\sqrt{\frac{1}{x}} \cdot 1.5 + \sqrt{x} \cdot 2}\\

\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

Original30.29%
Target1.02%
Herbie0.31%
\[\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)))) < 5.0000000000000001e-9

    1. Initial program 62.11

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

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

      \[\leadsto \color{blue}{\frac{1}{\frac{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x + x \cdot x\right)}{1 + \left(x - x\right)}}} \]
    4. Simplified0.42

      \[\leadsto \color{blue}{\frac{\frac{1}{x}}{\left(1 + x\right) \cdot \left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right)}} \]
      Proof

      [Start]16.74

      \[ \frac{1}{\frac{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x + x \cdot x\right)}{1 + \left(x - x\right)}} \]

      associate-/r/ [=>]16.74

      \[ \color{blue}{\frac{1}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x + x \cdot x\right)} \cdot \left(1 + \left(x - x\right)\right)} \]

      +-commutative [=>]16.74

      \[ \frac{1}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x + x \cdot x\right)} \cdot \color{blue}{\left(\left(x - x\right) + 1\right)} \]

      +-inverses [=>]16.74

      \[ \frac{1}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x + x \cdot x\right)} \cdot \left(\color{blue}{0} + 1\right) \]

      metadata-eval [=>]16.74

      \[ \frac{1}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x + x \cdot x\right)} \cdot \color{blue}{1} \]

      associate-/r/ [<=]16.74

      \[ \color{blue}{\frac{1}{\frac{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x + x \cdot x\right)}{1}}} \]

      /-rgt-identity [=>]16.74

      \[ \frac{1}{\color{blue}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x + x \cdot x\right)}} \]

      distribute-rgt1-in [=>]16.74

      \[ \frac{1}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \color{blue}{\left(\left(x + 1\right) \cdot x\right)}} \]

      +-commutative [<=]16.74

      \[ \frac{1}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(\color{blue}{\left(1 + x\right)} \cdot x\right)} \]

      associate-*r* [=>]1.7

      \[ \frac{1}{\color{blue}{\left(\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(1 + x\right)\right) \cdot x}} \]

      associate-/l/ [<=]0.42

      \[ \color{blue}{\frac{\frac{1}{x}}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(1 + x\right)}} \]

      *-commutative [=>]0.42

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

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

      \[\leadsto \frac{\frac{1}{x}}{\color{blue}{\sqrt{\frac{1}{x}} \cdot 1.5 + 2 \cdot \sqrt{x}}} \]
      Proof

      [Start]0.4

      \[ \frac{\frac{1}{x}}{-0.5 \cdot \sqrt{\frac{1}{x}} + \left(2 \cdot \sqrt{x} + 2 \cdot \sqrt{\frac{1}{x}}\right)} \]

      +-commutative [=>]0.4

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

      associate-+r+ [=>]0.4

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

      distribute-rgt-out [=>]0.4

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

      metadata-eval [=>]0.4

      \[ \frac{\frac{1}{x}}{\sqrt{\frac{1}{x}} \cdot \color{blue}{1.5} + 2 \cdot \sqrt{x}} \]

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

    1. Initial program 0.62

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

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

      \[\leadsto \color{blue}{{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}} \]
      Proof

      [Start]0.22

      \[ {x}^{-0.5} + \left(-{\left(1 + x\right)}^{-0.5}\right) \]

      sub-neg [<=]0.22

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

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

Alternatives

Alternative 1
Error0.63%
Cost13760
\[\frac{\frac{1}{x}}{\left(1 + x\right) \cdot \left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right)} \]
Alternative 2
Error0.61%
Cost13696
\[\frac{\frac{1}{x}}{\left(1 + x\right) \cdot {x}^{-0.5} + \sqrt{1 + x}} \]
Alternative 3
Error0.6%
Cost13444
\[\begin{array}{l} \mathbf{if}\;x \leq 37000000:\\ \;\;\;\;{x}^{-0.5} - \sqrt{\frac{1}{1 + x}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{x}}{\left(1 + x\right) \cdot \left(\sqrt{\frac{1}{x}} \cdot 2\right)}\\ \end{array} \]
Alternative 4
Error0.6%
Cost13380
\[\begin{array}{l} \mathbf{if}\;x \leq 37000000:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{x}}{\left(1 + x\right) \cdot \left(\sqrt{\frac{1}{x}} \cdot 2\right)}\\ \end{array} \]
Alternative 5
Error1.47%
Cost7364
\[\begin{array}{l} \mathbf{if}\;x \leq 0.67:\\ \;\;\;\;{x}^{-0.5} - \left(1 + x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{x}}{\left(1 + x\right) \cdot \left(\sqrt{\frac{1}{x}} \cdot 2\right)}\\ \end{array} \]
Alternative 6
Error1.51%
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 1:\\ \;\;\;\;{x}^{-0.5} - \left(1 + x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{x}}{\sqrt{x} \cdot 2}\\ \end{array} \]
Alternative 7
Error1.81%
Cost6980
\[\begin{array}{l} \mathbf{if}\;x \leq 0.67:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{x}}{\sqrt{x} \cdot 2}\\ \end{array} \]
Alternative 8
Error31.77%
Cost6916
\[\begin{array}{l} \mathbf{if}\;x \leq 0.8:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;\left(1 + {x}^{-0.5}\right) + -1\\ \end{array} \]
Alternative 9
Error45.92%
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.72:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{1 + \left(x - x\right)}{\left(x + 0.5\right) + \frac{-0.125}{x}}\\ \end{array} \]
Alternative 10
Error46.5%
Cost6720
\[\frac{1}{x + \sqrt{x}} \]
Alternative 11
Error48.39%
Cost6528
\[{x}^{-0.5} \]
Alternative 12
Error98.19%
Cost192
\[x \cdot -8 \]
Alternative 13
Error92.63%
Cost192
\[\frac{1}{x} \]

Error

Reproduce?

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