| Alternative 1 | |
|---|---|
| Error | 3.19% |
| Cost | 7304 |
(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)
(- (pow (+ 1.0 x) (/ 1.0 n)) t_0)
(if (<= (/ 1.0 n) 0.0002) (/ (log1p (/ 1.0 x)) n) (- 1.0 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 = pow((1.0 + x), (1.0 / n)) - t_0;
} else if ((1.0 / n) <= 0.0002) {
tmp = log1p((1.0 / x)) / n;
} else {
tmp = 1.0 - 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 = Math.pow((1.0 + x), (1.0 / n)) - t_0;
} else if ((1.0 / n) <= 0.0002) {
tmp = Math.log1p((1.0 / x)) / n;
} else {
tmp = 1.0 - 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 = math.pow((1.0 + x), (1.0 / n)) - t_0 elif (1.0 / n) <= 0.0002: tmp = math.log1p((1.0 / x)) / n else: tmp = 1.0 - 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 + x) ^ Float64(1.0 / n)) - t_0); elseif (Float64(1.0 / n) <= 0.0002) tmp = Float64(log1p(Float64(1.0 / x)) / n); else tmp = Float64(1.0 - 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[Power[N[(1.0 + x), $MachinePrecision], N[(1.0 / n), $MachinePrecision]], $MachinePrecision] - t$95$0), $MachinePrecision], If[LessEqual[N[(1.0 / n), $MachinePrecision], 0.0002], N[(N[Log[1 + N[(1.0 / x), $MachinePrecision]], $MachinePrecision] / n), $MachinePrecision], N[(1.0 - 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}:\\
\;\;\;\;{\left(1 + x\right)}^{\left(\frac{1}{n}\right)} - t_0\\
\mathbf{elif}\;\frac{1}{n} \leq 0.0002:\\
\;\;\;\;\frac{\mathsf{log1p}\left(\frac{1}{x}\right)}{n}\\
\mathbf{else}:\\
\;\;\;\;1 - t_0\\
\end{array}
Results
if (/.f64 1 n) < -4.00000000000000025e-9Initial program 2.4
if -4.00000000000000025e-9 < (/.f64 1 n) < 2.0000000000000001e-4Initial program 69.45
Taylor expanded in n around inf 23.85
Simplified23.84
[Start]23.85 | \[ \frac{\log \left(1 + x\right) - \log x}{n}
\] |
|---|---|
log1p-def [=>]23.84 | \[ \frac{\color{blue}{\mathsf{log1p}\left(x\right)} - \log x}{n}
\] |
Applied egg-rr23.64
Applied egg-rr23.64
Simplified1.62
[Start]23.64 | \[ \frac{\mathsf{log1p}\left(\frac{x + 1}{x} - 1\right)}{n}
\] |
|---|---|
*-lft-identity [<=]23.64 | \[ \frac{\mathsf{log1p}\left(\frac{\color{blue}{1 \cdot \left(x + 1\right)}}{x} - 1\right)}{n}
\] |
associate-*l/ [<=]27.5 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\frac{1}{x} \cdot \left(x + 1\right)} - 1\right)}{n}
\] |
distribute-rgt-in [=>]27.5 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\left(x \cdot \frac{1}{x} + 1 \cdot \frac{1}{x}\right)} - 1\right)}{n}
\] |
+-commutative [=>]27.5 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\left(1 \cdot \frac{1}{x} + x \cdot \frac{1}{x}\right)} - 1\right)}{n}
\] |
*-lft-identity [=>]27.5 | \[ \frac{\mathsf{log1p}\left(\left(\color{blue}{\frac{1}{x}} + x \cdot \frac{1}{x}\right) - 1\right)}{n}
\] |
rgt-mult-inverse [=>]23.64 | \[ \frac{\mathsf{log1p}\left(\left(\frac{1}{x} + \color{blue}{1}\right) - 1\right)}{n}
\] |
associate--l+ [=>]1.62 | \[ \frac{\mathsf{log1p}\left(\color{blue}{\frac{1}{x} + \left(1 - 1\right)}\right)}{n}
\] |
metadata-eval [=>]1.62 | \[ \frac{\mathsf{log1p}\left(\frac{1}{x} + \color{blue}{0}\right)}{n}
\] |
Taylor expanded in n around 0 23.64
Simplified1.62
[Start]23.64 | \[ \frac{\log \left(1 + \frac{1}{x}\right)}{n}
\] |
|---|---|
log1p-def [=>]1.62 | \[ \frac{\color{blue}{\mathsf{log1p}\left(\frac{1}{x}\right)}}{n}
\] |
if 2.0000000000000001e-4 < (/.f64 1 n) Initial program 9.8
Taylor expanded in x around 0 10.08
Taylor expanded in x around inf 10.08
Simplified10.08
[Start]10.08 | \[ 1 - e^{-1 \cdot \frac{\log \left(\frac{1}{x}\right)}{n}}
\] |
|---|---|
log-rec [=>]10.08 | \[ 1 - e^{-1 \cdot \frac{\color{blue}{-\log x}}{n}}
\] |
distribute-frac-neg [=>]10.08 | \[ 1 - e^{-1 \cdot \color{blue}{\left(-\frac{\log x}{n}\right)}}
\] |
mul-1-neg [<=]10.08 | \[ 1 - e^{-1 \cdot \color{blue}{\left(-1 \cdot \frac{\log x}{n}\right)}}
\] |
associate-*r* [=>]10.08 | \[ 1 - e^{\color{blue}{\left(-1 \cdot -1\right) \cdot \frac{\log x}{n}}}
\] |
metadata-eval [=>]10.08 | \[ 1 - e^{\color{blue}{1} \cdot \frac{\log x}{n}}
\] |
*-commutative [=>]10.08 | \[ 1 - e^{\color{blue}{\frac{\log x}{n} \cdot 1}}
\] |
associate-*l/ [=>]10.08 | \[ 1 - e^{\color{blue}{\frac{\log x \cdot 1}{n}}}
\] |
associate-*r/ [<=]10.08 | \[ 1 - e^{\color{blue}{\log x \cdot \frac{1}{n}}}
\] |
exp-to-pow [=>]10.08 | \[ 1 - \color{blue}{{x}^{\left(\frac{1}{n}\right)}}
\] |
Final simplification2.59
| Alternative 1 | |
|---|---|
| Error | 3.19% |
| Cost | 7304 |
| Alternative 2 | |
|---|---|
| Error | 11.08% |
| Cost | 6985 |
| Alternative 3 | |
|---|---|
| Error | 24.69% |
| Cost | 6788 |
| Alternative 4 | |
|---|---|
| Error | 44.83% |
| Cost | 585 |
| Alternative 5 | |
|---|---|
| Error | 44.14% |
| Cost | 585 |
| Alternative 6 | |
|---|---|
| Error | 60.3% |
| Cost | 64 |
herbie shell --seed 2023103
(FPCore (x n)
:name "2nthrt (problem 3.4.6)"
:precision binary64
(- (pow (+ x 1.0) (/ 1.0 n)) (pow x (/ 1.0 n))))