| Alternative 1 | |
|---|---|
| Accuracy | 99.5% |
| Cost | 52224 |

(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x) :precision binary64 (let* ((t_0 (+ (sqrt x) (sqrt (+ x 1.0))))) (if (<= x 3.5e+130) (/ (pow (fma x x x) -0.5) t_0) (/ (/ 1.0 t_0) x))))
double code(double x) {
return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
double code(double x) {
double t_0 = sqrt(x) + sqrt((x + 1.0));
double tmp;
if (x <= 3.5e+130) {
tmp = pow(fma(x, x, x), -0.5) / t_0;
} else {
tmp = (1.0 / t_0) / 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(x) + sqrt(Float64(x + 1.0))) tmp = 0.0 if (x <= 3.5e+130) tmp = Float64((fma(x, x, x) ^ -0.5) / t_0); else tmp = Float64(Float64(1.0 / t_0) / 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[x], $MachinePrecision] + N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, 3.5e+130], N[(N[Power[N[(x * x + x), $MachinePrecision], -0.5], $MachinePrecision] / t$95$0), $MachinePrecision], N[(N[(1.0 / t$95$0), $MachinePrecision] / x), $MachinePrecision]]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
t_0 := \sqrt{x} + \sqrt{x + 1}\\
\mathbf{if}\;x \leq 3.5 \cdot 10^{+130}:\\
\;\;\;\;\frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}}{t_0}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{t_0}}{x}\\
\end{array}
Herbie found 14 alternatives:
| Alternative | Accuracy | Speedup |
|---|
| Original | 68.8% |
|---|---|
| Target | 99.0% |
| Herbie | 99.8% |
if x < 3.5000000000000001e130Initial program 71.4%
Applied egg-rr71.4%
[Start]71.4 | \[ \frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\] |
|---|---|
frac-sub [=>]71.4 | \[ \color{blue}{\frac{1 \cdot \sqrt{x + 1} - \sqrt{x} \cdot 1}{\sqrt{x} \cdot \sqrt{x + 1}}}
\] |
*-un-lft-identity [<=]71.4 | \[ \frac{\color{blue}{\sqrt{x + 1}} - \sqrt{x} \cdot 1}{\sqrt{x} \cdot \sqrt{x + 1}}
\] |
+-commutative [=>]71.4 | \[ \frac{\sqrt{\color{blue}{1 + x}} - \sqrt{x} \cdot 1}{\sqrt{x} \cdot \sqrt{x + 1}}
\] |
*-rgt-identity [=>]71.4 | \[ \frac{\sqrt{1 + x} - \color{blue}{\sqrt{x}}}{\sqrt{x} \cdot \sqrt{x + 1}}
\] |
sqrt-unprod [=>]71.4 | \[ \frac{\sqrt{1 + x} - \sqrt{x}}{\color{blue}{\sqrt{x \cdot \left(x + 1\right)}}}
\] |
+-commutative [=>]71.4 | \[ \frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x \cdot \color{blue}{\left(1 + x\right)}}}
\] |
Applied egg-rr99.5%
[Start]71.4 | \[ \frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
|---|---|
flip-- [=>]71.6 | \[ \frac{\color{blue}{\frac{\sqrt{1 + x} \cdot \sqrt{1 + x} - \sqrt{x} \cdot \sqrt{x}}{\sqrt{1 + x} + \sqrt{x}}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
div-inv [=>]71.6 | \[ \frac{\color{blue}{\left(\sqrt{1 + x} \cdot \sqrt{1 + x} - \sqrt{x} \cdot \sqrt{x}\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
add-sqr-sqrt [<=]72.2 | \[ \frac{\left(\color{blue}{\left(1 + x\right)} - \sqrt{x} \cdot \sqrt{x}\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
add-sqr-sqrt [<=]73.5 | \[ \frac{\left(\left(1 + x\right) - \color{blue}{x}\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
associate--l+ [=>]99.5 | \[ \frac{\color{blue}{\left(1 + \left(x - x\right)\right)} \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
Simplified99.5%
[Start]99.5 | \[ \frac{\left(1 + \left(x - x\right)\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
|---|---|
+-inverses [=>]99.5 | \[ \frac{\left(1 + \color{blue}{0}\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
metadata-eval [=>]99.5 | \[ \frac{\color{blue}{1} \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
*-lft-identity [=>]99.5 | \[ \frac{\color{blue}{\frac{1}{\sqrt{1 + x} + \sqrt{x}}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
rem-square-sqrt [<=]99.5 | \[ \frac{\frac{1}{\sqrt{1 + \color{blue}{\sqrt{x} \cdot \sqrt{x}}} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
hypot-1-def [=>]99.5 | \[ \frac{\frac{1}{\color{blue}{\mathsf{hypot}\left(1, \sqrt{x}\right)} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
Applied egg-rr99.9%
[Start]99.5 | \[ \frac{\frac{1}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
|---|---|
add-log-exp [=>]8.7 | \[ \color{blue}{\log \left(e^{\frac{\frac{1}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}}\right)}
\] |
*-un-lft-identity [=>]8.7 | \[ \log \color{blue}{\left(1 \cdot e^{\frac{\frac{1}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}}\right)}
\] |
log-prod [=>]8.7 | \[ \color{blue}{\log 1 + \log \left(e^{\frac{\frac{1}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}}\right)}
\] |
metadata-eval [=>]8.7 | \[ \color{blue}{0} + \log \left(e^{\frac{\frac{1}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}}\right)
\] |
add-log-exp [<=]99.5 | \[ 0 + \color{blue}{\frac{\frac{1}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}}
\] |
associate-/l/ [=>]99.7 | \[ 0 + \color{blue}{\frac{1}{\sqrt{x \cdot \left(1 + x\right)} \cdot \left(\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}\right)}}
\] |
associate-/r* [=>]99.7 | \[ 0 + \color{blue}{\frac{\frac{1}{\sqrt{x \cdot \left(1 + x\right)}}}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}}
\] |
pow1/2 [=>]99.7 | \[ 0 + \frac{\frac{1}{\color{blue}{{\left(x \cdot \left(1 + x\right)\right)}^{0.5}}}}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}
\] |
pow-flip [=>]99.8 | \[ 0 + \frac{\color{blue}{{\left(x \cdot \left(1 + x\right)\right)}^{\left(-0.5\right)}}}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}
\] |
+-commutative [=>]99.8 | \[ 0 + \frac{{\left(x \cdot \color{blue}{\left(x + 1\right)}\right)}^{\left(-0.5\right)}}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}
\] |
distribute-lft-in [=>]99.8 | \[ 0 + \frac{{\color{blue}{\left(x \cdot x + x \cdot 1\right)}}^{\left(-0.5\right)}}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}
\] |
*-rgt-identity [=>]99.8 | \[ 0 + \frac{{\left(x \cdot x + \color{blue}{x}\right)}^{\left(-0.5\right)}}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}
\] |
fma-def [=>]99.8 | \[ 0 + \frac{{\color{blue}{\left(\mathsf{fma}\left(x, x, x\right)\right)}}^{\left(-0.5\right)}}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}
\] |
metadata-eval [=>]99.8 | \[ 0 + \frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{\color{blue}{-0.5}}}{\mathsf{hypot}\left(1, \sqrt{x}\right) + \sqrt{x}}
\] |
+-commutative [=>]99.8 | \[ 0 + \frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}}{\color{blue}{\sqrt{x} + \mathsf{hypot}\left(1, \sqrt{x}\right)}}
\] |
hypot-1-def [<=]99.8 | \[ 0 + \frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}}{\sqrt{x} + \color{blue}{\sqrt{1 + \sqrt{x} \cdot \sqrt{x}}}}
\] |
add-sqr-sqrt [<=]99.9 | \[ 0 + \frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}}{\sqrt{x} + \sqrt{1 + \color{blue}{x}}}
\] |
Simplified99.9%
[Start]99.9 | \[ 0 + \frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}}{\sqrt{x} + \sqrt{1 + x}}
\] |
|---|---|
+-lft-identity [=>]99.9 | \[ \color{blue}{\frac{{\left(\mathsf{fma}\left(x, x, x\right)\right)}^{-0.5}}{\sqrt{x} + \sqrt{1 + x}}}
\] |
if 3.5000000000000001e130 < x Initial program 54.9%
Applied egg-rr54.9%
[Start]54.9 | \[ \frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\] |
|---|---|
frac-sub [=>]54.9 | \[ \color{blue}{\frac{1 \cdot \sqrt{x + 1} - \sqrt{x} \cdot 1}{\sqrt{x} \cdot \sqrt{x + 1}}}
\] |
*-un-lft-identity [<=]54.9 | \[ \frac{\color{blue}{\sqrt{x + 1}} - \sqrt{x} \cdot 1}{\sqrt{x} \cdot \sqrt{x + 1}}
\] |
+-commutative [=>]54.9 | \[ \frac{\sqrt{\color{blue}{1 + x}} - \sqrt{x} \cdot 1}{\sqrt{x} \cdot \sqrt{x + 1}}
\] |
*-rgt-identity [=>]54.9 | \[ \frac{\sqrt{1 + x} - \color{blue}{\sqrt{x}}}{\sqrt{x} \cdot \sqrt{x + 1}}
\] |
sqrt-unprod [=>]54.9 | \[ \frac{\sqrt{1 + x} - \sqrt{x}}{\color{blue}{\sqrt{x \cdot \left(x + 1\right)}}}
\] |
+-commutative [=>]54.9 | \[ \frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x \cdot \color{blue}{\left(1 + x\right)}}}
\] |
Applied egg-rr65.2%
[Start]54.9 | \[ \frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
|---|---|
flip-- [=>]54.9 | \[ \frac{\color{blue}{\frac{\sqrt{1 + x} \cdot \sqrt{1 + x} - \sqrt{x} \cdot \sqrt{x}}{\sqrt{1 + x} + \sqrt{x}}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
div-inv [=>]54.9 | \[ \frac{\color{blue}{\left(\sqrt{1 + x} \cdot \sqrt{1 + x} - \sqrt{x} \cdot \sqrt{x}\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
add-sqr-sqrt [<=]54.9 | \[ \frac{\left(\color{blue}{\left(1 + x\right)} - \sqrt{x} \cdot \sqrt{x}\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
add-sqr-sqrt [<=]54.9 | \[ \frac{\left(\left(1 + x\right) - \color{blue}{x}\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
associate--l+ [=>]65.2 | \[ \frac{\color{blue}{\left(1 + \left(x - x\right)\right)} \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
Simplified65.2%
[Start]65.2 | \[ \frac{\left(1 + \left(x - x\right)\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
|---|---|
+-inverses [=>]65.2 | \[ \frac{\left(1 + \color{blue}{0}\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
metadata-eval [=>]65.2 | \[ \frac{\color{blue}{1} \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
*-lft-identity [=>]65.2 | \[ \frac{\color{blue}{\frac{1}{\sqrt{1 + x} + \sqrt{x}}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
Taylor expanded in x around inf 99.8%
Final simplification99.9%
| Alternative 1 | |
|---|---|
| Accuracy | 99.5% |
| Cost | 52224 |
| Alternative 2 | |
|---|---|
| Accuracy | 99.7% |
| Cost | 27268 |
| Alternative 3 | |
|---|---|
| Accuracy | 99.8% |
| Cost | 27204 |
| Alternative 4 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 26692 |
| Alternative 5 | |
|---|---|
| Accuracy | 99.0% |
| Cost | 26304 |
| Alternative 6 | |
|---|---|
| Accuracy | 99.8% |
| Cost | 14276 |
| Alternative 7 | |
|---|---|
| Accuracy | 98.4% |
| Cost | 7108 |
| Alternative 8 | |
|---|---|
| Accuracy | 99.0% |
| Cost | 7108 |
| Alternative 9 | |
|---|---|
| Accuracy | 98.5% |
| Cost | 7044 |
| Alternative 10 | |
|---|---|
| Accuracy | 98.2% |
| Cost | 6852 |
| Alternative 11 | |
|---|---|
| Accuracy | 67.0% |
| Cost | 6788 |
| Alternative 12 | |
|---|---|
| Accuracy | 66.1% |
| Cost | 6724 |
| Alternative 13 | |
|---|---|
| Accuracy | 19.3% |
| Cost | 64 |
herbie shell --seed 2023161
(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)))))