?

Average Error: 30.19% → 0.31%
Time: 12.8s
Precision: binary64
Cost: 26436

?

\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
\[\begin{array}{l} t_0 := \sqrt{1 + x} + \sqrt{x}\\ \mathbf{if}\;x \leq 2 \cdot 10^{+63}:\\ \;\;\;\;\frac{\sqrt{\frac{1}{\mathsf{fma}\left(x, x, x\right)}}}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{-1}{t_0}}{-0.5 - x}\\ \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)) (sqrt x))))
   (if (<= x 2e+63)
     (/ (sqrt (/ 1.0 (fma x x x))) t_0)
     (/ (/ -1.0 t_0) (- -0.5 x)))))
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)) + sqrt(x);
	double tmp;
	if (x <= 2e+63) {
		tmp = sqrt((1.0 / fma(x, x, x))) / t_0;
	} else {
		tmp = (-1.0 / t_0) / (-0.5 - 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)
	t_0 = Float64(sqrt(Float64(1.0 + x)) + sqrt(x))
	tmp = 0.0
	if (x <= 2e+63)
		tmp = Float64(sqrt(Float64(1.0 / fma(x, x, x))) / t_0);
	else
		tmp = Float64(Float64(-1.0 / t_0) / Float64(-0.5 - 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_] := Block[{t$95$0 = N[(N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision] + N[Sqrt[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, 2e+63], N[(N[Sqrt[N[(1.0 / N[(x * x + x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / t$95$0), $MachinePrecision], N[(N[(-1.0 / t$95$0), $MachinePrecision] / N[(-0.5 - x), $MachinePrecision]), $MachinePrecision]]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
t_0 := \sqrt{1 + x} + \sqrt{x}\\
\mathbf{if}\;x \leq 2 \cdot 10^{+63}:\\
\;\;\;\;\frac{\sqrt{\frac{1}{\mathsf{fma}\left(x, x, x\right)}}}{t_0}\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{-1}{t_0}}{-0.5 - x}\\


\end{array}

Error?

Target

Original30.19%
Target1.02%
Herbie0.31%
\[\frac{1}{\left(x + 1\right) \cdot \sqrt{x} + x \cdot \sqrt{x + 1}} \]

Derivation?

  1. Split input into 2 regimes
  2. if x < 2.00000000000000012e63

    1. Initial program 14.05

      \[\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.48

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

      [Start]0.47

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

      associate-/r* [=>]0.48

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

      +-commutative [=>]0.48

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

      +-inverses [=>]0.48

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

      metadata-eval [=>]0.48

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

      +-commutative [=>]0.48

      \[ \frac{\frac{1}{\sqrt{x + x \cdot x}}}{\color{blue}{\sqrt{1 + x} + \sqrt{x}}} \]
    4. Applied egg-rr0.31

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

    if 2.00000000000000012e63 < x

    1. Initial program 55.17

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

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

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

      [Start]20.32

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

      associate-/r* [=>]20.32

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

      +-commutative [=>]20.32

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

      +-inverses [=>]20.32

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

      metadata-eval [=>]20.32

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

      +-commutative [=>]20.32

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

      \[\leadsto \frac{\frac{1}{\color{blue}{0.5 + x}}}{\sqrt{1 + x} + \sqrt{x}} \]
    5. Simplified0.31

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

      [Start]0.31

      \[ \frac{\frac{1}{0.5 + x}}{\sqrt{1 + x} + \sqrt{x}} \]

      +-commutative [=>]0.31

      \[ \frac{\frac{1}{\color{blue}{x + 0.5}}}{\sqrt{1 + x} + \sqrt{x}} \]
    6. Applied egg-rr0.31

      \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{1 + x} + \sqrt{x}}}{-0.5 - x} \cdot -1} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.31

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

Alternatives

Alternative 1
Error0.34%
Cost26948
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{t_0} \leq 10^{-9}:\\ \;\;\;\;\frac{\frac{1}{x + 0.5}}{t_0 + \sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \end{array} \]
Alternative 2
Error0.67%
Cost26820
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{t_0} \leq 10^{-12}:\\ \;\;\;\;\frac{\frac{1}{x}}{t_0 + \sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \end{array} \]
Alternative 3
Error7.75%
Cost26692
\[\begin{array}{l} \mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{1 + x}} \leq 10^{-12}:\\ \;\;\;\;\frac{\frac{\frac{1}{x}}{1 + x}}{2 \cdot \sqrt{\frac{1}{x}}}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \end{array} \]
Alternative 4
Error0.41%
Cost26304
\[\frac{\frac{-1}{\sqrt{1 + x} + \sqrt{x}}}{-\mathsf{hypot}\left(x, \sqrt{x}\right)} \]
Alternative 5
Error1.03%
Cost26240
\[\frac{1}{\left(\sqrt{1 + x} + \sqrt{x}\right) \cdot \mathsf{hypot}\left(x, \sqrt{x}\right)} \]
Alternative 6
Error0.41%
Cost20164
\[\begin{array}{l} t_0 := \sqrt{1 + x} + \sqrt{x}\\ \mathbf{if}\;x \leq 2 \cdot 10^{+63}:\\ \;\;\;\;\frac{\frac{1}{\sqrt{x + x \cdot x}}}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{-1}{t_0}}{-0.5 - x}\\ \end{array} \]
Alternative 7
Error0.58%
Cost13892
\[\begin{array}{l} \mathbf{if}\;x \leq 2 \cdot 10^{+30}:\\ \;\;\;\;\frac{\frac{1}{x + x \cdot x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{-1}{\sqrt{1 + x} + \sqrt{x}}}{-0.5 - x}\\ \end{array} \]
Alternative 8
Error8.59%
Cost7364
\[\begin{array}{l} \mathbf{if}\;x \leq 0.68:\\ \;\;\;\;{x}^{-0.5} + \left(-1 - x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\frac{1}{x}}{1 + x}}{2 \cdot \sqrt{\frac{1}{x}}}\\ \end{array} \]
Alternative 9
Error30.79%
Cost7236
\[\begin{array}{l} \mathbf{if}\;x \leq 0.68:\\ \;\;\;\;{x}^{-0.5} + \left(-1 - x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{x} \cdot \left(\frac{0.5}{-1 - x} + \frac{0.5}{x}\right)\\ \end{array} \]
Alternative 10
Error9.44%
Cost7236
\[\begin{array}{l} \mathbf{if}\;x \leq 1:\\ \;\;\;\;{x}^{-0.5} + \left(-1 - x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{x \cdot x}}{2 \cdot \sqrt{\frac{1}{x}}}\\ \end{array} \]
Alternative 11
Error30.72%
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 1:\\ \;\;\;\;{x}^{-0.5} + \left(-1 - x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x \cdot \left(x + {x}^{0.5}\right)}\\ \end{array} \]
Alternative 12
Error32.57%
Cost6848
\[\frac{1}{\sqrt{x + x \cdot x}} \]
Alternative 13
Error46.05%
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.65:\\ \;\;\;\;-1 + {x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x + 0.5}\\ \end{array} \]
Alternative 14
Error33.04%
Cost6784
\[1 + \left(-1 + {x}^{-0.5}\right) \]
Alternative 15
Error48.58%
Cost6528
\[{x}^{-0.5} \]
Alternative 16
Error92.59%
Cost320
\[\frac{1}{x + 0.5} \]
Alternative 17
Error98.08%
Cost64
\[-1 \]
Alternative 18
Error94.21%
Cost64
\[2 \]

Error

Reproduce?

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