| Alternative 1 | |
|---|---|
| Error | 2.19% |
| Cost | 7560 |
(FPCore (x n) :precision binary64 (- (pow (+ x 1.0) (/ 1.0 n)) (pow x (/ 1.0 n))))
(FPCore (x n)
:precision binary64
(let* ((t_0 (pow x (/ 1.0 n))))
(if (<= (/ 1.0 n) -4e-9)
(* (/ 1.0 n) (/ t_0 x))
(if (<= (/ 1.0 n) 4e-12)
(/ (log1p (/ 1.0 x)) n)
(- (exp (/ (log1p x) n)) t_0)))))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 t_0 = pow(x, (1.0 / n));
double tmp;
if ((1.0 / n) <= -4e-9) {
tmp = (1.0 / n) * (t_0 / x);
} else if ((1.0 / n) <= 4e-12) {
tmp = log1p((1.0 / x)) / n;
} else {
tmp = exp((log1p(x) / n)) - t_0;
}
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 t_0 = Math.pow(x, (1.0 / n));
double tmp;
if ((1.0 / n) <= -4e-9) {
tmp = (1.0 / n) * (t_0 / x);
} else if ((1.0 / n) <= 4e-12) {
tmp = Math.log1p((1.0 / x)) / n;
} else {
tmp = Math.exp((Math.log1p(x) / n)) - t_0;
}
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): t_0 = math.pow(x, (1.0 / n)) tmp = 0 if (1.0 / n) <= -4e-9: tmp = (1.0 / n) * (t_0 / x) elif (1.0 / n) <= 4e-12: tmp = math.log1p((1.0 / x)) / n else: tmp = math.exp((math.log1p(x) / n)) - t_0 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) t_0 = x ^ Float64(1.0 / n) tmp = 0.0 if (Float64(1.0 / n) <= -4e-9) tmp = Float64(Float64(1.0 / n) * Float64(t_0 / x)); elseif (Float64(1.0 / n) <= 4e-12) tmp = Float64(log1p(Float64(1.0 / x)) / n); else tmp = Float64(exp(Float64(log1p(x) / n)) - t_0); 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_] := Block[{t$95$0 = N[Power[x, N[(1.0 / n), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(1.0 / n), $MachinePrecision], -4e-9], N[(N[(1.0 / n), $MachinePrecision] * N[(t$95$0 / x), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(1.0 / n), $MachinePrecision], 4e-12], N[(N[Log[1 + N[(1.0 / x), $MachinePrecision]], $MachinePrecision] / n), $MachinePrecision], N[(N[Exp[N[(N[Log[1 + x], $MachinePrecision] / n), $MachinePrecision]], $MachinePrecision] - t$95$0), $MachinePrecision]]]]
{\left(x + 1\right)}^{\left(\frac{1}{n}\right)} - {x}^{\left(\frac{1}{n}\right)}
\begin{array}{l}
t_0 := {x}^{\left(\frac{1}{n}\right)}\\
\mathbf{if}\;\frac{1}{n} \leq -4 \cdot 10^{-9}:\\
\;\;\;\;\frac{1}{n} \cdot \frac{t_0}{x}\\
\mathbf{elif}\;\frac{1}{n} \leq 4 \cdot 10^{-12}:\\
\;\;\;\;\frac{\mathsf{log1p}\left(\frac{1}{x}\right)}{n}\\
\mathbf{else}:\\
\;\;\;\;e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - t_0\\
\end{array}
Results
if (/.f64 1 n) < -4.00000000000000025e-9Initial program 2.66
Taylor expanded in x around inf 2.76
Simplified2.76
[Start]2.76 | \[ \frac{e^{-1 \cdot \frac{\log \left(\frac{1}{x}\right)}{n}}}{n \cdot x}
\] |
|---|---|
mul-1-neg [=>]2.76 | \[ \frac{e^{\color{blue}{-\frac{\log \left(\frac{1}{x}\right)}{n}}}}{n \cdot x}
\] |
log-rec [=>]2.76 | \[ \frac{e^{-\frac{\color{blue}{-\log x}}{n}}}{n \cdot x}
\] |
mul-1-neg [<=]2.76 | \[ \frac{e^{-\frac{\color{blue}{-1 \cdot \log x}}{n}}}{n \cdot x}
\] |
distribute-neg-frac [=>]2.76 | \[ \frac{e^{\color{blue}{\frac{--1 \cdot \log x}{n}}}}{n \cdot x}
\] |
mul-1-neg [=>]2.76 | \[ \frac{e^{\frac{-\color{blue}{\left(-\log x\right)}}{n}}}{n \cdot x}
\] |
remove-double-neg [=>]2.76 | \[ \frac{e^{\frac{\color{blue}{\log x}}{n}}}{n \cdot x}
\] |
*-commutative [=>]2.76 | \[ \frac{e^{\frac{\log x}{n}}}{\color{blue}{x \cdot n}}
\] |
Applied egg-rr2.76
if -4.00000000000000025e-9 < (/.f64 1 n) < 3.99999999999999992e-12Initial program 70.31
Taylor expanded in n around inf 22.87
Applied egg-rr22.72
Applied egg-rr22.71
Simplified0.83
[Start]22.71 | \[ \frac{\mathsf{log1p}\left(\frac{1 + x}{x} - 1\right)}{n}
\] |
|---|---|
*-lft-identity [<=]22.71 | \[ \frac{\mathsf{log1p}\left(\color{blue}{1 \cdot \frac{1 + x}{x}} - 1\right)}{n}
\] |
associate-*r/ [=>]22.71 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\frac{1 \cdot \left(1 + x\right)}{x}} - 1\right)}{n}
\] |
associate-*l/ [<=]26.19 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\frac{1}{x} \cdot \left(1 + x\right)} - 1\right)}{n}
\] |
distribute-rgt-in [=>]26.19 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\left(1 \cdot \frac{1}{x} + x \cdot \frac{1}{x}\right)} - 1\right)}{n}
\] |
*-lft-identity [=>]26.19 | \[ \frac{\mathsf{log1p}\left(\left(\color{blue}{\frac{1}{x}} + x \cdot \frac{1}{x}\right) - 1\right)}{n}
\] |
rgt-mult-inverse [=>]22.72 | \[ \frac{\mathsf{log1p}\left(\left(\frac{1}{x} + \color{blue}{1}\right) - 1\right)}{n}
\] |
+-commutative [<=]22.72 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\left(1 + \frac{1}{x}\right)} - 1\right)}{n}
\] |
+-commutative [=>]22.72 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\left(\frac{1}{x} + 1\right)} - 1\right)}{n}
\] |
associate--l+ [=>]0.83 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\frac{1}{x} + \left(1 - 1\right)}\right)}{n}
\] |
metadata-eval [=>]0.83 | \[ \frac{\mathsf{log1p}\left(\frac{1}{x} + \color{blue}{0}\right)}{n}
\] |
Taylor expanded in n around 0 22.72
Simplified0.83
[Start]22.72 | \[ \frac{\log \left(1 + \frac{1}{x}\right)}{n}
\] |
|---|---|
log1p-def [=>]0.83 | \[ \frac{\color{blue}{\mathsf{log1p}\left(\frac{1}{x}\right)}}{n}
\] |
if 3.99999999999999992e-12 < (/.f64 1 n) Initial program 11.64
Taylor expanded in n around 0 11.67
Simplified6.72
[Start]11.67 | \[ e^{\frac{\log \left(1 + x\right)}{n}} - {x}^{\left(\frac{1}{n}\right)}
\] |
|---|---|
log1p-def [=>]6.72 | \[ e^{\frac{\color{blue}{\mathsf{log1p}\left(x\right)}}{n}} - {x}^{\left(\frac{1}{n}\right)}
\] |
Final simplification1.82
| Alternative 1 | |
|---|---|
| Error | 2.19% |
| Cost | 7560 |
| Alternative 2 | |
|---|---|
| Error | 2.94% |
| Cost | 7304 |
| Alternative 3 | |
|---|---|
| Error | 2.39% |
| Cost | 7304 |
| Alternative 4 | |
|---|---|
| Error | 2.39% |
| Cost | 7304 |
| Alternative 5 | |
|---|---|
| Error | 11.24% |
| Cost | 6980 |
| Alternative 6 | |
|---|---|
| Error | 24.39% |
| Cost | 6788 |
| Alternative 7 | |
|---|---|
| Error | 54.51% |
| Cost | 841 |
| Alternative 8 | |
|---|---|
| Error | 45.14% |
| Cost | 585 |
| Alternative 9 | |
|---|---|
| Error | 63.27% |
| Cost | 320 |
| Alternative 10 | |
|---|---|
| Error | 62.49% |
| Cost | 320 |
herbie shell --seed 2023125
(FPCore (x n)
:name "2nthrt (problem 3.4.6)"
:precision binary64
(- (pow (+ x 1.0) (/ 1.0 n)) (pow x (/ 1.0 n))))