?

Average Error: 19.7 → 0.0
Time: 13.3s
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}:\\ \;\;\;\;0.5 \cdot {x}^{-1.5}\\ \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)))
   (* 0.5 (pow x -1.5))))
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 = 0.5 * pow(x, -1.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)
	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(0.5 * (x ^ -1.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_] := 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[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $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}:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\


\end{array}

Error?

Target

Original19.7
Target0.7
Herbie0.0
\[\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.5

      \[\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{\frac{1}{\sqrt{x + x \cdot x}}}{\sqrt{1 + x} + \sqrt{x}}} \]
      Proof

      [Start]0.3

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

      associate-/r* [=>]0.3

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

      +-commutative [=>]0.3

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

      +-inverses [=>]0.3

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

      metadata-eval [=>]0.3

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

      +-commutative [=>]0.3

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

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

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

      [Start]6.3

      \[ e^{\mathsf{log1p}\left(\frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}}{\sqrt{1 + x} + \sqrt{x}}\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{1 + x} + \sqrt{x}}\right)\right)} \]

      expm1-log1p [=>]0.1

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

    if 5e15 < x

    1. Initial program 40.2

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

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

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

      [Start]60.0

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

      sub-neg [=>]60.0

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

      associate-/l/ [=>]60.0

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

      distribute-neg-frac [=>]60.0

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

      metadata-eval [=>]60.0

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

      +-commutative [=>]60.0

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{\frac{1}{{x}^{3}}}} \]
    5. Applied egg-rr40.2

      \[\leadsto 0.5 \cdot \color{blue}{\left(\left(1 + {x}^{-1.5}\right) - 1\right)} \]
    6. Simplified0.0

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

      [Start]40.2

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

      +-commutative [=>]40.2

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

      associate--l+ [=>]0.0

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

      metadata-eval [=>]0.0

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

      +-rgt-identity [=>]0.0

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

    \[\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}:\\ \;\;\;\;0.5 \cdot {x}^{-1.5}\\ \end{array} \]

Alternatives

Alternative 1
Error0.1
Cost27588
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{t_0} \leq 10^{-5}:\\ \;\;\;\;\frac{\frac{1}{\left(0.5 + \left(x + \frac{0.0625}{x \cdot x}\right)\right) + \frac{-0.125}{x}}}{t_0 + \sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \end{array} \]
Alternative 2
Error0.2
Cost27204
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{t_0} \leq 2 \cdot 10^{-8}:\\ \;\;\;\;\frac{\frac{1}{\left(x + 0.5\right) + \frac{-0.125}{x}}}{t_0 + \sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \end{array} \]
Alternative 3
Error0.2
Cost26948
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{t_0} \leq 2 \cdot 10^{-8}:\\ \;\;\;\;\frac{\frac{1}{x + 0.5}}{t_0 + \sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \end{array} \]
Alternative 4
Error0.2
Cost26948
\[\begin{array}{l} t_0 := \sqrt{1 + x}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{t_0} \leq 2 \cdot 10^{-8}:\\ \;\;\;\;\frac{\frac{-1}{t_0 + \sqrt{x}}}{-0.5 - x}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \end{array} \]
Alternative 5
Error0.3
Cost26692
\[\begin{array}{l} \mathbf{if}\;\frac{1}{\sqrt{x}} + \frac{-1}{\sqrt{1 + x}} \leq 10^{-15}:\\ \;\;\;\;0.5 \cdot {x}^{-1.5}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\ \end{array} \]
Alternative 6
Error0.3
Cost26304
\[\frac{\frac{-1}{\sqrt{1 + x} + \sqrt{x}}}{-\mathsf{hypot}\left(x, \sqrt{x}\right)} \]
Alternative 7
Error0.2
Cost20164
\[\begin{array}{l} \mathbf{if}\;x \leq 5 \cdot 10^{+15}:\\ \;\;\;\;\frac{\frac{1}{\sqrt{x + x \cdot x}}}{\sqrt{1 + x} + \sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot {x}^{-1.5}\\ \end{array} \]
Alternative 8
Error0.9
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 1:\\ \;\;\;\;{x}^{-0.5} - \left(1 + x \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot {x}^{-1.5}\\ \end{array} \]
Alternative 9
Error2.0
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.5:\\ \;\;\;\;{x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot {x}^{-1.5}\\ \end{array} \]
Alternative 10
Error1.1
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.68:\\ \;\;\;\;-1 + {x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot {x}^{-1.5}\\ \end{array} \]
Alternative 11
Error31.3
Cost6528
\[{x}^{-0.5} \]
Alternative 12
Error59.3
Cost320
\[\frac{1}{x + 0.5} \]
Alternative 13
Error59.3
Cost192
\[\frac{1}{x} \]
Alternative 14
Error62.8
Cost64
\[-1 \]
Alternative 15
Error60.3
Cost64
\[2 \]

Error

Reproduce?

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