?

Average Error: 19.8 → 0.2
Time: 13.1s
Precision: binary64
Cost: 26500

?

\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
\[\begin{array}{l} \mathbf{if}\;x \leq 5 \cdot 10^{+56}:\\ \;\;\;\;\frac{1}{\sqrt{x} + \sqrt{1 + x}} \cdot {\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} \cdot \frac{0.5 - \frac{0.375}{x}}{x}\\ \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x)
 :precision binary64
 (if (<= x 5e+56)
   (* (/ 1.0 (+ (sqrt x) (sqrt (+ 1.0 x)))) (pow (fma x x x) -0.5))
   (* (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+56) {
		tmp = (1.0 / (sqrt(x) + sqrt((1.0 + x)))) * pow(fma(x, x, x), -0.5);
	} 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+56)
		tmp = Float64(Float64(1.0 / Float64(sqrt(x) + sqrt(Float64(1.0 + x)))) * (fma(x, x, x) ^ -0.5));
	else
		tmp = Float64((x ^ -0.5) * Float64(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+56], N[(N[(1.0 / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[N[(x * x + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision], N[(N[Power[x, -0.5], $MachinePrecision] * N[(N[(0.5 - N[(0.375 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
\mathbf{if}\;x \leq 5 \cdot 10^{+56}:\\
\;\;\;\;\frac{1}{\sqrt{x} + \sqrt{1 + x}} \cdot {\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}\\

\mathbf{else}:\\
\;\;\;\;{x}^{-0.5} \cdot \frac{0.5 - \frac{0.375}{x}}{x}\\


\end{array}

Error?

Target

Original19.8
Target0.7
Herbie0.2
\[\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.00000000000000024e56

    1. Initial program 8.4

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

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

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

      [Start]0.3

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

      +-commutative [=>]0.3

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

      +-inverses [=>]0.3

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

      metadata-eval [=>]0.3

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

      +-commutative [=>]0.3

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

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

    if 5.00000000000000024e56 < x

    1. Initial program 36.7

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

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

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

      [Start]36.7

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

      associate-*l/ [=>]36.7

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

      associate-/l* [=>]36.7

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

      metadata-eval [<=]36.7

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

      associate-/l* [<=]36.7

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

      *-commutative [<=]36.7

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

      mul-1-neg [=>]36.7

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

      remove-double-neg [=>]36.7

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

      /-rgt-identity [=>]36.7

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

      associate-/l/ [=>]36.7

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

      associate-/r* [=>]36.7

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

      div-sub [=>]36.7

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

      *-inverses [=>]36.7

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

      \[\leadsto \frac{\color{blue}{0.5 \cdot \frac{1}{x} - 0.375 \cdot \frac{1}{{x}^{2}}}}{\sqrt{x}} \]
    5. Simplified0.2

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

      [Start]0.2

      \[ \frac{0.5 \cdot \frac{1}{x} - 0.375 \cdot \frac{1}{{x}^{2}}}{\sqrt{x}} \]

      associate-*r/ [=>]0.2

      \[ \frac{\color{blue}{\frac{0.5 \cdot 1}{x}} - 0.375 \cdot \frac{1}{{x}^{2}}}{\sqrt{x}} \]

      metadata-eval [=>]0.2

      \[ \frac{\frac{\color{blue}{0.5}}{x} - 0.375 \cdot \frac{1}{{x}^{2}}}{\sqrt{x}} \]

      associate-*r/ [=>]0.2

      \[ \frac{\frac{0.5}{x} - \color{blue}{\frac{0.375 \cdot 1}{{x}^{2}}}}{\sqrt{x}} \]

      metadata-eval [=>]0.2

      \[ \frac{\frac{0.5}{x} - \frac{\color{blue}{0.375}}{{x}^{2}}}{\sqrt{x}} \]

      unpow2 [=>]0.2

      \[ \frac{\frac{0.5}{x} - \frac{0.375}{\color{blue}{x \cdot x}}}{\sqrt{x}} \]
    6. Applied egg-rr0.2

      \[\leadsto \color{blue}{\frac{0.5 - \frac{0.375}{x}}{x} \cdot {x}^{-0.5}} \]
    7. Simplified0.2

      \[\leadsto \color{blue}{{x}^{-0.5} \cdot \frac{0.5 - \frac{0.375}{x}}{x}} \]
      Proof

      [Start]0.2

      \[ \frac{0.5 - \frac{0.375}{x}}{x} \cdot {x}^{-0.5} \]

      *-commutative [=>]0.2

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

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

Alternatives

Alternative 1
Error0.2
Cost33988
\[\begin{array}{l} t_0 := \frac{-1}{\sqrt{1 + x}}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + t_0 \leq 5 \cdot 10^{-9}:\\ \;\;\;\;\frac{\left(0.5 \cdot \frac{1}{x} + 0.3125 \cdot \frac{1}{{x}^{3}}\right) + 0.375 \cdot \frac{-1}{{x}^{2}}}{\sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} + t_0\\ \end{array} \]
Alternative 2
Error0.2
Cost27268
\[\begin{array}{l} t_0 := \frac{-1}{\sqrt{1 + x}}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + t_0 \leq 5 \cdot 10^{-9}:\\ \;\;\;\;\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} + t_0\\ \end{array} \]
Alternative 3
Error0.2
Cost26756
\[\begin{array}{l} t_0 := \frac{-1}{\sqrt{1 + x}}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + t_0 \leq 5 \cdot 10^{-9}:\\ \;\;\;\;{x}^{-0.5} \cdot \frac{0.5 - \frac{0.375}{x}}{x}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} + t_0\\ \end{array} \]
Alternative 4
Error0.7
Cost26368
\[\frac{1}{\frac{\mathsf{hypot}\left(x, \sqrt{x}\right)}{\frac{1}{\sqrt{x} + \sqrt{1 + x}}}} \]
Alternative 5
Error0.2
Cost13636
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ \mathbf{if}\;x \leq 112000:\\ \;\;\;\;{x}^{-0.5} + \frac{-1}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{x + 0.5}}{\sqrt{x} + t_0}\\ \end{array} \]
Alternative 6
Error0.2
Cost13636
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ \mathbf{if}\;x \leq 112000:\\ \;\;\;\;{x}^{-0.5} + \frac{-1}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{\sqrt{x} + t_0}}{x + 0.5}\\ \end{array} \]
Alternative 7
Error0.2
Cost13380
\[\begin{array}{l} \mathbf{if}\;x \leq 160000:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} \cdot \frac{0.5 - \frac{0.375}{x}}{x}\\ \end{array} \]
Alternative 8
Error0.8
Cost7172
\[\begin{array}{l} \mathbf{if}\;x \leq 1.1:\\ \;\;\;\;{x}^{-0.5} + \left(-1 - x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} \cdot \frac{0.5 - \frac{0.375}{x}}{x}\\ \end{array} \]
Alternative 9
Error0.7
Cost7172
\[\begin{array}{l} \mathbf{if}\;x \leq 1.4:\\ \;\;\;\;{x}^{-0.5} + \frac{-1}{1 - x \cdot -0.5}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} \cdot \frac{0.5 - \frac{0.375}{x}}{x}\\ \end{array} \]
Alternative 10
Error0.8
Cost7108
\[\begin{array}{l} \mathbf{if}\;x \leq 1.1:\\ \;\;\;\;{x}^{-0.5} + \left(-1 - x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{0.5 - \frac{0.375}{x}}{x}}{\sqrt{x}}\\ \end{array} \]
Alternative 11
Error1.1
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 1:\\ \;\;\;\;{x}^{-0.5} + \left(-1 - x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{0.5}{x}}{\sqrt{x}}\\ \end{array} \]
Alternative 12
Error1.1
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 1.1:\\ \;\;\;\;{x}^{-0.5} + \left(-1 - x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 - \frac{0.375}{x}}{{x}^{1.5}}\\ \end{array} \]
Alternative 13
Error1.4
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
Error21.0
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.6:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;\left(1 + \frac{1}{x + 0.5}\right) + -1\\ \end{array} \]
Alternative 15
Error21.7
Cost6660
\[\begin{array}{l} \mathbf{if}\;x \leq 8.5 \cdot 10^{+122}:\\ \;\;\;\;{x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;1 + \left(\frac{1}{x + 0.5} + -1\right)\\ \end{array} \]
Alternative 16
Error50.4
Cost576
\[1 + \left(\frac{1}{x + 0.5} + -1\right) \]
Alternative 17
Error50.4
Cost576
\[\left(1 + \frac{1}{x + 0.5}\right) + -1 \]
Alternative 18
Error59.2
Cost320
\[\frac{1}{x + 0.5} \]
Alternative 19
Error59.2
Cost192
\[\frac{1}{x} \]
Alternative 20
Error60.3
Cost64
\[2 \]

Error

Reproduce?

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