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

\mathbf{else}:\\
\;\;\;\;\frac{{x}^{-0.5} \cdot \left(0.5 - \frac{0.375}{x}\right)}{x}\\


\end{array}

Error

Target

Original20.0
Target0.6
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 < 5e15

    1. Initial program 1.2

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

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

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

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

      [Start]5.9

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

      expm1-def [=>]4.6

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

      expm1-log1p [=>]0.1

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

    if 5e15 < x

    1. Initial program 40.0

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

      \[\leadsto \color{blue}{\frac{\frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x}}}{-\sqrt{1 + x}} \cdot -1} \]
    3. Simplified40.0

      \[\leadsto \color{blue}{\frac{1 - \frac{\sqrt{x}}{\sqrt{1 + x}}}{\sqrt{x}}} \]
      Proof

      [Start]40.0

      \[ \frac{\frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x}}}{-\sqrt{1 + x}} \cdot -1 \]

      associate-*l/ [=>]40.0

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

      associate-/l* [=>]40.0

      \[ \color{blue}{\frac{\frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x}}}{\frac{-\sqrt{1 + x}}{-1}}} \]

      metadata-eval [<=]40.0

      \[ \frac{\frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x}}}{\frac{-\sqrt{1 + x}}{\color{blue}{\frac{1}{-1}}}} \]

      associate-/l* [<=]40.0

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

      *-commutative [<=]40.0

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

      mul-1-neg [=>]40.0

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

      remove-double-neg [=>]40.0

      \[ \frac{\frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x}}}{\frac{\color{blue}{\sqrt{1 + x}}}{1}} \]

      /-rgt-identity [=>]40.0

      \[ \frac{\frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x}}}{\color{blue}{\sqrt{1 + x}}} \]

      associate-/l/ [=>]40.0

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

      associate-/r* [=>]40.0

      \[ \color{blue}{\frac{\frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{1 + x}}}{\sqrt{x}}} \]

      div-sub [=>]40.0

      \[ \frac{\color{blue}{\frac{\sqrt{1 + x}}{\sqrt{1 + x}} - \frac{\sqrt{x}}{\sqrt{1 + x}}}}{\sqrt{x}} \]

      *-inverses [=>]40.0

      \[ \frac{\color{blue}{1} - \frac{\sqrt{x}}{\sqrt{1 + x}}}{\sqrt{x}} \]
    4. Taylor expanded in x around inf 40.0

      \[\leadsto \frac{1 - \color{blue}{\left(\left(1 + 0.375 \cdot \frac{1}{{x}^{2}}\right) - 0.5 \cdot \frac{1}{x}\right)}}{\sqrt{x}} \]
    5. Simplified40.0

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

      [Start]40.0

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

      associate--l+ [=>]40.0

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

      associate-*r/ [=>]40.0

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

      metadata-eval [=>]40.0

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

      unpow2 [=>]40.0

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

      associate-*r/ [=>]40.0

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

      metadata-eval [=>]40.0

      \[ \frac{1 - \left(1 + \left(\frac{0.375}{x \cdot x} - \frac{\color{blue}{0.5}}{x}\right)\right)}{\sqrt{x}} \]
    6. Applied egg-rr40.0

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

      \[\leadsto \color{blue}{{x}^{-0.5} \cdot 0 + {x}^{-0.5} \cdot \left(-1 \cdot \frac{\frac{0.375}{x} + -0.5}{x}\right)} \]
    8. Simplified0.2

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

      [Start]0.2

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

      distribute-lft-out [=>]0.2

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

      +-lft-identity [=>]0.2

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

      mul-1-neg [=>]0.2

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

      distribute-neg-frac [=>]0.2

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

      associate-*r/ [=>]0.2

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

      +-commutative [<=]0.2

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

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

Alternatives

