| Alternative 1 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 20612 |
(FPCore (p x) :precision binary64 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
(FPCore (p x) :precision binary64 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -0.99999999) (/ (- (fabs p)) x) (exp (* 0.5 (log (+ 0.5 (* x (/ 0.5 (hypot x (+ p p))))))))))
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 tmp;
if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.99999999) {
tmp = -fabs(p) / x;
} else {
tmp = exp((0.5 * log((0.5 + (x * (0.5 / hypot(x, (p + p))))))));
}
return tmp;
}
public static double code(double p, double x) {
return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
public static double code(double p, double x) {
double tmp;
if ((x / Math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.99999999) {
tmp = -Math.abs(p) / x;
} else {
tmp = Math.exp((0.5 * Math.log((0.5 + (x * (0.5 / Math.hypot(x, (p + p))))))));
}
return tmp;
}
def code(p, x): return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
def code(p, x): tmp = 0 if (x / math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.99999999: tmp = -math.fabs(p) / x else: tmp = math.exp((0.5 * math.log((0.5 + (x * (0.5 / math.hypot(x, (p + p)))))))) 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) tmp = 0.0 if (Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) <= -0.99999999) tmp = Float64(Float64(-abs(p)) / x); else tmp = exp(Float64(0.5 * log(Float64(0.5 + Float64(x * Float64(0.5 / hypot(x, Float64(p + p)))))))); end return tmp end
function tmp = code(p, x) tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x))))))); end
function tmp_2 = code(p, x) tmp = 0.0; if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.99999999) tmp = -abs(p) / x; else tmp = exp((0.5 * log((0.5 + (x * (0.5 / hypot(x, (p + p)))))))); end tmp_2 = 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_] := If[LessEqual[N[(x / N[Sqrt[N[(N[(p * N[(4.0 * p), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -0.99999999], N[((-N[Abs[p], $MachinePrecision]) / x), $MachinePrecision], N[Exp[N[(0.5 * N[Log[N[(0.5 + N[(x * N[(0.5 / N[Sqrt[x ^ 2 + N[(p + p), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $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}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.99999999:\\
\;\;\;\;\frac{-\left|p\right|}{x}\\
\mathbf{else}:\\
\;\;\;\;e^{0.5 \cdot \log \left(0.5 + x \cdot \frac{0.5}{\mathsf{hypot}\left(x, p + p\right)}\right)}\\
\end{array}
Results
| Original | 79.7% |
|---|---|
| Target | 79.7% |
| Herbie | 99.8% |
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -0.99999998999999995Initial program 16.6%
Taylor expanded in x around -inf 56.8%
Simplified56.8%
[Start]56.8 | \[ -1 \cdot \frac{\sqrt{2} \cdot \left(\sqrt{0.5} \cdot p\right)}{x}
\] |
|---|---|
mul-1-neg [=>]56.8 | \[ \color{blue}{-\frac{\sqrt{2} \cdot \left(\sqrt{0.5} \cdot p\right)}{x}}
\] |
*-commutative [=>]56.8 | \[ -\frac{\sqrt{2} \cdot \color{blue}{\left(p \cdot \sqrt{0.5}\right)}}{x}
\] |
Applied egg-rr63.6%
[Start]56.8 | \[ -\frac{\sqrt{2} \cdot \left(p \cdot \sqrt{0.5}\right)}{x}
\] |
|---|---|
add-sqr-sqrt [=>]49.3 | \[ -\frac{\color{blue}{\sqrt{\sqrt{2} \cdot \left(p \cdot \sqrt{0.5}\right)} \cdot \sqrt{\sqrt{2} \cdot \left(p \cdot \sqrt{0.5}\right)}}}{x}
\] |
sqrt-unprod [=>]63.0 | \[ -\frac{\color{blue}{\sqrt{\left(\sqrt{2} \cdot \left(p \cdot \sqrt{0.5}\right)\right) \cdot \left(\sqrt{2} \cdot \left(p \cdot \sqrt{0.5}\right)\right)}}}{x}
\] |
swap-sqr [=>]62.8 | \[ -\frac{\sqrt{\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right) \cdot \left(\left(p \cdot \sqrt{0.5}\right) \cdot \left(p \cdot \sqrt{0.5}\right)\right)}}}{x}
\] |
add-sqr-sqrt [<=]63.3 | \[ -\frac{\sqrt{\color{blue}{2} \cdot \left(\left(p \cdot \sqrt{0.5}\right) \cdot \left(p \cdot \sqrt{0.5}\right)\right)}}{x}
\] |
*-commutative [=>]63.3 | \[ -\frac{\sqrt{2 \cdot \left(\color{blue}{\left(\sqrt{0.5} \cdot p\right)} \cdot \left(p \cdot \sqrt{0.5}\right)\right)}}{x}
\] |
*-commutative [=>]63.3 | \[ -\frac{\sqrt{2 \cdot \left(\left(\sqrt{0.5} \cdot p\right) \cdot \color{blue}{\left(\sqrt{0.5} \cdot p\right)}\right)}}{x}
\] |
swap-sqr [=>]63.1 | \[ -\frac{\sqrt{2 \cdot \color{blue}{\left(\left(\sqrt{0.5} \cdot \sqrt{0.5}\right) \cdot \left(p \cdot p\right)\right)}}}{x}
\] |
add-sqr-sqrt [<=]63.6 | \[ -\frac{\sqrt{2 \cdot \left(\color{blue}{0.5} \cdot \left(p \cdot p\right)\right)}}{x}
\] |
Simplified99.7%
[Start]63.6 | \[ -\frac{\sqrt{2 \cdot \left(0.5 \cdot \left(p \cdot p\right)\right)}}{x}
\] |
|---|---|
associate-*r* [=>]63.6 | \[ -\frac{\sqrt{\color{blue}{\left(2 \cdot 0.5\right) \cdot \left(p \cdot p\right)}}}{x}
\] |
metadata-eval [=>]63.6 | \[ -\frac{\sqrt{\color{blue}{1} \cdot \left(p \cdot p\right)}}{x}
\] |
*-lft-identity [=>]63.6 | \[ -\frac{\sqrt{\color{blue}{p \cdot p}}}{x}
\] |
rem-sqrt-square [=>]99.7 | \[ -\frac{\color{blue}{\left|p\right|}}{x}
\] |
if -0.99999998999999995 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 99.9%
Applied egg-rr99.9%
[Start]99.9 | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\] |
|---|---|
pow1/2 [=>]99.9 | \[ \color{blue}{{\left(0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)\right)}^{0.5}}
\] |
pow-to-exp [=>]99.9 | \[ \color{blue}{e^{\log \left(0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)\right) \cdot 0.5}}
\] |
Applied egg-rr99.9%
[Start]99.9 | \[ e^{\log \left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right) \cdot 0.5}
\] |
|---|---|
fma-udef [=>]99.9 | \[ e^{\log \color{blue}{\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot 0.5 + 0.5\right)} \cdot 0.5}
\] |
+-commutative [=>]99.9 | \[ e^{\log \color{blue}{\left(0.5 + \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot 0.5\right)} \cdot 0.5}
\] |
clear-num [=>]99.9 | \[ e^{\log \left(0.5 + \color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(x, 2 \cdot p\right)}{x}}} \cdot 0.5\right) \cdot 0.5}
\] |
associate-*l/ [=>]99.9 | \[ e^{\log \left(0.5 + \color{blue}{\frac{1 \cdot 0.5}{\frac{\mathsf{hypot}\left(x, 2 \cdot p\right)}{x}}}\right) \cdot 0.5}
\] |
metadata-eval [=>]99.9 | \[ e^{\log \left(0.5 + \frac{\color{blue}{0.5}}{\frac{\mathsf{hypot}\left(x, 2 \cdot p\right)}{x}}\right) \cdot 0.5}
\] |
add-log-exp [=>]90.3 | \[ e^{\log \left(0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, \color{blue}{\log \left(e^{2 \cdot p}\right)}\right)}{x}}\right) \cdot 0.5}
\] |
*-commutative [=>]90.3 | \[ e^{\log \left(0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, \log \left(e^{\color{blue}{p \cdot 2}}\right)\right)}{x}}\right) \cdot 0.5}
\] |
exp-lft-sqr [=>]90.2 | \[ e^{\log \left(0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, \log \color{blue}{\left(e^{p} \cdot e^{p}\right)}\right)}{x}}\right) \cdot 0.5}
\] |
log-prod [=>]90.2 | \[ e^{\log \left(0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, \color{blue}{\log \left(e^{p}\right) + \log \left(e^{p}\right)}\right)}{x}}\right) \cdot 0.5}
\] |
add-log-exp [<=]95.2 | \[ e^{\log \left(0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, \color{blue}{p} + \log \left(e^{p}\right)\right)}{x}}\right) \cdot 0.5}
\] |
add-log-exp [<=]99.9 | \[ e^{\log \left(0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, p + \color{blue}{p}\right)}{x}}\right) \cdot 0.5}
\] |
Simplified99.9%
[Start]99.9 | \[ e^{\log \left(0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, p + p\right)}{x}}\right) \cdot 0.5}
\] |
|---|---|
associate-/r/ [=>]99.9 | \[ e^{\log \left(0.5 + \color{blue}{\frac{0.5}{\mathsf{hypot}\left(x, p + p\right)} \cdot x}\right) \cdot 0.5}
\] |
Final simplification99.8%
| Alternative 1 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 20612 |
| Alternative 2 | |
|---|---|
| Accuracy | 67.7% |
| Cost | 7768 |
| Alternative 3 | |
|---|---|
| Accuracy | 67.8% |
| Cost | 7640 |
| Alternative 4 | |
|---|---|
| Accuracy | 68.1% |
| Cost | 7448 |
| Alternative 5 | |
|---|---|
| Accuracy | 68.6% |
| Cost | 7256 |
| Alternative 6 | |
|---|---|
| Accuracy | 62.6% |
| Cost | 6728 |
| Alternative 7 | |
|---|---|
| Accuracy | 16.4% |
| Cost | 256 |
herbie shell --seed 2023152
(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))))))))