?

Average Error: 31.05% → 0.22%
Time: 10.6s
Precision: binary64
Cost: 39684

?

\[\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 0:\\ \;\;\;\;\frac{\frac{0.5}{\sqrt{x}}}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}}{\sqrt{x} + t_0}\\ \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)) 0.0)
     (/ (/ 0.5 (sqrt x)) x)
     (/ (pow (fma x x x) -0.5) (+ (sqrt x) t_0)))))
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)) <= 0.0) {
		tmp = (0.5 / sqrt(x)) / x;
	} else {
		tmp = pow(fma(x, x, x), -0.5) / (sqrt(x) + t_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)
	t_0 = sqrt(Float64(1.0 + x))
	tmp = 0.0
	if (Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / t_0)) <= 0.0)
		tmp = Float64(Float64(0.5 / sqrt(x)) / x);
	else
		tmp = Float64((fma(x, x, x) ^ -0.5) / Float64(sqrt(x) + t_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_] := Block[{t$95$0 = N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / t$95$0), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(0.5 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision], N[(N[Power[N[(x * x + x), $MachinePrecision], -0.5], $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]]]
\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 0:\\
\;\;\;\;\frac{\frac{0.5}{\sqrt{x}}}{x}\\

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


\end{array}

Error?

Target

Original31.05%
Target1.07%
Herbie0.22%
\[\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)))) < 0.0

    1. Initial program 62.16

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

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

      \[\leadsto \frac{1}{\color{blue}{2 \cdot \sqrt{{x}^{3}}}} \]
    4. Applied egg-rr1.19

      \[\leadsto \color{blue}{{\left(\frac{\sqrt[3]{0.5}}{\sqrt{x}}\right)}^{3}} \]
    5. Simplified0.36

      \[\leadsto \color{blue}{\frac{\frac{0.5}{\sqrt{x}}}{x}} \]
      Proof

      [Start]1.19

      \[ {\left(\frac{\sqrt[3]{0.5}}{\sqrt{x}}\right)}^{3} \]

      cube-div [=>]2.53

      \[ \color{blue}{\frac{{\left(\sqrt[3]{0.5}\right)}^{3}}{{\left(\sqrt{x}\right)}^{3}}} \]

      cube-mult [=>]2.54

      \[ \frac{{\left(\sqrt[3]{0.5}\right)}^{3}}{\color{blue}{\sqrt{x} \cdot \left(\sqrt{x} \cdot \sqrt{x}\right)}} \]

      associate-/r* [=>]1.18

      \[ \color{blue}{\frac{\frac{{\left(\sqrt[3]{0.5}\right)}^{3}}{\sqrt{x}}}{\sqrt{x} \cdot \sqrt{x}}} \]

      rem-cube-cbrt [=>]0.74

      \[ \frac{\frac{\color{blue}{0.5}}{\sqrt{x}}}{\sqrt{x} \cdot \sqrt{x}} \]

      rem-square-sqrt [=>]0.36

      \[ \frac{\frac{0.5}{\sqrt{x}}}{\color{blue}{x}} \]

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

    1. Initial program 2.37

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

      \[\leadsto \color{blue}{\frac{1 + \left(x - x\right)}{\sqrt{x + x \cdot x} \cdot \left(\sqrt{x} + \sqrt{1 + x}\right)}} \]
    3. Simplified0.47

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

      [Start]0.47

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

      +-commutative [=>]0.47

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

      +-inverses [=>]0.47

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

      metadata-eval [=>]0.47

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

      +-commutative [=>]0.47

      \[ \frac{1}{\sqrt{x + x \cdot x} \cdot \color{blue}{\left(\sqrt{1 + x} + \sqrt{x}\right)}} \]
    4. Applied egg-rr9.93

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

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

      [Start]9.93

      \[ e^{\mathsf{log1p}\left(\frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}}{\sqrt{x + 1} + \sqrt{x}}\right)} - 1 \]

      expm1-def [=>]7.04

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

      expm1-log1p [=>]0.1

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

      +-commutative [=>]0.1

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

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

Alternatives

Alternative 1
Error0.31%
Cost27588
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{t_0} \leq 5 \cdot 10^{-9}:\\ \;\;\;\;\frac{\left(1 + \left(x - x\right)\right) \cdot \frac{1}{0.5 + \left(x + \frac{-0.125}{x}\right)}}{\sqrt{x} + t_0}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \end{array} \]
Alternative 2
Error1.19%
Cost26692
\[\begin{array}{l} \mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{1 + x}} \leq 0:\\ \;\;\;\;\frac{\frac{0.5}{\sqrt{x}}}{x}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \end{array} \]
Alternative 3
Error1.11%
Cost26368
\[\frac{1}{\frac{\mathsf{hypot}\left(x, \sqrt{x}\right)}{\frac{1}{\sqrt{x} + \sqrt{1 + x}}}} \]
Alternative 4
Error0.41%
Cost26304
\[\frac{\frac{-1}{\sqrt{x} + \sqrt{1 + x}}}{-\mathsf{hypot}\left(x, \sqrt{x}\right)} \]
Alternative 5
Error1.73%
Cost7236
\[\begin{array}{l} \mathbf{if}\;x \leq 1.7:\\ \;\;\;\;\frac{1}{\sqrt{x}} - \frac{1}{1 + x \cdot 0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{0.5}{\sqrt{x}}}{x}\\ \end{array} \]
Alternative 6
Error1.88%
Cost6852
\[\begin{array}{l} \mathbf{if}\;x \leq 0.66:\\ \;\;\;\;-1 + {x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{0.5}{\sqrt{x}}}{x}\\ \end{array} \]
Alternative 7
Error47.23%
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.6:\\ \;\;\;\;-1 + {x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{-0.5}{x}}{x} + \frac{1}{x}\\ \end{array} \]
Alternative 8
Error2.48%
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.66:\\ \;\;\;\;-1 + {x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5}{{x}^{1.5}}\\ \end{array} \]
Alternative 9
Error92.59%
Cost320
\[\frac{1}{x + 0.5} \]
Alternative 10
Error92.61%
Cost192
\[\frac{1}{x} \]

Error

Reproduce?

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