| Alternative 1 | |
|---|---|
| Error | 1.3 |
| Cost | 13832 |
(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) -4e-12)
(/ (exp (/ (log x) n)) (* n x))
(if (<= (/ 1.0 n) 1e-10)
(/ (log1p (/ 1.0 x)) n)
(- (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) <= -4e-12) {
tmp = exp((log(x) / n)) / (n * x);
} else if ((1.0 / n) <= 1e-10) {
tmp = log1p((1.0 / x)) / n;
} else {
tmp = exp((log1p(x) / n)) - pow(x, (1.0 / n));
}
return tmp;
}
public static double code(double x, double n) {
return Math.pow((x + 1.0), (1.0 / n)) - Math.pow(x, (1.0 / n));
}
public static double code(double x, double n) {
double tmp;
if ((1.0 / n) <= -4e-12) {
tmp = Math.exp((Math.log(x) / n)) / (n * x);
} else if ((1.0 / n) <= 1e-10) {
tmp = Math.log1p((1.0 / x)) / n;
} else {
tmp = Math.exp((Math.log1p(x) / n)) - Math.pow(x, (1.0 / n));
}
return tmp;
}
def code(x, n): return math.pow((x + 1.0), (1.0 / n)) - math.pow(x, (1.0 / n))
def code(x, n): tmp = 0 if (1.0 / n) <= -4e-12: tmp = math.exp((math.log(x) / n)) / (n * x) elif (1.0 / n) <= 1e-10: tmp = math.log1p((1.0 / x)) / n else: tmp = math.exp((math.log1p(x) / n)) - math.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) <= -4e-12) tmp = Float64(exp(Float64(log(x) / n)) / Float64(n * x)); elseif (Float64(1.0 / n) <= 1e-10) tmp = Float64(log1p(Float64(1.0 / x)) / n); 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], -4e-12], N[(N[Exp[N[(N[Log[x], $MachinePrecision] / n), $MachinePrecision]], $MachinePrecision] / N[(n * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(1.0 / n), $MachinePrecision], 1e-10], N[(N[Log[1 + N[(1.0 / x), $MachinePrecision]], $MachinePrecision] / n), $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 -4 \cdot 10^{-12}:\\
\;\;\;\;\frac{e^{\frac{\log x}{n}}}{n \cdot x}\\
\mathbf{elif}\;\frac{1}{n} \leq 10^{-10}:\\
\;\;\;\;\frac{\mathsf{log1p}\left(\frac{1}{x}\right)}{n}\\
\mathbf{else}:\\
\;\;\;\;e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - {x}^{\left(\frac{1}{n}\right)}\\
\end{array}
Results
if (/.f64 1 n) < -3.99999999999999992e-12Initial program 2.8
Taylor expanded in x around inf 2.4
Simplified2.4
[Start]2.4 | \[ \frac{e^{-1 \cdot \frac{\log \left(\frac{1}{x}\right)}{n}}}{n \cdot x}
\] |
|---|---|
mul-1-neg [=>]2.4 | \[ \frac{e^{\color{blue}{-\frac{\log \left(\frac{1}{x}\right)}{n}}}}{n \cdot x}
\] |
log-rec [=>]2.4 | \[ \frac{e^{-\frac{\color{blue}{-\log x}}{n}}}{n \cdot x}
\] |
mul-1-neg [<=]2.4 | \[ \frac{e^{-\frac{\color{blue}{-1 \cdot \log x}}{n}}}{n \cdot x}
\] |
distribute-neg-frac [=>]2.4 | \[ \frac{e^{\color{blue}{\frac{--1 \cdot \log x}{n}}}}{n \cdot x}
\] |
mul-1-neg [=>]2.4 | \[ \frac{e^{\frac{-\color{blue}{\left(-\log x\right)}}{n}}}{n \cdot x}
\] |
remove-double-neg [=>]2.4 | \[ \frac{e^{\frac{\color{blue}{\log x}}{n}}}{n \cdot x}
\] |
*-commutative [=>]2.4 | \[ \frac{e^{\frac{\log x}{n}}}{\color{blue}{x \cdot n}}
\] |
if -3.99999999999999992e-12 < (/.f64 1 n) < 1.00000000000000004e-10Initial program 44.8
Taylor expanded in n around inf 14.7
Simplified14.7
[Start]14.7 | \[ \frac{\log \left(1 + x\right) - \log x}{n}
\] |
|---|---|
log1p-def [=>]14.7 | \[ \frac{\color{blue}{\mathsf{log1p}\left(x\right)} - \log x}{n}
\] |
Applied egg-rr14.6
Applied egg-rr14.6
Simplified0.5
[Start]14.6 | \[ \frac{\mathsf{log1p}\left(\frac{x + 1}{x} - 1\right)}{n}
\] |
|---|---|
*-lft-identity [<=]14.6 | \[ \frac{\mathsf{log1p}\left(\frac{\color{blue}{1 \cdot \left(x + 1\right)}}{x} - 1\right)}{n}
\] |
associate-*l/ [<=]16.9 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\frac{1}{x} \cdot \left(x + 1\right)} - 1\right)}{n}
\] |
distribute-rgt-in [=>]16.9 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\left(x \cdot \frac{1}{x} + 1 \cdot \frac{1}{x}\right)} - 1\right)}{n}
\] |
+-commutative [=>]16.9 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\left(1 \cdot \frac{1}{x} + x \cdot \frac{1}{x}\right)} - 1\right)}{n}
\] |
*-lft-identity [=>]16.9 | \[ \frac{\mathsf{log1p}\left(\left(\color{blue}{\frac{1}{x}} + x \cdot \frac{1}{x}\right) - 1\right)}{n}
\] |
rgt-mult-inverse [=>]14.6 | \[ \frac{\mathsf{log1p}\left(\left(\frac{1}{x} + \color{blue}{1}\right) - 1\right)}{n}
\] |
associate--l+ [=>]0.5 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\frac{1}{x} + \left(1 - 1\right)}\right)}{n}
\] |
metadata-eval [=>]0.5 | \[ \frac{\mathsf{log1p}\left(\frac{1}{x} + \color{blue}{0}\right)}{n}
\] |
Taylor expanded in n around 0 14.6
Simplified0.5
[Start]14.6 | \[ \frac{\log \left(1 + \frac{1}{x}\right)}{n}
\] |
|---|---|
log1p-def [=>]0.5 | \[ \frac{\color{blue}{\mathsf{log1p}\left(\frac{1}{x}\right)}}{n}
\] |
if 1.00000000000000004e-10 < (/.f64 1 n) Initial program 6.7
Taylor expanded in n around 0 6.8
Simplified4.1
[Start]6.8 | \[ e^{\frac{\log \left(1 + x\right)}{n}} - {x}^{\left(\frac{1}{n}\right)}
\] |
|---|---|
log1p-def [=>]4.1 | \[ e^{\frac{\color{blue}{\mathsf{log1p}\left(x\right)}}{n}} - {x}^{\left(\frac{1}{n}\right)}
\] |
Final simplification1.3
| Alternative 1 | |
|---|---|
| Error | 1.3 |
| Cost | 13832 |
| Alternative 2 | |
|---|---|
| Error | 1.5 |
| Cost | 13508 |
| Alternative 3 | |
|---|---|
| Error | 1.6 |
| Cost | 7436 |
| Alternative 4 | |
|---|---|
| Error | 1.8 |
| Cost | 7304 |
| Alternative 5 | |
|---|---|
| Error | 7.1 |
| Cost | 6980 |
| Alternative 6 | |
|---|---|
| Error | 16.3 |
| Cost | 6788 |
| Alternative 7 | |
|---|---|
| Error | 35.2 |
| Cost | 841 |
| Alternative 8 | |
|---|---|
| Error | 28.6 |
| Cost | 585 |
| Alternative 9 | |
|---|---|
| Error | 40.4 |
| Cost | 320 |
| Alternative 10 | |
|---|---|
| Error | 39.9 |
| Cost | 320 |
| Alternative 11 | |
|---|---|
| Error | 39.9 |
| Cost | 320 |
herbie shell --seed 2023018
(FPCore (x n)
:name "2nthrt (problem 3.4.6)"
:precision binary64
(- (pow (+ x 1.0) (/ 1.0 n)) (pow x (/ 1.0 n))))