Average Error: 19.8 → 0.4
Time: 8.9s
Precision: binary64
Cost: 39748
\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
\[\begin{array}{l} t_0 := {\left(1 + x\right)}^{-0.25}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{1 + x}} \leq 4 \cdot 10^{-18}:\\ \;\;\;\;{x}^{-1.5} \cdot \left(\frac{-0.375}{x} + 0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t_0, -t_0, {x}^{-0.5}\right)\\ \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (pow (+ 1.0 x) -0.25)))
   (if (<= (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ 1.0 x)))) 4e-18)
     (* (pow x -1.5) (+ (/ -0.375 x) 0.5))
     (fma t_0 (- t_0) (pow 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 = pow((1.0 + x), -0.25);
	double tmp;
	if (((1.0 / sqrt(x)) - (1.0 / sqrt((1.0 + x)))) <= 4e-18) {
		tmp = pow(x, -1.5) * ((-0.375 / x) + 0.5);
	} else {
		tmp = fma(t_0, -t_0, pow(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 = Float64(1.0 + x) ^ -0.25
	tmp = 0.0
	if (Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(1.0 + x)))) <= 4e-18)
		tmp = Float64((x ^ -1.5) * Float64(Float64(-0.375 / x) + 0.5));
	else
		tmp = fma(t_0, Float64(-t_0), (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[Power[N[(1.0 + x), $MachinePrecision], -0.25], $MachinePrecision]}, If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 4e-18], N[(N[Power[x, -1.5], $MachinePrecision] * N[(N[(-0.375 / x), $MachinePrecision] + 0.5), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * (-t$95$0) + N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision]]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
t_0 := {\left(1 + x\right)}^{-0.25}\\
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{1 + x}} \leq 4 \cdot 10^{-18}:\\
\;\;\;\;{x}^{-1.5} \cdot \left(\frac{-0.375}{x} + 0.5\right)\\

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


\end{array}

Error

Target

Original19.8
Target0.6
Herbie0.4
\[\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)))) < 4.0000000000000003e-18

    1. Initial program 40.1

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

      \[\leadsto \color{blue}{\left({x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\right) + \mathsf{fma}\left(-1, {\left(1 + x\right)}^{-0.5}, {\left(1 + x\right)}^{-0.5}\right)} \]
    3. Simplified40.1

      \[\leadsto \color{blue}{{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}} \]
      Proof
      (-.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= +-rgt-identity_binary64 (+.f64 (-.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)) 0)): 0 points increase in error, 0 points decrease in error
      (+.f64 (-.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)) (Rewrite<= mul0-lft_binary64 (*.f64 0 (pow.f64 (+.f64 1 x) -1/2)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (-.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)) (*.f64 (Rewrite<= metadata-eval (+.f64 -1 1)) (pow.f64 (+.f64 1 x) -1/2))): 0 points increase in error, 0 points decrease in error
      (+.f64 (-.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)) (Rewrite<= distribute-lft1-in_binary64 (+.f64 (*.f64 -1 (pow.f64 (+.f64 1 x) -1/2)) (pow.f64 (+.f64 1 x) -1/2)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (-.f64 (pow.f64 x -1/2) (pow.f64 (+.f64 1 x) -1/2)) (Rewrite<= fma-udef_binary64 (fma.f64 -1 (pow.f64 (+.f64 1 x) -1/2) (pow.f64 (+.f64 1 x) -1/2)))): 0 points increase in error, 0 points decrease in error
    4. Taylor expanded in x around -inf 64.0

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

      \[\leadsto \color{blue}{\frac{{\left(\frac{-1}{\frac{-1}{x}}\right)}^{-0.5}}{x} \cdot \left(0.5 + \frac{-0.375}{x}\right)} \]
      Proof
      (*.f64 (/.f64 (pow.f64 (/.f64 -1 (/.f64 -1 x)) -1/2) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (pow.f64 (/.f64 (Rewrite<= rem-exp-log_binary64 (exp.f64 (log.f64 -1))) (/.f64 -1 x)) -1/2) x) (+.f64 1/2 (/.f64 -3/8 x))): 175 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (pow.f64 (/.f64 (exp.f64 (log.f64 -1)) (/.f64 (Rewrite<= metadata-eval (neg.f64 1)) x)) -1/2) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (pow.f64 (/.f64 (exp.f64 (log.f64 -1)) (Rewrite<= distribute-neg-frac_binary64 (neg.f64 (/.f64 1 x)))) -1/2) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (pow.f64 (/.f64 (exp.f64 (log.f64 -1)) (neg.f64 (Rewrite<= unpow-1_binary64 (pow.f64 x -1)))) -1/2) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (pow.f64 (/.f64 (exp.f64 (log.f64 -1)) (Rewrite<= rem-exp-log_binary64 (exp.f64 (log.f64 (neg.f64 (pow.f64 x -1)))))) -1/2) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (pow.f64 (/.f64 (exp.f64 (log.f64 -1)) (exp.f64 (log.f64 (neg.f64 (Rewrite=> unpow-1_binary64 (/.f64 1 x)))))) -1/2) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (pow.f64 (/.f64 (exp.f64 (log.f64 -1)) (exp.f64 (log.f64 (Rewrite=> distribute-neg-frac_binary64 (/.f64 (neg.f64 1) x))))) -1/2) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (pow.f64 (/.f64 (exp.f64 (log.f64 -1)) (exp.f64 (log.f64 (/.f64 (Rewrite=> metadata-eval -1) x)))) -1/2) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (pow.f64 (Rewrite<= exp-diff_binary64 (exp.f64 (-.f64 (log.f64 -1) (log.f64 (/.f64 -1 x))))) -1/2) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (pow.f64 (exp.f64 (Rewrite<= unsub-neg_binary64 (+.f64 (log.f64 -1) (neg.f64 (log.f64 (/.f64 -1 x)))))) -1/2) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (pow.f64 (exp.f64 (+.f64 (log.f64 -1) (Rewrite<= mul-1-neg_binary64 (*.f64 -1 (log.f64 (/.f64 -1 x)))))) -1/2) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (pow.f64 (exp.f64 (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)))) -1/2) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (Rewrite<= exp-prod_binary64 (exp.f64 (*.f64 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)) -1/2))) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (*.f64 (/.f64 (exp.f64 (Rewrite<= *-commutative_binary64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1))))) x) (+.f64 1/2 (/.f64 -3/8 x))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= distribute-rgt-out_binary64 (+.f64 (*.f64 1/2 (/.f64 (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)))) x)) (*.f64 (/.f64 -3/8 x) (/.f64 (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)))) x)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 1/2 (/.f64 (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)))) x)) (Rewrite<= times-frac_binary64 (/.f64 (*.f64 -3/8 (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1))))) (*.f64 x x)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 1/2 (/.f64 (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)))) x)) (/.f64 (*.f64 -3/8 (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1))))) (Rewrite<= unpow2_binary64 (pow.f64 x 2)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 1/2 (/.f64 (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)))) x)) (Rewrite<= associate-*r/_binary64 (*.f64 -3/8 (/.f64 (exp.f64 (*.f64 -1/2 (+.f64 (*.f64 -1 (log.f64 (/.f64 -1 x))) (log.f64 -1)))) (pow.f64 x 2))))): 0 points increase in error, 0 points decrease in error
    6. Applied egg-rr0.0

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

      \[\leadsto \color{blue}{{x}^{-1.5} \cdot \left(\frac{-0.375}{x} + 0.5\right)} \]
      Proof
      (*.f64 (pow.f64 x -3/2) (+.f64 (/.f64 -3/8 x) 1/2)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= distribute-lft-out_binary64 (+.f64 (*.f64 (pow.f64 x -3/2) (/.f64 -3/8 x)) (*.f64 (pow.f64 x -3/2) 1/2))): 0 points increase in error, 1 points decrease in error

    if 4.0000000000000003e-18 < (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1))))

    1. Initial program 0.9

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

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

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

