Average Error: 20.3 → 0.4
Time: 5.0s
Precision: binary64
\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{t_0} \leq 5.459127345053966 \cdot 10^{-19}:\\ \;\;\;\;{x}^{-1.5} \cdot \left(0.5 + \frac{-0.375}{x}\right)\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_1 := {\left(1 + x\right)}^{-0.5}\\ t_2 := {t_0}^{-0.5}\\ \left({x}^{-0.5} - t_1\right) + \mathsf{fma}\left(-t_2, t_2, t_1\right) \end{array}\\ \end{array} \]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
t_0 := \sqrt{1 + x}\\
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{t_0} \leq 5.459127345053966 \cdot 10^{-19}:\\
\;\;\;\;{x}^{-1.5} \cdot \left(0.5 + \frac{-0.375}{x}\right)\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_1 := {\left(1 + x\right)}^{-0.5}\\
t_2 := {t_0}^{-0.5}\\
\left({x}^{-0.5} - t_1\right) + \mathsf{fma}\left(-t_2, t_2, t_1\right)
\end{array}\\


\end{array}
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (sqrt (+ 1.0 x))))
   (if (<= (- (/ 1.0 (sqrt x)) (/ 1.0 t_0)) 5.459127345053966e-19)
     (* (pow x -1.5) (+ 0.5 (/ -0.375 x)))
     (let* ((t_1 (pow (+ 1.0 x) -0.5)) (t_2 (pow t_0 -0.5)))
       (+ (- (pow x -0.5) t_1) (fma (- t_2) t_2 t_1))))))
double code(double x) {
	return (1.0 / sqrt(x)) - (1.0 / sqrt(x + 1.0));
}
double code(double x) {
	double t_0 = sqrt(1.0 + x);
	double tmp;
	if (((1.0 / sqrt(x)) - (1.0 / t_0)) <= 5.459127345053966e-19) {
		tmp = pow(x, -1.5) * (0.5 + (-0.375 / x));
	} else {
		double t_1 = pow((1.0 + x), -0.5);
		double t_2 = pow(t_0, -0.5);
		tmp = (pow(x, -0.5) - t_1) + fma(-t_2, t_2, t_1);
	}
	return tmp;
}

Error

Bits error versus x

Target

Original20.3
Target0.7
Herbie0.4
\[\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.45913e-19

    1. Initial program 41.0

      \[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
    2. Applied egg41.0

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

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

      \[\leadsto \color{blue}{\frac{{x}^{-0.5}}{x} \cdot \left(0.5 + \frac{-0.375}{x}\right)} \]
    5. Applied egg0.0

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

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

    1. Initial program 1.0

      \[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
    2. Applied egg0.7

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

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

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

Reproduce

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