| Alternative 1 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 33284 |

(FPCore (p x) :precision binary64 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
(FPCore (p x)
:precision binary64
(let* ((t_0 (cbrt (/ x (hypot x (+ p p))))))
(if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -0.99999998)
(fabs (/ p x))
(sqrt (* 0.5 (fma (pow t_0 2.0) t_0 1.0))))))double code(double p, double x) {
return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
double code(double p, double x) {
double t_0 = cbrt((x / hypot(x, (p + p))));
double tmp;
if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.99999998) {
tmp = fabs((p / x));
} else {
tmp = sqrt((0.5 * fma(pow(t_0, 2.0), t_0, 1.0)));
}
return tmp;
}
function code(p, x) return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))))) end
function code(p, x) t_0 = cbrt(Float64(x / hypot(x, Float64(p + p)))) tmp = 0.0 if (Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) <= -0.99999998) tmp = abs(Float64(p / x)); else tmp = sqrt(Float64(0.5 * fma((t_0 ^ 2.0), t_0, 1.0))); end return tmp end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[p_, x_] := Block[{t$95$0 = N[Power[N[(x / N[Sqrt[x ^ 2 + N[(p + p), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]}, If[LessEqual[N[(x / N[Sqrt[N[(N[(p * N[(4.0 * p), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -0.99999998], N[Abs[N[(p / x), $MachinePrecision]], $MachinePrecision], N[Sqrt[N[(0.5 * N[(N[Power[t$95$0, 2.0], $MachinePrecision] * t$95$0 + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\begin{array}{l}
t_0 := \sqrt[3]{\frac{x}{\mathsf{hypot}\left(x, p + p\right)}}\\
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.99999998:\\
\;\;\;\;\left|\frac{p}{x}\right|\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \mathsf{fma}\left({t_0}^{2}, t_0, 1\right)}\\
\end{array}
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
| Original | 79.5% |
|---|---|
| Target | 79.5% |
| Herbie | 99.9% |
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -0.999999980000000011Initial program 11.5%
Applied egg-rr11.5%
[Start]11.5 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\] |
|---|---|
add-exp-log [=>]11.5 | \[ \sqrt{0.5 \cdot \color{blue}{e^{\log \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}}
\] |
log1p-udef [<=]11.5 | \[ \sqrt{0.5 \cdot e^{\color{blue}{\mathsf{log1p}\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}}
\] |
+-commutative [=>]11.5 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}}\right)}}
\] |
add-sqr-sqrt [=>]11.5 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\sqrt{x \cdot x + \color{blue}{\sqrt{\left(4 \cdot p\right) \cdot p} \cdot \sqrt{\left(4 \cdot p\right) \cdot p}}}}\right)}}
\] |
hypot-def [=>]11.5 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\color{blue}{\mathsf{hypot}\left(x, \sqrt{\left(4 \cdot p\right) \cdot p}\right)}}\right)}}
\] |
associate-*l* [=>]11.5 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, \sqrt{\color{blue}{4 \cdot \left(p \cdot p\right)}}\right)}\right)}}
\] |
sqrt-prod [=>]11.5 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, \color{blue}{\sqrt{4} \cdot \sqrt{p \cdot p}}\right)}\right)}}
\] |
metadata-eval [=>]11.5 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, \color{blue}{2} \cdot \sqrt{p \cdot p}\right)}\right)}}
\] |
sqrt-unprod [<=]6.8 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot \color{blue}{\left(\sqrt{p} \cdot \sqrt{p}\right)}\right)}\right)}}
\] |
add-sqr-sqrt [<=]11.5 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot \color{blue}{p}\right)}\right)}}
\] |
Taylor expanded in x around -inf 46.6%
Simplified49.4%
[Start]46.6 | \[ \sqrt{0.5} \cdot \sqrt{e^{2 \cdot \log \left(\frac{-1}{x}\right) + \log \left(--2 \cdot {p}^{2}\right)}}
\] |
|---|---|
exp-sum [=>]46.7 | \[ \sqrt{0.5} \cdot \sqrt{\color{blue}{e^{2 \cdot \log \left(\frac{-1}{x}\right)} \cdot e^{\log \left(--2 \cdot {p}^{2}\right)}}}
\] |
*-commutative [=>]46.7 | \[ \sqrt{0.5} \cdot \sqrt{e^{\color{blue}{\log \left(\frac{-1}{x}\right) \cdot 2}} \cdot e^{\log \left(--2 \cdot {p}^{2}\right)}}
\] |
exp-to-pow [=>]46.9 | \[ \sqrt{0.5} \cdot \sqrt{\color{blue}{{\left(\frac{-1}{x}\right)}^{2}} \cdot e^{\log \left(--2 \cdot {p}^{2}\right)}}
\] |
rem-exp-log [=>]49.4 | \[ \sqrt{0.5} \cdot \sqrt{{\left(\frac{-1}{x}\right)}^{2} \cdot \color{blue}{\left(--2 \cdot {p}^{2}\right)}}
\] |
distribute-lft-neg-in [=>]49.4 | \[ \sqrt{0.5} \cdot \sqrt{{\left(\frac{-1}{x}\right)}^{2} \cdot \color{blue}{\left(\left(--2\right) \cdot {p}^{2}\right)}}
\] |
metadata-eval [=>]49.4 | \[ \sqrt{0.5} \cdot \sqrt{{\left(\frac{-1}{x}\right)}^{2} \cdot \left(\color{blue}{2} \cdot {p}^{2}\right)}
\] |
unpow2 [=>]49.4 | \[ \sqrt{0.5} \cdot \sqrt{{\left(\frac{-1}{x}\right)}^{2} \cdot \left(2 \cdot \color{blue}{\left(p \cdot p\right)}\right)}
\] |
Applied egg-rr46.2%
[Start]49.4 | \[ \sqrt{0.5} \cdot \sqrt{{\left(\frac{-1}{x}\right)}^{2} \cdot \left(2 \cdot \left(p \cdot p\right)\right)}
\] |
|---|---|
add-cube-cbrt [=>]49.0 | \[ \color{blue}{\left(\sqrt[3]{\sqrt{0.5} \cdot \sqrt{{\left(\frac{-1}{x}\right)}^{2} \cdot \left(2 \cdot \left(p \cdot p\right)\right)}} \cdot \sqrt[3]{\sqrt{0.5} \cdot \sqrt{{\left(\frac{-1}{x}\right)}^{2} \cdot \left(2 \cdot \left(p \cdot p\right)\right)}}\right) \cdot \sqrt[3]{\sqrt{0.5} \cdot \sqrt{{\left(\frac{-1}{x}\right)}^{2} \cdot \left(2 \cdot \left(p \cdot p\right)\right)}}}
\] |
pow3 [=>]49.0 | \[ \color{blue}{{\left(\sqrt[3]{\sqrt{0.5} \cdot \sqrt{{\left(\frac{-1}{x}\right)}^{2} \cdot \left(2 \cdot \left(p \cdot p\right)\right)}}\right)}^{3}}
\] |
Simplified99.7%
[Start]46.2 | \[ {\left({\left(\left(2 \cdot \left(\left(p \cdot p\right) \cdot {x}^{-2}\right)\right) \cdot 0.5\right)}^{0.16666666666666666}\right)}^{3}
\] |
|---|---|
exp-to-pow [<=]46.2 | \[ \color{blue}{e^{\log \left({\left(\left(2 \cdot \left(\left(p \cdot p\right) \cdot {x}^{-2}\right)\right) \cdot 0.5\right)}^{0.16666666666666666}\right) \cdot 3}}
\] |
*-commutative [=>]46.2 | \[ e^{\color{blue}{3 \cdot \log \left({\left(\left(2 \cdot \left(\left(p \cdot p\right) \cdot {x}^{-2}\right)\right) \cdot 0.5\right)}^{0.16666666666666666}\right)}}
\] |
log-pow [=>]46.4 | \[ e^{3 \cdot \color{blue}{\left(0.16666666666666666 \cdot \log \left(\left(2 \cdot \left(\left(p \cdot p\right) \cdot {x}^{-2}\right)\right) \cdot 0.5\right)\right)}}
\] |
associate-*r* [=>]46.8 | \[ e^{\color{blue}{\left(3 \cdot 0.16666666666666666\right) \cdot \log \left(\left(2 \cdot \left(\left(p \cdot p\right) \cdot {x}^{-2}\right)\right) \cdot 0.5\right)}}
\] |
metadata-eval [=>]46.8 | \[ e^{\color{blue}{0.5} \cdot \log \left(\left(2 \cdot \left(\left(p \cdot p\right) \cdot {x}^{-2}\right)\right) \cdot 0.5\right)}
\] |
*-commutative [<=]46.8 | \[ e^{\color{blue}{\log \left(\left(2 \cdot \left(\left(p \cdot p\right) \cdot {x}^{-2}\right)\right) \cdot 0.5\right) \cdot 0.5}}
\] |
exp-to-pow [=>]49.5 | \[ \color{blue}{{\left(\left(2 \cdot \left(\left(p \cdot p\right) \cdot {x}^{-2}\right)\right) \cdot 0.5\right)}^{0.5}}
\] |
unpow1/2 [=>]49.5 | \[ \color{blue}{\sqrt{\left(2 \cdot \left(\left(p \cdot p\right) \cdot {x}^{-2}\right)\right) \cdot 0.5}}
\] |
*-commutative [=>]49.5 | \[ \sqrt{\color{blue}{\left(\left(\left(p \cdot p\right) \cdot {x}^{-2}\right) \cdot 2\right)} \cdot 0.5}
\] |
associate-*l* [=>]49.5 | \[ \sqrt{\color{blue}{\left(\left(p \cdot p\right) \cdot {x}^{-2}\right) \cdot \left(2 \cdot 0.5\right)}}
\] |
unpow2 [<=]49.5 | \[ \sqrt{\left(\color{blue}{{p}^{2}} \cdot {x}^{-2}\right) \cdot \left(2 \cdot 0.5\right)}
\] |
metadata-eval [=>]49.5 | \[ \sqrt{\left({p}^{2} \cdot {x}^{-2}\right) \cdot \color{blue}{1}}
\] |
associate-*r* [<=]49.5 | \[ \sqrt{\color{blue}{{p}^{2} \cdot \left({x}^{-2} \cdot 1\right)}}
\] |
unpow2 [=>]49.5 | \[ \sqrt{\color{blue}{\left(p \cdot p\right)} \cdot \left({x}^{-2} \cdot 1\right)}
\] |
*-rgt-identity [=>]49.5 | \[ \sqrt{\left(p \cdot p\right) \cdot \color{blue}{{x}^{-2}}}
\] |
metadata-eval [<=]49.5 | \[ \sqrt{\left(p \cdot p\right) \cdot {x}^{\color{blue}{\left(2 \cdot -1\right)}}}
\] |
pow-sqr [<=]49.3 | \[ \sqrt{\left(p \cdot p\right) \cdot \color{blue}{\left({x}^{-1} \cdot {x}^{-1}\right)}}
\] |
if -0.999999980000000011 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 99.8%
Applied egg-rr99.8%
[Start]99.8 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\] |
|---|---|
add-exp-log [=>]99.8 | \[ \sqrt{0.5 \cdot \color{blue}{e^{\log \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}}
\] |
log1p-udef [<=]99.8 | \[ \sqrt{0.5 \cdot e^{\color{blue}{\mathsf{log1p}\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}}
\] |
+-commutative [=>]99.8 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}}\right)}}
\] |
add-sqr-sqrt [=>]99.8 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\sqrt{x \cdot x + \color{blue}{\sqrt{\left(4 \cdot p\right) \cdot p} \cdot \sqrt{\left(4 \cdot p\right) \cdot p}}}}\right)}}
\] |
hypot-def [=>]99.8 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\color{blue}{\mathsf{hypot}\left(x, \sqrt{\left(4 \cdot p\right) \cdot p}\right)}}\right)}}
\] |
associate-*l* [=>]99.8 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, \sqrt{\color{blue}{4 \cdot \left(p \cdot p\right)}}\right)}\right)}}
\] |
sqrt-prod [=>]99.8 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, \color{blue}{\sqrt{4} \cdot \sqrt{p \cdot p}}\right)}\right)}}
\] |
metadata-eval [=>]99.8 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, \color{blue}{2} \cdot \sqrt{p \cdot p}\right)}\right)}}
\] |
sqrt-unprod [<=]48.5 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot \color{blue}{\left(\sqrt{p} \cdot \sqrt{p}\right)}\right)}\right)}}
\] |
add-sqr-sqrt [<=]99.8 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot \color{blue}{p}\right)}\right)}}
\] |
Applied egg-rr99.8%
[Start]99.8 | \[ \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right)}}
\] |
|---|---|
log1p-udef [=>]99.8 | \[ \sqrt{0.5 \cdot e^{\color{blue}{\log \left(1 + \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right)}}}
\] |
add-exp-log [<=]99.8 | \[ \sqrt{0.5 \cdot \color{blue}{\left(1 + \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right)}}
\] |
+-commutative [=>]99.8 | \[ \sqrt{0.5 \cdot \color{blue}{\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)} + 1\right)}}
\] |
add-cube-cbrt [=>]99.8 | \[ \sqrt{0.5 \cdot \left(\color{blue}{\left(\sqrt[3]{\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}} \cdot \sqrt[3]{\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right) \cdot \sqrt[3]{\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}} + 1\right)}
\] |
fma-def [=>]99.8 | \[ \sqrt{0.5 \cdot \color{blue}{\mathsf{fma}\left(\sqrt[3]{\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}} \cdot \sqrt[3]{\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}, \sqrt[3]{\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}, 1\right)}}
\] |
Final simplification99.8%
| Alternative 1 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 33284 |
| Alternative 2 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 20612 |
| Alternative 3 | |
|---|---|
| Accuracy | 69.2% |
| Cost | 7256 |
| Alternative 4 | |
|---|---|
| Accuracy | 69.0% |
| Cost | 7256 |
| Alternative 5 | |
|---|---|
| Accuracy | 68.8% |
| Cost | 6992 |
| Alternative 6 | |
|---|---|
| Accuracy | 43.7% |
| Cost | 324 |
| Alternative 7 | |
|---|---|
| Accuracy | 36.1% |
| Cost | 64 |
herbie shell --seed 2023160
(FPCore (p x)
:name "Given's Rotation SVD example"
:precision binary64
:pre (and (< 1e-150 (fabs x)) (< (fabs x) 1e+150))
:herbie-target
(sqrt (+ 0.5 (/ (copysign 0.5 x) (hypot 1.0 (/ (* 2.0 p) x)))))
(sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))