Average Error: 19.8 → 0.3
Time: 14.2s
Precision: binary64
Cost: 13892
\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
\[\begin{array}{l} \mathbf{if}\;x \leq 5 \cdot 10^{+41}:\\ \;\;\;\;\frac{1}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x \cdot \left(1 + x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{{x}^{-0.5}}{\mathsf{fma}\left(x, 2, 2\right)}\\ \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x)
 :precision binary64
 (if (<= x 5e+41)
   (/ 1.0 (* (+ (pow x -0.5) (pow (+ 1.0 x) -0.5)) (* x (+ 1.0 x))))
   (/ (pow x -0.5) (fma x 2.0 2.0))))
double code(double x) {
	return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
double code(double x) {
	double tmp;
	if (x <= 5e+41) {
		tmp = 1.0 / ((pow(x, -0.5) + pow((1.0 + x), -0.5)) * (x * (1.0 + x)));
	} else {
		tmp = pow(x, -0.5) / fma(x, 2.0, 2.0);
	}
	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 <= 5e+41)
		tmp = Float64(1.0 / Float64(Float64((x ^ -0.5) + (Float64(1.0 + x) ^ -0.5)) * Float64(x * Float64(1.0 + x))));
	else
		tmp = Float64((x ^ -0.5) / fma(x, 2.0, 2.0));
	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_] := If[LessEqual[x, 5e+41], N[(1.0 / N[(N[(N[Power[x, -0.5], $MachinePrecision] + N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision] * N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Power[x, -0.5], $MachinePrecision] / N[(x * 2.0 + 2.0), $MachinePrecision]), $MachinePrecision]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
\mathbf{if}\;x \leq 5 \cdot 10^{+41}:\\
\;\;\;\;\frac{1}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x \cdot \left(1 + x\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{{x}^{-0.5}}{\mathsf{fma}\left(x, 2, 2\right)}\\


\end{array}

Error

Target

Original19.8
Target0.7
Herbie0.3
\[\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.00000000000000022e41

    1. Initial program 5.9

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

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

      \[\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-rr10.5

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\frac{1}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x \cdot \left(1 + x\right)\right)}\right)} - 1} \]
    5. Simplified0.5

      \[\leadsto \color{blue}{\frac{1}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x \cdot \left(1 + x\right)\right)}} \]
      Proof
      (/.f64 1 (*.f64 (+.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)) (*.f64 x (+.f64 1 x)))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= expm1-log1p_binary64 (expm1.f64 (log1p.f64 (/.f64 1 (*.f64 (+.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)) (*.f64 x (+.f64 1 x))))))): 124 points increase in error, 2 points decrease in error
      (Rewrite<= expm1-def_binary64 (-.f64 (exp.f64 (log1p.f64 (/.f64 1 (*.f64 (+.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)) (*.f64 x (+.f64 1 x)))))) 1)): 80 points increase in error, 6 points decrease in error

    if 5.00000000000000022e41 < x

    1. Initial program 37.9

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

      \[\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 37.9

      \[\leadsto \frac{\frac{1}{x} - \frac{1}{1 + x}}{\color{blue}{2 \cdot \sqrt{\frac{1}{x}}}} \]
    4. Applied egg-rr11.1

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

      \[\leadsto \color{blue}{{x}^{-0.5} \cdot \frac{\frac{1}{1 + x}}{2}} \]
    6. Simplified0.1

      \[\leadsto \color{blue}{\frac{{x}^{-0.5}}{\mathsf{fma}\left(x, 2, 2\right)}} \]
      Proof
      (/.f64 (pow.f64 x -1/2) (fma.f64 x 2 2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (Rewrite<= *-rgt-identity_binary64 (*.f64 (pow.f64 x -1/2) 1)) (fma.f64 x 2 2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (*.f64 (pow.f64 x -1/2) 1) (Rewrite<= fma-def_binary64 (+.f64 (*.f64 x 2) 2))): 0 points increase in error, 0 points decrease in error
      (/.f64 (*.f64 (pow.f64 x -1/2) 1) (+.f64 (*.f64 x 2) (Rewrite<= metadata-eval (*.f64 1 2)))): 0 points increase in error, 0 points decrease in error
      (/.f64 (*.f64 (pow.f64 x -1/2) 1) (Rewrite<= distribute-rgt-in_binary64 (*.f64 2 (+.f64 x 1)))): 0 points increase in error, 0 points decrease in error
      (/.f64 (*.f64 (pow.f64 x -1/2) 1) (*.f64 2 (Rewrite<= +-commutative_binary64 (+.f64 1 x)))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-*r/_binary64 (*.f64 (pow.f64 x -1/2) (/.f64 1 (*.f64 2 (+.f64 1 x))))): 20 points increase in error, 8 points decrease in error
      (*.f64 (pow.f64 x -1/2) (Rewrite<= associate-/l/_binary64 (/.f64 (/.f64 1 (+.f64 1 x)) 2))): 0 points increase in error, 0 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification0.3

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

Alternatives

Alternative 1
Error0.5
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.4
Cost13444
\[\begin{array}{l} \mathbf{if}\;x \leq 44000000:\\ \;\;\;\;{x}^{-0.5} - \sqrt{\frac{1}{1 + x}}\\ \mathbf{else}:\\ \;\;\;\;\frac{{x}^{-0.5}}{\mathsf{fma}\left(x, 2, 2\right)}\\ \end{array} \]
Alternative 3
Error0.4
Cost13380
\[\begin{array}{l} \mathbf{if}\;x \leq 44000000:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{{x}^{-0.5}}{\mathsf{fma}\left(x, 2, 2\right)}\\ \end{array} \]
Alternative 4
Error1.0
Cost13316
\[\begin{array}{l} \mathbf{if}\;x \leq 0.68:\\ \;\;\;\;{x}^{-0.5} - \left(1 + x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{{x}^{-0.5}}{\mathsf{fma}\left(x, 2, 2\right)}\\ \end{array} \]
Alternative 5
Error1.2
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 0.42:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} \cdot \frac{0.5}{1 + x}\\ \end{array} \]
Alternative 6
Error1.0
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 0.68:\\ \;\;\;\;{x}^{-0.5} - \left(1 + x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} \cdot \frac{0.5}{1 + x}\\ \end{array} \]
Alternative 7
Error1.3
Cost6852
\[\begin{array}{l} \mathbf{if}\;x \leq 0.68:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{0.5}{x}}{\sqrt{x}}\\ \end{array} \]
Alternative 8
Error30.1
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.82:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x}\\ \end{array} \]
Alternative 9
Error59.3
Cost192
\[\frac{1}{x} \]
Alternative 10
Error62.8
Cost64
\[-1 \]

Error

Reproduce

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