Average Error: 19.4 → 0.1
Time: 9.4s
Precision: binary64
Cost: 26372
\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
\[\begin{array}{l} t_0 := \sqrt{x} + \sqrt{1 + x}\\ \mathbf{if}\;x \leq 5.055835627447984 \cdot 10^{+124}:\\ \;\;\;\;\frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{t_0}}{0.5 + \left(\left(x + \frac{0.0625}{x \cdot x}\right) + \frac{-0.125}{x}\right)}\\ \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (+ (sqrt x) (sqrt (+ 1.0 x)))))
   (if (<= x 5.055835627447984e+124)
     (/ (pow (fma x x x) -0.5) t_0)
     (/ (/ 1.0 t_0) (+ 0.5 (+ (+ x (/ 0.0625 (* x x))) (/ -0.125 x)))))))
double code(double x) {
	return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
double code(double x) {
	double t_0 = sqrt(x) + sqrt((1.0 + x));
	double tmp;
	if (x <= 5.055835627447984e+124) {
		tmp = pow(fma(x, x, x), -0.5) / t_0;
	} else {
		tmp = (1.0 / t_0) / (0.5 + ((x + (0.0625 / (x * x))) + (-0.125 / 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)
	t_0 = Float64(sqrt(x) + sqrt(Float64(1.0 + x)))
	tmp = 0.0
	if (x <= 5.055835627447984e+124)
		tmp = Float64((fma(x, x, x) ^ -0.5) / t_0);
	else
		tmp = Float64(Float64(1.0 / t_0) / Float64(0.5 + Float64(Float64(x + Float64(0.0625 / Float64(x * x))) + Float64(-0.125 / x))));
	end
	return 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_] := Block[{t$95$0 = N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, 5.055835627447984e+124], N[(N[Power[N[(x * x + x), $MachinePrecision], -0.5], $MachinePrecision] / t$95$0), $MachinePrecision], N[(N[(1.0 / t$95$0), $MachinePrecision] / N[(0.5 + N[(N[(x + N[(0.0625 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-0.125 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
t_0 := \sqrt{x} + \sqrt{1 + x}\\
\mathbf{if}\;x \leq 5.055835627447984 \cdot 10^{+124}:\\
\;\;\;\;\frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}}{t_0}\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{t_0}}{0.5 + \left(\left(x + \frac{0.0625}{x \cdot x}\right) + \frac{-0.125}{x}\right)}\\


\end{array}

Error

Target

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

Derivation

  1. Split input into 2 regimes
  2. if x < 5.0558356274479844e124

    1. Initial program 16.3

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

      \[\leadsto \color{blue}{\frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x \cdot \left(1 + x\right)}}} \]
    3. Applied egg-rr15.5

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

      \[\leadsto \frac{\frac{\color{blue}{1}}{\sqrt{x + 1} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}} \]
    5. Applied egg-rr0.1

      \[\leadsto \color{blue}{0 + \frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}}{\sqrt{1 + x} + \sqrt{x}}} \]

    if 5.0558356274479844e124 < x

    1. Initial program 26.5

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

      \[\leadsto \color{blue}{\frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x \cdot \left(1 + x\right)}}} \]
    3. Applied egg-rr26.5

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

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

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

      \[\leadsto \frac{\frac{1}{\sqrt{x + 1} + \sqrt{x}}}{\color{blue}{0.5 + \left(\left(x + \frac{0.0625}{x \cdot x}\right) + \frac{-0.125}{x}\right)}} \]
      Proof
      (+.f64 1/2 (+.f64 (+.f64 x (/.f64 1/16 (*.f64 x x))) (/.f64 -1/8 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 1/2 (+.f64 (+.f64 x (/.f64 (Rewrite<= metadata-eval (*.f64 1/16 1)) (*.f64 x x))) (/.f64 -1/8 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 1/2 (+.f64 (+.f64 x (/.f64 (*.f64 1/16 1) (Rewrite<= unpow2_binary64 (pow.f64 x 2)))) (/.f64 -1/8 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 1/2 (+.f64 (+.f64 x (Rewrite<= associate-*r/_binary64 (*.f64 1/16 (/.f64 1 (pow.f64 x 2))))) (/.f64 -1/8 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 1/2 (+.f64 (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 1/16 (/.f64 1 (pow.f64 x 2))) x)) (/.f64 -1/8 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 1/2 (+.f64 (+.f64 (*.f64 1/16 (/.f64 1 (pow.f64 x 2))) x) (/.f64 (Rewrite<= metadata-eval (neg.f64 1/8)) x))): 0 points increase in error, 0 points decrease in error
      (+.f64 1/2 (+.f64 (+.f64 (*.f64 1/16 (/.f64 1 (pow.f64 x 2))) x) (Rewrite<= distribute-neg-frac_binary64 (neg.f64 (/.f64 1/8 x))))): 0 points increase in error, 0 points decrease in error
      (+.f64 1/2 (+.f64 (+.f64 (*.f64 1/16 (/.f64 1 (pow.f64 x 2))) x) (neg.f64 (/.f64 (Rewrite<= metadata-eval (*.f64 1/8 1)) x)))): 0 points increase in error, 0 points decrease in error
      (+.f64 1/2 (+.f64 (+.f64 (*.f64 1/16 (/.f64 1 (pow.f64 x 2))) x) (neg.f64 (Rewrite<= associate-*r/_binary64 (*.f64 1/8 (/.f64 1 x)))))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-+l+_binary64 (+.f64 (+.f64 1/2 (+.f64 (*.f64 1/16 (/.f64 1 (pow.f64 x 2))) x)) (neg.f64 (*.f64 1/8 (/.f64 1 x))))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= sub-neg_binary64 (-.f64 (+.f64 1/2 (+.f64 (*.f64 1/16 (/.f64 1 (pow.f64 x 2))) x)) (*.f64 1/8 (/.f64 1 x)))): 0 points increase in error, 0 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification0.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 5.055835627447984 \cdot 10^{+124}:\\ \;\;\;\;\frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}}{\sqrt{x} + \sqrt{1 + x}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{\sqrt{x} + \sqrt{1 + x}}}{0.5 + \left(\left(x + \frac{0.0625}{x \cdot x}\right) + \frac{-0.125}{x}\right)}\\ \end{array} \]

Alternatives

Alternative 1
Error0.2
Cost27204
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{t_0} \leq 2 \cdot 10^{-7}:\\ \;\;\;\;\frac{\frac{1}{\sqrt{x} + t_0}}{\frac{-0.125}{x} + \left(x + 0.5\right)}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \end{array} \]
Alternative 2
Error0.3
Cost26432
\[\frac{-1}{\sqrt{x} + \sqrt{1 + x}} \cdot \frac{1}{-\mathsf{hypot}\left(x, \sqrt{x}\right)} \]
Alternative 3
Error19.3
Cost13248
\[{x}^{-0.5} - {\left(1 + x\right)}^{-0.5} \]
Alternative 4
Error19.8
Cost7172
\[\begin{array}{l} \mathbf{if}\;x \leq 0.015014295139124887:\\ \;\;\;\;{x}^{-0.5} + \frac{-1}{1 + x \cdot 0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(x, x, -0.25\right)} \cdot \left(x + -0.5\right)\\ \end{array} \]
Alternative 5
Error19.8
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 0.015014295139124887:\\ \;\;\;\;{x}^{-0.5} + \left(-1 + x \cdot 0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sqrt{x \cdot \left(1 + x\right)}}\\ \end{array} \]
Alternative 6
Error20.4
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.015014295139124887:\\ \;\;\;\;-1 + {x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;-1 + \left(1 + \frac{1}{x + 0.5}\right)\\ \end{array} \]
Alternative 7
Error59.3
Cost320
\[\frac{1}{x + 0.5} \]
Alternative 8
Error59.3
Cost192
\[\frac{1}{x} \]
Alternative 9
Error62.8
Cost64
\[-1 \]
Alternative 10
Error60.3
Cost64
\[2 \]

Error

Reproduce

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