Alternatives

Alternative 1
Error0.4
Cost26756
\[\begin{array}{l} t_0 := \frac{1}{\sqrt{1 + x}}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} - t_0 \leq 4 \cdot 10^{-18}:\\ \;\;\;\;{x}^{-1.5} \cdot \left(\frac{-0.375}{x} + 0.5\right)\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - t_0\\ \end{array} \]
Alternative 2
Error0.1
Cost13380
\[\begin{array}{l} \mathbf{if}\;x \leq 165000:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-1.5} \cdot \left(\frac{-0.375}{x} + 0.5\right)\\ \end{array} \]
Alternative 3
Error0.6
Cost7556
\[\begin{array}{l} \mathbf{if}\;x \leq 1.1:\\ \;\;\;\;\left({x}^{-0.5} + x \cdot 0.5\right) + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{{x}^{-1.5}}{\frac{0.5 + \frac{0.375}{x}}{0.25 - \frac{0.140625}{x \cdot x}}}\\ \end{array} \]
Alternative 4
Error0.8
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 0.86:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;{x}^{-1.5} \cdot \left(\frac{-0.375}{x} + 0.5\right)\\ \end{array} \]
Alternative 5
Error0.6
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 1.1:\\ \;\;\;\;\left({x}^{-0.5} + x \cdot 0.5\right) + -1\\ \mathbf{else}:\\ \;\;\;\;{x}^{-1.5} \cdot \left(\frac{-0.375}{x} + 0.5\right)\\ \end{array} \]
Alternative 6
Error1.2
Cost6916
\[\begin{array}{l} \mathbf{if}\;x \leq 0.66:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot {\left(\frac{1}{x}\right)}^{1.5}\\ \end{array} \]
Alternative 7
Error30.1
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.82:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x}\\ \end{array} \]
Alternative 8
Error30.4
Cost6720
\[\frac{1}{x + \sqrt{x}} \]
Alternative 9
Error31.6
Cost6528
\[{x}^{-0.5} \]
Alternative 10
Error61.5
Cost192
\[x \cdot 0.5 \]
Alternative 11
Error59.3
Cost192
\[\frac{1}{x} \]
Alternative 12
Error62.8
Cost64
\[-1 \]

Error

Reproduce

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