| Alternative 1 | |
|---|---|
| Accuracy | 99.7% |
| Cost | 27332 |
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x)
:precision binary64
(if (<= (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ 1.0 x)))) 5e-9)
(* (+ (/ 0.5 x) (- (/ 0.3125 (pow x 3.0)) (/ 0.375 (* x x)))) (pow x -0.5))
(fma
(- (cbrt (/ 1.0 (+ 1.0 x))))
(pow (+ 1.0 x) -0.16666666666666666)
(pow x -0.5))))double code(double x) {
return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) - (1.0 / sqrt((1.0 + x)))) <= 5e-9) {
tmp = ((0.5 / x) + ((0.3125 / pow(x, 3.0)) - (0.375 / (x * x)))) * pow(x, -0.5);
} else {
tmp = fma(-cbrt((1.0 / (1.0 + x))), pow((1.0 + x), -0.16666666666666666), 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) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(1.0 + x)))) <= 5e-9) tmp = Float64(Float64(Float64(0.5 / x) + Float64(Float64(0.3125 / (x ^ 3.0)) - Float64(0.375 / Float64(x * x)))) * (x ^ -0.5)); else tmp = fma(Float64(-cbrt(Float64(1.0 / Float64(1.0 + x)))), (Float64(1.0 + x) ^ -0.16666666666666666), (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_] := If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 5e-9], N[(N[(N[(0.5 / x), $MachinePrecision] + N[(N[(0.3125 / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] - N[(0.375 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision], N[((-N[Power[N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]) * N[Power[N[(1.0 + x), $MachinePrecision], -0.16666666666666666], $MachinePrecision] + N[Power[x, -0.5], $MachinePrecision]), $MachinePrecision]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{1 + x}} \leq 5 \cdot 10^{-9}:\\
\;\;\;\;\left(\frac{0.5}{x} + \left(\frac{0.3125}{{x}^{3}} - \frac{0.375}{x \cdot x}\right)\right) \cdot {x}^{-0.5}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-\sqrt[3]{\frac{1}{1 + x}}, {\left(1 + x\right)}^{-0.16666666666666666}, {x}^{-0.5}\right)\\
\end{array}
| Original | 69.4% |
|---|---|
| Target | 98.9% |
| Herbie | 99.7% |
if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 5.0000000000000001e-9Initial program 32.4%
Applied egg-rr32.4%
[Start]32.4 | \[ \frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\] |
|---|---|
frac-sub [=>]32.4 | \[ \color{blue}{\frac{1 \cdot \sqrt{x + 1} - \sqrt{x} \cdot 1}{\sqrt{x} \cdot \sqrt{x + 1}}}
\] |
div-inv [=>]32.4 | \[ \color{blue}{\left(1 \cdot \sqrt{x + 1} - \sqrt{x} \cdot 1\right) \cdot \frac{1}{\sqrt{x} \cdot \sqrt{x + 1}}}
\] |
*-un-lft-identity [<=]32.4 | \[ \left(\color{blue}{\sqrt{x + 1}} - \sqrt{x} \cdot 1\right) \cdot \frac{1}{\sqrt{x} \cdot \sqrt{x + 1}}
\] |
+-commutative [=>]32.4 | \[ \left(\sqrt{\color{blue}{1 + x}} - \sqrt{x} \cdot 1\right) \cdot \frac{1}{\sqrt{x} \cdot \sqrt{x + 1}}
\] |
*-rgt-identity [=>]32.4 | \[ \left(\sqrt{1 + x} - \color{blue}{\sqrt{x}}\right) \cdot \frac{1}{\sqrt{x} \cdot \sqrt{x + 1}}
\] |
metadata-eval [<=]32.4 | \[ \left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \frac{\color{blue}{1 \cdot 1}}{\sqrt{x} \cdot \sqrt{x + 1}}
\] |
frac-times [<=]32.4 | \[ \left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \color{blue}{\left(\frac{1}{\sqrt{x}} \cdot \frac{1}{\sqrt{x + 1}}\right)}
\] |
un-div-inv [=>]32.4 | \[ \left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \color{blue}{\frac{\frac{1}{\sqrt{x}}}{\sqrt{x + 1}}}
\] |
pow1/2 [=>]32.4 | \[ \left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \frac{\frac{1}{\color{blue}{{x}^{0.5}}}}{\sqrt{x + 1}}
\] |
pow-flip [=>]32.4 | \[ \left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \frac{\color{blue}{{x}^{\left(-0.5\right)}}}{\sqrt{x + 1}}
\] |
metadata-eval [=>]32.4 | \[ \left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \frac{{x}^{\color{blue}{-0.5}}}{\sqrt{x + 1}}
\] |
+-commutative [=>]32.4 | \[ \left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \frac{{x}^{-0.5}}{\sqrt{\color{blue}{1 + x}}}
\] |
Simplified32.4%
[Start]32.4 | \[ \left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \frac{{x}^{-0.5}}{\sqrt{1 + x}}
\] |
|---|---|
associate-*r/ [=>]32.4 | \[ \color{blue}{\frac{\left(\sqrt{1 + x} - \sqrt{x}\right) \cdot {x}^{-0.5}}{\sqrt{1 + x}}}
\] |
*-rgt-identity [<=]32.4 | \[ \frac{\left(\sqrt{1 + x} - \sqrt{x}\right) \cdot {x}^{-0.5}}{\color{blue}{\sqrt{1 + x} \cdot 1}}
\] |
times-frac [=>]32.4 | \[ \color{blue}{\frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{1 + x}} \cdot \frac{{x}^{-0.5}}{1}}
\] |
div-sub [=>]32.4 | \[ \color{blue}{\left(\frac{\sqrt{1 + x}}{\sqrt{1 + x}} - \frac{\sqrt{x}}{\sqrt{1 + x}}\right)} \cdot \frac{{x}^{-0.5}}{1}
\] |
*-inverses [=>]32.4 | \[ \left(\color{blue}{1} - \frac{\sqrt{x}}{\sqrt{1 + x}}\right) \cdot \frac{{x}^{-0.5}}{1}
\] |
/-rgt-identity [=>]32.4 | \[ \left(1 - \frac{\sqrt{x}}{\sqrt{1 + x}}\right) \cdot \color{blue}{{x}^{-0.5}}
\] |
Taylor expanded in x around inf 99.6%
Simplified99.6%
[Start]99.6 | \[ \left(\left(0.5 \cdot \frac{1}{x} + 0.3125 \cdot \frac{1}{{x}^{3}}\right) - 0.375 \cdot \frac{1}{{x}^{2}}\right) \cdot {x}^{-0.5}
\] |
|---|---|
associate--l+ [=>]99.6 | \[ \color{blue}{\left(0.5 \cdot \frac{1}{x} + \left(0.3125 \cdot \frac{1}{{x}^{3}} - 0.375 \cdot \frac{1}{{x}^{2}}\right)\right)} \cdot {x}^{-0.5}
\] |
associate-*r/ [=>]99.6 | \[ \left(\color{blue}{\frac{0.5 \cdot 1}{x}} + \left(0.3125 \cdot \frac{1}{{x}^{3}} - 0.375 \cdot \frac{1}{{x}^{2}}\right)\right) \cdot {x}^{-0.5}
\] |
metadata-eval [=>]99.6 | \[ \left(\frac{\color{blue}{0.5}}{x} + \left(0.3125 \cdot \frac{1}{{x}^{3}} - 0.375 \cdot \frac{1}{{x}^{2}}\right)\right) \cdot {x}^{-0.5}
\] |
associate-*r/ [=>]99.6 | \[ \left(\frac{0.5}{x} + \left(\color{blue}{\frac{0.3125 \cdot 1}{{x}^{3}}} - 0.375 \cdot \frac{1}{{x}^{2}}\right)\right) \cdot {x}^{-0.5}
\] |
metadata-eval [=>]99.6 | \[ \left(\frac{0.5}{x} + \left(\frac{\color{blue}{0.3125}}{{x}^{3}} - 0.375 \cdot \frac{1}{{x}^{2}}\right)\right) \cdot {x}^{-0.5}
\] |
associate-*r/ [=>]99.6 | \[ \left(\frac{0.5}{x} + \left(\frac{0.3125}{{x}^{3}} - \color{blue}{\frac{0.375 \cdot 1}{{x}^{2}}}\right)\right) \cdot {x}^{-0.5}
\] |
metadata-eval [=>]99.6 | \[ \left(\frac{0.5}{x} + \left(\frac{0.3125}{{x}^{3}} - \frac{\color{blue}{0.375}}{{x}^{2}}\right)\right) \cdot {x}^{-0.5}
\] |
unpow2 [=>]99.6 | \[ \left(\frac{0.5}{x} + \left(\frac{0.3125}{{x}^{3}} - \frac{0.375}{\color{blue}{x \cdot x}}\right)\right) \cdot {x}^{-0.5}
\] |
if 5.0000000000000001e-9 < (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) Initial program 99.4%
Applied egg-rr99.8%
[Start]99.4 | \[ \frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\] |
|---|---|
*-un-lft-identity [=>]99.4 | \[ \color{blue}{1 \cdot \frac{1}{\sqrt{x}}} - \frac{1}{\sqrt{x + 1}}
\] |
clear-num [=>]99.4 | \[ 1 \cdot \frac{1}{\sqrt{x}} - \color{blue}{\frac{1}{\frac{\sqrt{x + 1}}{1}}}
\] |
associate-/r/ [=>]99.4 | \[ 1 \cdot \frac{1}{\sqrt{x}} - \color{blue}{\frac{1}{\sqrt{x + 1}} \cdot 1}
\] |
prod-diff [=>]99.4 | \[ \color{blue}{\mathsf{fma}\left(1, \frac{1}{\sqrt{x}}, -1 \cdot \frac{1}{\sqrt{x + 1}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)}
\] |
*-un-lft-identity [<=]99.4 | \[ \mathsf{fma}\left(1, \frac{1}{\sqrt{x}}, -\color{blue}{\frac{1}{\sqrt{x + 1}}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
fma-neg [<=]99.4 | \[ \color{blue}{\left(1 \cdot \frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}\right)} + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
*-un-lft-identity [<=]99.4 | \[ \left(\color{blue}{\frac{1}{\sqrt{x}}} - \frac{1}{\sqrt{x + 1}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
inv-pow [=>]99.4 | \[ \left(\color{blue}{{\left(\sqrt{x}\right)}^{-1}} - \frac{1}{\sqrt{x + 1}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
sqrt-pow2 [=>]99.8 | \[ \left(\color{blue}{{x}^{\left(\frac{-1}{2}\right)}} - \frac{1}{\sqrt{x + 1}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
metadata-eval [=>]99.8 | \[ \left({x}^{\color{blue}{-0.5}} - \frac{1}{\sqrt{x + 1}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
pow1/2 [=>]99.8 | \[ \left({x}^{-0.5} - \frac{1}{\color{blue}{{\left(x + 1\right)}^{0.5}}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
pow-flip [=>]99.8 | \[ \left({x}^{-0.5} - \color{blue}{{\left(x + 1\right)}^{\left(-0.5\right)}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
+-commutative [=>]99.8 | \[ \left({x}^{-0.5} - {\color{blue}{\left(1 + x\right)}}^{\left(-0.5\right)}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
metadata-eval [=>]99.8 | \[ \left({x}^{-0.5} - {\left(1 + x\right)}^{\color{blue}{-0.5}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
Simplified99.8%
[Start]99.8 | \[ \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)
\] |
|---|---|
fma-udef [=>]99.8 | \[ \left({x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\right) + \color{blue}{\left(-1 \cdot {\left(1 + x\right)}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right)}
\] |
distribute-lft1-in [=>]99.8 | \[ \left({x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\right) + \color{blue}{\left(-1 + 1\right) \cdot {\left(1 + x\right)}^{-0.5}}
\] |
metadata-eval [=>]99.8 | \[ \left({x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\right) + \color{blue}{0} \cdot {\left(1 + x\right)}^{-0.5}
\] |
mul0-lft [=>]99.8 | \[ \left({x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\right) + \color{blue}{0}
\] |
+-rgt-identity [=>]99.8 | \[ \color{blue}{{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}}
\] |
Applied egg-rr99.9%
[Start]99.8 | \[ {x}^{-0.5} - {\left(1 + x\right)}^{-0.5}
\] |
|---|---|
sub-neg [=>]99.8 | \[ \color{blue}{{x}^{-0.5} + \left(-{\left(1 + x\right)}^{-0.5}\right)}
\] |
+-commutative [=>]99.8 | \[ \color{blue}{\left(-{\left(1 + x\right)}^{-0.5}\right) + {x}^{-0.5}}
\] |
add-cube-cbrt [=>]99.8 | \[ \left(-\color{blue}{\left(\sqrt[3]{{\left(1 + x\right)}^{-0.5}} \cdot \sqrt[3]{{\left(1 + x\right)}^{-0.5}}\right) \cdot \sqrt[3]{{\left(1 + x\right)}^{-0.5}}}\right) + {x}^{-0.5}
\] |
distribute-lft-neg-in [=>]99.8 | \[ \color{blue}{\left(-\sqrt[3]{{\left(1 + x\right)}^{-0.5}} \cdot \sqrt[3]{{\left(1 + x\right)}^{-0.5}}\right) \cdot \sqrt[3]{{\left(1 + x\right)}^{-0.5}}} + {x}^{-0.5}
\] |
fma-def [=>]99.9 | \[ \color{blue}{\mathsf{fma}\left(-\sqrt[3]{{\left(1 + x\right)}^{-0.5}} \cdot \sqrt[3]{{\left(1 + x\right)}^{-0.5}}, \sqrt[3]{{\left(1 + x\right)}^{-0.5}}, {x}^{-0.5}\right)}
\] |
cbrt-unprod [=>]99.9 | \[ \mathsf{fma}\left(-\color{blue}{\sqrt[3]{{\left(1 + x\right)}^{-0.5} \cdot {\left(1 + x\right)}^{-0.5}}}, \sqrt[3]{{\left(1 + x\right)}^{-0.5}}, {x}^{-0.5}\right)
\] |
pow-prod-up [=>]99.9 | \[ \mathsf{fma}\left(-\sqrt[3]{\color{blue}{{\left(1 + x\right)}^{\left(-0.5 + -0.5\right)}}}, \sqrt[3]{{\left(1 + x\right)}^{-0.5}}, {x}^{-0.5}\right)
\] |
metadata-eval [=>]99.9 | \[ \mathsf{fma}\left(-\sqrt[3]{{\left(1 + x\right)}^{\color{blue}{-1}}}, \sqrt[3]{{\left(1 + x\right)}^{-0.5}}, {x}^{-0.5}\right)
\] |
unpow-1 [=>]99.9 | \[ \mathsf{fma}\left(-\sqrt[3]{\color{blue}{\frac{1}{1 + x}}}, \sqrt[3]{{\left(1 + x\right)}^{-0.5}}, {x}^{-0.5}\right)
\] |
+-commutative [=>]99.9 | \[ \mathsf{fma}\left(-\sqrt[3]{\frac{1}{\color{blue}{x + 1}}}, \sqrt[3]{{\left(1 + x\right)}^{-0.5}}, {x}^{-0.5}\right)
\] |
pow1/3 [=>]99.9 | \[ \mathsf{fma}\left(-\sqrt[3]{\frac{1}{x + 1}}, \color{blue}{{\left({\left(1 + x\right)}^{-0.5}\right)}^{0.3333333333333333}}, {x}^{-0.5}\right)
\] |
pow-pow [=>]99.9 | \[ \mathsf{fma}\left(-\sqrt[3]{\frac{1}{x + 1}}, \color{blue}{{\left(1 + x\right)}^{\left(-0.5 \cdot 0.3333333333333333\right)}}, {x}^{-0.5}\right)
\] |
+-commutative [=>]99.9 | \[ \mathsf{fma}\left(-\sqrt[3]{\frac{1}{x + 1}}, {\color{blue}{\left(x + 1\right)}}^{\left(-0.5 \cdot 0.3333333333333333\right)}, {x}^{-0.5}\right)
\] |
metadata-eval [=>]99.9 | \[ \mathsf{fma}\left(-\sqrt[3]{\frac{1}{x + 1}}, {\left(x + 1\right)}^{\color{blue}{-0.16666666666666666}}, {x}^{-0.5}\right)
\] |
Final simplification99.8%
| Alternative 1 | |
|---|---|
| Accuracy | 99.7% |
| Cost | 27332 |
| Alternative 2 | |
|---|---|
| Accuracy | 99.7% |
| Cost | 27204 |
| Alternative 3 | |
|---|---|
| Accuracy | 99.7% |
| Cost | 26948 |
| Alternative 4 | |
|---|---|
| Accuracy | 99.7% |
| Cost | 26692 |
| Alternative 5 | |
|---|---|
| Accuracy | 99.0% |
| Cost | 7172 |
| Alternative 6 | |
|---|---|
| Accuracy | 98.7% |
| Cost | 7044 |
| Alternative 7 | |
|---|---|
| Accuracy | 97.0% |
| Cost | 6788 |
| Alternative 8 | |
|---|---|
| Accuracy | 98.4% |
| Cost | 6788 |
| Alternative 9 | |
|---|---|
| Accuracy | 51.6% |
| Cost | 6528 |
| Alternative 10 | |
|---|---|
| Accuracy | 4.8% |
| Cost | 576 |
| Alternative 11 | |
|---|---|
| Accuracy | 1.9% |
| Cost | 64 |
herbie shell --seed 2023159
(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)))))