?

Average Error: 19.8 → 0.2
Time: 13.2s
Precision: binary64
Cost: 39812

?

\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
\[\begin{array}{l} t_0 := \sqrt{x + 1}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{t_0} \leq 0:\\ \;\;\;\;\frac{\sqrt{x} \cdot \frac{0.5}{x}}{x + 1}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sqrt{x} + t_0} \cdot {\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}\\ \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (sqrt (+ x 1.0))))
   (if (<= (+ (/ 1.0 (sqrt x)) (/ -1.0 t_0)) 0.0)
     (/ (* (sqrt x) (/ 0.5 x)) (+ x 1.0))
     (* (/ 1.0 (+ (sqrt x) t_0)) (pow (fma x x x) -0.5)))))
double code(double x) {
	return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
double code(double x) {
	double t_0 = sqrt((x + 1.0));
	double tmp;
	if (((1.0 / sqrt(x)) + (-1.0 / t_0)) <= 0.0) {
		tmp = (sqrt(x) * (0.5 / x)) / (x + 1.0);
	} else {
		tmp = (1.0 / (sqrt(x) + t_0)) * pow(fma(x, x, x), -0.5);
	}
	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(x + 1.0))
	tmp = 0.0
	if (Float64(Float64(1.0 / sqrt(x)) + Float64(-1.0 / t_0)) <= 0.0)
		tmp = Float64(Float64(sqrt(x) * Float64(0.5 / x)) / Float64(x + 1.0));
	else
		tmp = Float64(Float64(1.0 / Float64(sqrt(x) + t_0)) * (fma(x, x, x) ^ -0.5));
	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[(x + 1.0), $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[(N[Sqrt[x], $MachinePrecision] * N[(0.5 / x), $MachinePrecision]), $MachinePrecision] / N[(x + 1.0), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / N[(N[Sqrt[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision] * N[Power[N[(x * x + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
t_0 := \sqrt{x + 1}\\
\mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{t_0} \leq 0:\\
\;\;\;\;\frac{\sqrt{x} \cdot \frac{0.5}{x}}{x + 1}\\

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


\end{array}

Error?

Target

Original19.8
Target0.6
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 (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 0.0

    1. Initial program 40.8

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

      \[\leadsto \color{blue}{\left(\frac{1}{x} + \frac{-1}{1 + x}\right) \cdot \frac{1}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}} \]
    3. Simplified40.8

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

      [Start]40.8

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

      associate-*r/ [=>]40.8

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

      *-rgt-identity [=>]40.8

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

      \[\leadsto \frac{\frac{1}{x} + \frac{-1}{1 + x}}{\color{blue}{2 \cdot \sqrt{\frac{1}{x}}}} \]
    5. Applied egg-rr21.9

      \[\leadsto \color{blue}{\frac{\left(1 + \left(x - x\right)\right) \cdot \left(0.5 \cdot \sqrt{x}\right)}{x \cdot \left(1 - x \cdot x\right)} \cdot \left(1 - x\right)} \]
    6. Simplified16.7

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

      [Start]21.9

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

      +-commutative [=>]21.9

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

      +-inverses [=>]21.9

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

      metadata-eval [=>]21.9

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

      *-lft-identity [=>]21.9

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

      times-frac [=>]16.7

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

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

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

    1. Initial program 1.3

      \[\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}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.2

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

Alternatives

Alternative 1
Error0.3
Cost45952
\[\begin{array}{l} t_0 := \sqrt{x} + \sqrt{x + 1}\\ \frac{\frac{-1}{\mathsf{hypot}\left(x, \sqrt{x}\right)} \cdot t_0}{-{t_0}^{2}} \end{array} \]
Alternative 2
Error0.3
Cost26948
\[\begin{array}{l} t_0 := \sqrt{x + 1}\\ t_1 := \frac{-1}{t_0}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + t_1 \leq 2 \cdot 10^{-13}:\\ \;\;\;\;\frac{\frac{1}{x + 0.5}}{\sqrt{x} + t_0}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} + t_1\\ \end{array} \]
Alternative 3
Error0.4
Cost26756
\[\begin{array}{l} t_0 := \frac{-1}{\sqrt{x + 1}}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + t_0 \leq 2 \cdot 10^{-13}:\\ \;\;\;\;\frac{\sqrt{x} \cdot \frac{0.5}{x}}{x + 1}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} + t_0\\ \end{array} \]
Alternative 4
Error0.7
Cost26432
\[\frac{1}{\frac{\left(-\sqrt{x}\right) - \sqrt{x + 1}}{\frac{-1}{\mathsf{hypot}\left(x, \sqrt{x}\right)}}} \]
Alternative 5
Error0.7
Cost26368
\[\frac{1}{\frac{\mathsf{hypot}\left(x, \sqrt{x}\right)}{\frac{1}{\sqrt{x} + \sqrt{x + 1}}}} \]
Alternative 6
Error0.3
Cost20164
\[\begin{array}{l} \mathbf{if}\;x \leq 5 \cdot 10^{+53}:\\ \;\;\;\;\frac{1}{\left(\sqrt{x} + \sqrt{x + 1}\right) \cdot \sqrt{x + x \cdot x}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\sqrt{x} \cdot \frac{0.5}{x}}{x + 1}\\ \end{array} \]
Alternative 7
Error0.4
Cost13380
\[\begin{array}{l} \mathbf{if}\;x \leq 41000000:\\ \;\;\;\;{x}^{-0.5} - {\left(x + 1\right)}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{\sqrt{x} \cdot \frac{0.5}{x}}{x + 1}\\ \end{array} \]
Alternative 8
Error1.3
Cost7108
\[\begin{array}{l} \mathbf{if}\;x \leq 0.68:\\ \;\;\;\;{x}^{-0.5} - \left(1 + x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5}{\frac{x}{\frac{\sqrt{x}}{x + 1}}}\\ \end{array} \]
Alternative 9
Error1.0
Cost7108
\[\begin{array}{l} \mathbf{if}\;x \leq 0.68:\\ \;\;\;\;{x}^{-0.5} - \left(1 + x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\sqrt{x} \cdot \frac{0.5}{x}}{x + 1}\\ \end{array} \]
Alternative 10
Error20.6
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 8.2 \cdot 10^{+76}:\\ \;\;\;\;{x}^{-0.5} - \left(1 + x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;-1 + \left(1 - {x}^{-0.5}\right)\\ \end{array} \]
Alternative 11
Error21.1
Cost6916
\[\begin{array}{l} \mathbf{if}\;x \leq 4:\\ \;\;\;\;-1 + {x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;-1 + \left(1 - {x}^{-0.5}\right)\\ \end{array} \]
Alternative 12
Error21.1
Cost6916
\[\begin{array}{l} \mathbf{if}\;x \leq 4:\\ \;\;\;\;-1 + {x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;1 + \left(-1 - {x}^{-0.5}\right)\\ \end{array} \]
Alternative 13
Error29.5
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.58:\\ \;\;\;\;-1 + {x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x + 0.5}\\ \end{array} \]
Alternative 14
Error59.3
Cost320
\[\frac{1}{x + 0.5} \]
Alternative 15
Error62.8
Cost64
\[-1 \]
Alternative 16
Error60.3
Cost64
\[2 \]

Error

Reproduce?

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