| Alternative 1 | |
|---|---|
| Accuracy | 69.9% |
| Cost | 7436 |

(FPCore (x n) :precision binary64 (- (pow (+ x 1.0) (/ 1.0 n)) (pow x (/ 1.0 n))))
(FPCore (x n) :precision binary64 (if (<= x 0.6) (- (expm1 (/ (log x) n))) (/ (* (/ 1.0 n) (pow x (/ 1.0 n))) x)))
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 (x <= 0.6) {
tmp = -expm1((log(x) / n));
} else {
tmp = ((1.0 / n) * pow(x, (1.0 / n))) / x;
}
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 (x <= 0.6) {
tmp = -Math.expm1((Math.log(x) / n));
} else {
tmp = ((1.0 / n) * Math.pow(x, (1.0 / n))) / x;
}
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 x <= 0.6: tmp = -math.expm1((math.log(x) / n)) else: tmp = ((1.0 / n) * math.pow(x, (1.0 / n))) / x 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 (x <= 0.6) tmp = Float64(-expm1(Float64(log(x) / n))); else tmp = Float64(Float64(Float64(1.0 / n) * (x ^ Float64(1.0 / n))) / x); 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[x, 0.6], (-N[(Exp[N[(N[Log[x], $MachinePrecision] / n), $MachinePrecision]] - 1), $MachinePrecision]), N[(N[(N[(1.0 / n), $MachinePrecision] * N[Power[x, N[(1.0 / n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
{\left(x + 1\right)}^{\left(\frac{1}{n}\right)} - {x}^{\left(\frac{1}{n}\right)}
\begin{array}{l}
\mathbf{if}\;x \leq 0.6:\\
\;\;\;\;-\mathsf{expm1}\left(\frac{\log x}{n}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{n} \cdot {x}^{\left(\frac{1}{n}\right)}}{x}\\
\end{array}
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
if x < 0.599999999999999978Initial program 45.9%
Taylor expanded in x around 0 45.2%
Simplified45.2%
[Start]45.2 | \[ 1 - e^{\frac{\log x}{n}}
\] |
|---|---|
*-rgt-identity [<=]45.2 | \[ 1 - e^{\frac{\color{blue}{\log x \cdot 1}}{n}}
\] |
associate-*r/ [<=]45.2 | \[ 1 - e^{\color{blue}{\log x \cdot \frac{1}{n}}}
\] |
unpow-1 [<=]45.2 | \[ 1 - e^{\log x \cdot \color{blue}{{n}^{-1}}}
\] |
exp-to-pow [=>]45.2 | \[ 1 - \color{blue}{{x}^{\left({n}^{-1}\right)}}
\] |
unpow-1 [=>]45.2 | \[ 1 - {x}^{\color{blue}{\left(\frac{1}{n}\right)}}
\] |
Taylor expanded in x around 0 45.2%
Simplified89.7%
[Start]45.2 | \[ 1 - e^{\frac{\log x}{n}}
\] |
|---|---|
sub-neg [=>]45.2 | \[ \color{blue}{1 + \left(-e^{\frac{\log x}{n}}\right)}
\] |
+-commutative [=>]45.2 | \[ \color{blue}{\left(-e^{\frac{\log x}{n}}\right) + 1}
\] |
neg-sub0 [=>]45.2 | \[ \color{blue}{\left(0 - e^{\frac{\log x}{n}}\right)} + 1
\] |
metadata-eval [<=]45.2 | \[ \left(\color{blue}{\log 1} - e^{\frac{\log x}{n}}\right) + 1
\] |
associate-+l- [=>]45.2 | \[ \color{blue}{\log 1 - \left(e^{\frac{\log x}{n}} - 1\right)}
\] |
metadata-eval [=>]45.2 | \[ \color{blue}{0} - \left(e^{\frac{\log x}{n}} - 1\right)
\] |
sub0-neg [=>]45.2 | \[ \color{blue}{-\left(e^{\frac{\log x}{n}} - 1\right)}
\] |
expm1-def [=>]89.7 | \[ -\color{blue}{\mathsf{expm1}\left(\frac{\log x}{n}\right)}
\] |
if 0.599999999999999978 < x Initial program 59.8%
Taylor expanded in x around inf 97.3%
Simplified97.3%
[Start]97.3 | \[ \frac{e^{-1 \cdot \frac{\log \left(\frac{1}{x}\right)}{n}}}{n \cdot x}
\] |
|---|---|
log-rec [=>]97.3 | \[ \frac{e^{-1 \cdot \frac{\color{blue}{-\log x}}{n}}}{n \cdot x}
\] |
mul-1-neg [<=]97.3 | \[ \frac{e^{-1 \cdot \frac{\color{blue}{-1 \cdot \log x}}{n}}}{n \cdot x}
\] |
mul-1-neg [=>]97.3 | \[ \frac{e^{-1 \cdot \frac{\color{blue}{-\log x}}{n}}}{n \cdot x}
\] |
distribute-frac-neg [=>]97.3 | \[ \frac{e^{-1 \cdot \color{blue}{\left(-\frac{\log x}{n}\right)}}}{n \cdot x}
\] |
neg-mul-1 [<=]97.3 | \[ \frac{e^{\color{blue}{-\left(-\frac{\log x}{n}\right)}}}{n \cdot x}
\] |
remove-double-neg [=>]97.3 | \[ \frac{e^{\color{blue}{\frac{\log x}{n}}}}{n \cdot x}
\] |
*-rgt-identity [<=]97.3 | \[ \frac{e^{\frac{\color{blue}{\log x \cdot 1}}{n}}}{n \cdot x}
\] |
associate-*r/ [<=]97.3 | \[ \frac{e^{\color{blue}{\log x \cdot \frac{1}{n}}}}{n \cdot x}
\] |
unpow-1 [<=]97.3 | \[ \frac{e^{\log x \cdot \color{blue}{{n}^{-1}}}}{n \cdot x}
\] |
exp-to-pow [=>]97.3 | \[ \frac{\color{blue}{{x}^{\left({n}^{-1}\right)}}}{n \cdot x}
\] |
unpow-1 [=>]97.3 | \[ \frac{{x}^{\color{blue}{\left(\frac{1}{n}\right)}}}{n \cdot x}
\] |
*-commutative [=>]97.3 | \[ \frac{{x}^{\left(\frac{1}{n}\right)}}{\color{blue}{x \cdot n}}
\] |
Applied egg-rr99.2%
[Start]97.3 | \[ \frac{{x}^{\left(\frac{1}{n}\right)}}{x \cdot n}
\] |
|---|---|
*-un-lft-identity [=>]97.3 | \[ \frac{\color{blue}{1 \cdot {x}^{\left(\frac{1}{n}\right)}}}{x \cdot n}
\] |
times-frac [=>]99.2 | \[ \color{blue}{\frac{1}{x} \cdot \frac{{x}^{\left(\frac{1}{n}\right)}}{n}}
\] |
Applied egg-rr99.3%
[Start]99.2 | \[ \frac{1}{x} \cdot \frac{{x}^{\left(\frac{1}{n}\right)}}{n}
\] |
|---|---|
associate-*l/ [=>]99.3 | \[ \color{blue}{\frac{1 \cdot \frac{{x}^{\left(\frac{1}{n}\right)}}{n}}{x}}
\] |
*-un-lft-identity [<=]99.3 | \[ \frac{\color{blue}{\frac{{x}^{\left(\frac{1}{n}\right)}}{n}}}{x}
\] |
Applied egg-rr99.3%
[Start]99.3 | \[ \frac{\frac{{x}^{\left(\frac{1}{n}\right)}}{n}}{x}
\] |
|---|---|
div-inv [=>]99.3 | \[ \frac{\color{blue}{{x}^{\left(\frac{1}{n}\right)} \cdot \frac{1}{n}}}{x}
\] |
Final simplification93.7%
| Alternative 1 | |
|---|---|
| Accuracy | 69.9% |
| Cost | 7436 |
| Alternative 2 | |
|---|---|
| Accuracy | 69.9% |
| Cost | 7308 |
| Alternative 3 | |
|---|---|
| Accuracy | 69.9% |
| Cost | 7308 |
| Alternative 4 | |
|---|---|
| Accuracy | 56.8% |
| Cost | 7116 |
| Alternative 5 | |
|---|---|
| Accuracy | 58.1% |
| Cost | 6852 |
| Alternative 6 | |
|---|---|
| Accuracy | 57.9% |
| Cost | 6788 |
| Alternative 7 | |
|---|---|
| Accuracy | 45.3% |
| Cost | 585 |
| Alternative 8 | |
|---|---|
| Accuracy | 45.9% |
| Cost | 585 |
| Alternative 9 | |
|---|---|
| Accuracy | 30.4% |
| Cost | 64 |
herbie shell --seed 2023161
(FPCore (x n)
:name "2nthrt (problem 3.4.6)"
:precision binary64
(- (pow (+ x 1.0) (/ 1.0 n)) (pow x (/ 1.0 n))))