| Alternative 1 | |
|---|---|
| Accuracy | 82.9% |
| Cost | 79368 |

(FPCore (x n) :precision binary64 (- (pow (+ x 1.0) (/ 1.0 n)) (pow x (/ 1.0 n))))
(FPCore (x n)
:precision binary64
(if (<= (/ 1.0 n) -1e-30)
(/ (/ (exp (/ (log x) n)) n) x)
(if (<= (/ 1.0 n) 1e-7)
(+
(+
(fma 0.5 (/ (pow (log1p x) 2.0) (* n n)) (/ (- (log1p x) (log x)) n))
(/
(* 0.16666666666666666 (- (pow (log1p x) 3.0) (pow (log x) 3.0)))
(pow n 3.0)))
(* (/ (pow (log x) 2.0) (* n n)) -0.5))
(- (exp (/ (log1p x) n)) (pow x (/ 1.0 n))))))double code(double x, double n) {
return pow((x + 1.0), (1.0 / n)) - pow(x, (1.0 / n));
}
double code(double x, double n) {
double tmp;
if ((1.0 / n) <= -1e-30) {
tmp = (exp((log(x) / n)) / n) / x;
} else if ((1.0 / n) <= 1e-7) {
tmp = (fma(0.5, (pow(log1p(x), 2.0) / (n * n)), ((log1p(x) - log(x)) / n)) + ((0.16666666666666666 * (pow(log1p(x), 3.0) - pow(log(x), 3.0))) / pow(n, 3.0))) + ((pow(log(x), 2.0) / (n * n)) * -0.5);
} else {
tmp = exp((log1p(x) / n)) - pow(x, (1.0 / n));
}
return tmp;
}
function code(x, n) return Float64((Float64(x + 1.0) ^ Float64(1.0 / n)) - (x ^ Float64(1.0 / n))) end
function code(x, n) tmp = 0.0 if (Float64(1.0 / n) <= -1e-30) tmp = Float64(Float64(exp(Float64(log(x) / n)) / n) / x); elseif (Float64(1.0 / n) <= 1e-7) tmp = Float64(Float64(fma(0.5, Float64((log1p(x) ^ 2.0) / Float64(n * n)), Float64(Float64(log1p(x) - log(x)) / n)) + Float64(Float64(0.16666666666666666 * Float64((log1p(x) ^ 3.0) - (log(x) ^ 3.0))) / (n ^ 3.0))) + Float64(Float64((log(x) ^ 2.0) / Float64(n * n)) * -0.5)); else tmp = Float64(exp(Float64(log1p(x) / n)) - (x ^ Float64(1.0 / n))); end return tmp end
code[x_, n_] := N[(N[Power[N[(x + 1.0), $MachinePrecision], N[(1.0 / n), $MachinePrecision]], $MachinePrecision] - N[Power[x, N[(1.0 / n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
code[x_, n_] := If[LessEqual[N[(1.0 / n), $MachinePrecision], -1e-30], N[(N[(N[Exp[N[(N[Log[x], $MachinePrecision] / n), $MachinePrecision]], $MachinePrecision] / n), $MachinePrecision] / x), $MachinePrecision], If[LessEqual[N[(1.0 / n), $MachinePrecision], 1e-7], N[(N[(N[(0.5 * N[(N[Power[N[Log[1 + x], $MachinePrecision], 2.0], $MachinePrecision] / N[(n * n), $MachinePrecision]), $MachinePrecision] + N[(N[(N[Log[1 + x], $MachinePrecision] - N[Log[x], $MachinePrecision]), $MachinePrecision] / n), $MachinePrecision]), $MachinePrecision] + N[(N[(0.16666666666666666 * N[(N[Power[N[Log[1 + x], $MachinePrecision], 3.0], $MachinePrecision] - N[Power[N[Log[x], $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Power[n, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(N[Power[N[Log[x], $MachinePrecision], 2.0], $MachinePrecision] / N[(n * n), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision], N[(N[Exp[N[(N[Log[1 + x], $MachinePrecision] / n), $MachinePrecision]], $MachinePrecision] - N[Power[x, N[(1.0 / n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
{\left(x + 1\right)}^{\left(\frac{1}{n}\right)} - {x}^{\left(\frac{1}{n}\right)}
\begin{array}{l}
\mathbf{if}\;\frac{1}{n} \leq -1 \cdot 10^{-30}:\\
\;\;\;\;\frac{\frac{e^{\frac{\log x}{n}}}{n}}{x}\\
\mathbf{elif}\;\frac{1}{n} \leq 10^{-7}:\\
\;\;\;\;\left(\mathsf{fma}\left(0.5, \frac{{\left(\mathsf{log1p}\left(x\right)\right)}^{2}}{n \cdot n}, \frac{\mathsf{log1p}\left(x\right) - \log x}{n}\right) + \frac{0.16666666666666666 \cdot \left({\left(\mathsf{log1p}\left(x\right)\right)}^{3} - {\log x}^{3}\right)}{{n}^{3}}\right) + \frac{{\log x}^{2}}{n \cdot n} \cdot -0.5\\
\mathbf{else}:\\
\;\;\;\;e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - {x}^{\left(\frac{1}{n}\right)}\\
\end{array}
Herbie found 15 alternatives:
| Alternative | Accuracy | Speedup |
|---|
if (/.f64 1 n) < -1e-30Initial program 87.4%
Taylor expanded in x around inf 98.1%
Simplified98.1%
[Start]98.1% | \[ \frac{e^{-1 \cdot \frac{\log \left(\frac{1}{x}\right)}{n}}}{n \cdot x}
\] |
|---|---|
mul-1-neg [=>]98.1% | \[ \frac{e^{\color{blue}{-\frac{\log \left(\frac{1}{x}\right)}{n}}}}{n \cdot x}
\] |
log-rec [=>]98.1% | \[ \frac{e^{-\frac{\color{blue}{-\log x}}{n}}}{n \cdot x}
\] |
*-commutative [=>]98.1% | \[ \frac{e^{-\frac{-\log x}{n}}}{\color{blue}{x \cdot n}}
\] |
Taylor expanded in x around 0 98.1%
Simplified98.2%
[Start]98.1% | \[ \frac{e^{--1 \cdot \frac{\log x}{n}}}{n \cdot x}
\] |
|---|---|
associate-/r* [=>]98.2% | \[ \color{blue}{\frac{\frac{e^{--1 \cdot \frac{\log x}{n}}}{n}}{x}}
\] |
neg-mul-1 [<=]98.2% | \[ \frac{\frac{e^{-\color{blue}{\left(-\frac{\log x}{n}\right)}}}{n}}{x}
\] |
remove-double-neg [=>]98.2% | \[ \frac{\frac{e^{\color{blue}{\frac{\log x}{n}}}}{n}}{x}
\] |
if -1e-30 < (/.f64 1 n) < 9.9999999999999995e-8Initial program 32.1%
Taylor expanded in n around -inf 86.9%
Simplified86.9%
[Start]86.9% | \[ \left(0.5 \cdot \frac{{\log \left(1 + x\right)}^{2}}{{n}^{2}} + \left(-1 \cdot \frac{-0.16666666666666666 \cdot {\log \left(1 + x\right)}^{3} - -0.16666666666666666 \cdot {\log x}^{3}}{{n}^{3}} + -1 \cdot \frac{-1 \cdot \log \left(1 + x\right) - -1 \cdot \log x}{n}\right)\right) - 0.5 \cdot \frac{{\log x}^{2}}{{n}^{2}}
\] |
|---|---|
sub-neg [=>]86.9% | \[ \color{blue}{\left(0.5 \cdot \frac{{\log \left(1 + x\right)}^{2}}{{n}^{2}} + \left(-1 \cdot \frac{-0.16666666666666666 \cdot {\log \left(1 + x\right)}^{3} - -0.16666666666666666 \cdot {\log x}^{3}}{{n}^{3}} + -1 \cdot \frac{-1 \cdot \log \left(1 + x\right) - -1 \cdot \log x}{n}\right)\right) + \left(-0.5 \cdot \frac{{\log x}^{2}}{{n}^{2}}\right)}
\] |
if 9.9999999999999995e-8 < (/.f64 1 n) Initial program 90.4%
Taylor expanded in n around 0 90.4%
Simplified100.0%
[Start]90.4% | \[ e^{\frac{\log \left(1 + x\right)}{n}} - e^{\frac{\log x}{n}}
\] |
|---|---|
log1p-def [=>]100.0% | \[ e^{\frac{\color{blue}{\mathsf{log1p}\left(x\right)}}{n}} - e^{\frac{\log x}{n}}
\] |
*-rgt-identity [<=]100.0% | \[ e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - e^{\frac{\color{blue}{\log x \cdot 1}}{n}}
\] |
associate-*r/ [<=]100.0% | \[ e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - e^{\color{blue}{\log x \cdot \frac{1}{n}}}
\] |
unpow-1 [<=]100.0% | \[ e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - e^{\log x \cdot \color{blue}{{n}^{-1}}}
\] |
exp-to-pow [=>]100.0% | \[ e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - \color{blue}{{x}^{\left({n}^{-1}\right)}}
\] |
/-rgt-identity [<=]100.0% | \[ e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - {x}^{\color{blue}{\left(\frac{{n}^{-1}}{1}\right)}}
\] |
metadata-eval [<=]100.0% | \[ e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - {x}^{\left(\frac{{n}^{-1}}{\color{blue}{\frac{2}{2}}}\right)}
\] |
associate-/l* [<=]100.0% | \[ e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - {x}^{\color{blue}{\left(\frac{{n}^{-1} \cdot 2}{2}\right)}}
\] |
*-commutative [<=]100.0% | \[ e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - {x}^{\left(\frac{\color{blue}{2 \cdot {n}^{-1}}}{2}\right)}
\] |
*-commutative [=>]100.0% | \[ e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - {x}^{\left(\frac{\color{blue}{{n}^{-1} \cdot 2}}{2}\right)}
\] |
associate-/l* [=>]100.0% | \[ e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - {x}^{\color{blue}{\left(\frac{{n}^{-1}}{\frac{2}{2}}\right)}}
\] |
metadata-eval [=>]100.0% | \[ e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - {x}^{\left(\frac{{n}^{-1}}{\color{blue}{1}}\right)}
\] |
/-rgt-identity [=>]100.0% | \[ e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - {x}^{\color{blue}{\left({n}^{-1}\right)}}
\] |
unpow-1 [=>]100.0% | \[ e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - {x}^{\color{blue}{\left(\frac{1}{n}\right)}}
\] |
Final simplification91.2%
| Alternative 1 | |
|---|---|
| Accuracy | 82.9% |
| Cost | 79368 |
| Alternative 2 | |
|---|---|
| Accuracy | 82.9% |
| Cost | 46664 |
| Alternative 3 | |
|---|---|
| Accuracy | 82.8% |
| Cost | 20232 |
| Alternative 4 | |
|---|---|
| Accuracy | 82.0% |
| Cost | 13640 |
| Alternative 5 | |
|---|---|
| Accuracy | 82.0% |
| Cost | 13640 |
| Alternative 6 | |
|---|---|
| Accuracy | 80.9% |
| Cost | 7628 |
| Alternative 7 | |
|---|---|
| Accuracy | 82.1% |
| Cost | 7560 |
| Alternative 8 | |
|---|---|
| Accuracy | 81.9% |
| Cost | 7368 |
| Alternative 9 | |
|---|---|
| Accuracy | 81.9% |
| Cost | 7368 |
| Alternative 10 | |
|---|---|
| Accuracy | 72.0% |
| Cost | 6852 |
| Alternative 11 | |
|---|---|
| Accuracy | 71.8% |
| Cost | 6788 |
| Alternative 12 | |
|---|---|
| Accuracy | 54.7% |
| Cost | 580 |
| Alternative 13 | |
|---|---|
| Accuracy | 36.7% |
| Cost | 320 |
| Alternative 14 | |
|---|---|
| Accuracy | 37.2% |
| Cost | 320 |
| Alternative 15 | |
|---|---|
| Accuracy | 4.7% |
| Cost | 192 |
herbie shell --seed 2023263
(FPCore (x n)
:name "2nthrt (problem 3.4.6)"
:precision binary64
(- (pow (+ x 1.0) (/ 1.0 n)) (pow x (/ 1.0 n))))