Alternative 1
Error0.2
Cost27972
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ t_1 := \frac{-1}{t_0}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + t_1 \leq 10^{-6}:\\ \;\;\;\;\frac{\left(1 + \left(x - x\right)\right) \cdot \frac{1}{0.5 + \left(\left(x + \frac{0.0625}{x \cdot x}\right) + \frac{-0.125}{x}\right)}}{t_0 + \sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} + t_1\\ \end{array} \]
Alternative 2
Error0.2
Cost27588
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ t_1 := \frac{-1}{t_0}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + t_1 \leq 10^{-6}:\\ \;\;\;\;\frac{\left(1 + \left(x - x\right)\right) \cdot \frac{1}{x + \left(0.5 + \frac{-0.125}{x}\right)}}{t_0 + \sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} + t_1\\ \end{array} \]
Alternative 3
Error0.2
Cost27268
\[\begin{array}{l} \mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 4 \cdot 10^{-8}:\\ \;\;\;\;\frac{\frac{0.5}{x} + \left(\frac{0.3125}{{x}^{3}} + \frac{-0.375}{x \cdot x}\right)}{\sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - \sqrt{\frac{1}{1 + x}}\\ \end{array} \]
Alternative 4
Error0.4
Cost26944
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ \frac{\left(1 + \left(x - x\right)\right) \cdot \frac{1}{\frac{t_0}{\frac{1}{\sqrt{x}}}}}{t_0 + \sqrt{x}} \end{array} \]
Alternative 5
Error0.2
Cost26756
\[\begin{array}{l} \mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 2 \cdot 10^{-9}:\\ \;\;\;\;\frac{{x}^{-0.5} \cdot \left(0.5 - \frac{0.375}{x}\right)}{x}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - \sqrt{\frac{1}{1 + x}}\\ \end{array} \]
Alternative 6
Error0.4
Cost13892
\[\begin{array}{l} \mathbf{if}\;x \leq 10^{+63}:\\ \;\;\;\;\frac{\frac{1}{x + x \cdot x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}\\ \mathbf{else}:\\ \;\;\;\;\frac{{x}^{-0.5} \cdot \left(0.5 - \frac{0.375}{x}\right)}{x}\\ \end{array} \]
Alternative 7
Error0.2
Cost13380
\[\begin{array}{l} \mathbf{if}\;x \leq 215000:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{{x}^{-0.5} \cdot \left(0.5 - \frac{0.375}{x}\right)}{x}\\ \end{array} \]
Alternative 8
Error1.0
Cost7172
\[\begin{array}{l} \mathbf{if}\;x \leq 1.45:\\ \;\;\;\;{x}^{-0.5} + \frac{-1}{1 + x \cdot 0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 - \frac{0.375}{x}}{{x}^{1.5}}\\ \end{array} \]
Alternative 9
Error0.6
Cost7172
\[\begin{array}{l} \mathbf{if}\;x \leq 1.45:\\ \;\;\;\;{x}^{-0.5} + \frac{-1}{1 + x \cdot 0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{0.5 - \frac{0.375}{x}}{x}}{\sqrt{x}}\\ \end{array} \]
Alternative 10
Error0.6
Cost7172
\[\begin{array}{l} \mathbf{if}\;x \leq 1.45:\\ \;\;\;\;{x}^{-0.5} + \frac{-1}{1 + x \cdot 0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{{x}^{-0.5} \cdot \left(0.5 - \frac{0.375}{x}\right)}{x}\\ \end{array} \]
Alternative 11
Error0.9
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 1:\\ \;\;\;\;\left({x}^{-0.5} + x \cdot 0.5\right) + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{0.5}{x}}{\sqrt{x}}\\ \end{array} \]
Alternative 12
Error1.0
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 1.1:\\ \;\;\;\;\left({x}^{-0.5} + x \cdot 0.5\right) + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 - \frac{0.375}{x}}{{x}^{1.5}}\\ \end{array} \]
Alternative 13
Error1.1
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 14
Error31.8
Cost6528
\[{x}^{-0.5} \]

Error

Reproduce

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