| Alternative 1 | |
|---|---|
| Error | 1.95% |
| Cost | 21768 |
(FPCore (i n) :precision binary64 (* 100.0 (/ (- (pow (+ 1.0 (/ i n)) n) 1.0) (/ i n))))
(FPCore (i n)
:precision binary64
(let* ((t_0 (pow (+ 1.0 (/ i n)) n)) (t_1 (/ (+ t_0 -1.0) (/ i n))))
(if (<= t_1 5e-310)
(/ (* (expm1 (* n (log1p (/ i n)))) 100.0) (/ i n))
(if (<= t_1 1000000000.0)
(* n (/ (+ (* t_0 100.0) -100.0) i))
(* 100.0 (/ n (+ 1.0 (* i (+ -0.5 (* i 0.08333333333333333))))))))))double code(double i, double n) {
return 100.0 * ((pow((1.0 + (i / n)), n) - 1.0) / (i / n));
}
double code(double i, double n) {
double t_0 = pow((1.0 + (i / n)), n);
double t_1 = (t_0 + -1.0) / (i / n);
double tmp;
if (t_1 <= 5e-310) {
tmp = (expm1((n * log1p((i / n)))) * 100.0) / (i / n);
} else if (t_1 <= 1000000000.0) {
tmp = n * (((t_0 * 100.0) + -100.0) / i);
} else {
tmp = 100.0 * (n / (1.0 + (i * (-0.5 + (i * 0.08333333333333333)))));
}
return tmp;
}
public static double code(double i, double n) {
return 100.0 * ((Math.pow((1.0 + (i / n)), n) - 1.0) / (i / n));
}
public static double code(double i, double n) {
double t_0 = Math.pow((1.0 + (i / n)), n);
double t_1 = (t_0 + -1.0) / (i / n);
double tmp;
if (t_1 <= 5e-310) {
tmp = (Math.expm1((n * Math.log1p((i / n)))) * 100.0) / (i / n);
} else if (t_1 <= 1000000000.0) {
tmp = n * (((t_0 * 100.0) + -100.0) / i);
} else {
tmp = 100.0 * (n / (1.0 + (i * (-0.5 + (i * 0.08333333333333333)))));
}
return tmp;
}
def code(i, n): return 100.0 * ((math.pow((1.0 + (i / n)), n) - 1.0) / (i / n))
def code(i, n): t_0 = math.pow((1.0 + (i / n)), n) t_1 = (t_0 + -1.0) / (i / n) tmp = 0 if t_1 <= 5e-310: tmp = (math.expm1((n * math.log1p((i / n)))) * 100.0) / (i / n) elif t_1 <= 1000000000.0: tmp = n * (((t_0 * 100.0) + -100.0) / i) else: tmp = 100.0 * (n / (1.0 + (i * (-0.5 + (i * 0.08333333333333333))))) return tmp
function code(i, n) return Float64(100.0 * Float64(Float64((Float64(1.0 + Float64(i / n)) ^ n) - 1.0) / Float64(i / n))) end
function code(i, n) t_0 = Float64(1.0 + Float64(i / n)) ^ n t_1 = Float64(Float64(t_0 + -1.0) / Float64(i / n)) tmp = 0.0 if (t_1 <= 5e-310) tmp = Float64(Float64(expm1(Float64(n * log1p(Float64(i / n)))) * 100.0) / Float64(i / n)); elseif (t_1 <= 1000000000.0) tmp = Float64(n * Float64(Float64(Float64(t_0 * 100.0) + -100.0) / i)); else tmp = Float64(100.0 * Float64(n / Float64(1.0 + Float64(i * Float64(-0.5 + Float64(i * 0.08333333333333333)))))); end return tmp end
code[i_, n_] := N[(100.0 * N[(N[(N[Power[N[(1.0 + N[(i / n), $MachinePrecision]), $MachinePrecision], n], $MachinePrecision] - 1.0), $MachinePrecision] / N[(i / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[i_, n_] := Block[{t$95$0 = N[Power[N[(1.0 + N[(i / n), $MachinePrecision]), $MachinePrecision], n], $MachinePrecision]}, Block[{t$95$1 = N[(N[(t$95$0 + -1.0), $MachinePrecision] / N[(i / n), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, 5e-310], N[(N[(N[(Exp[N[(n * N[Log[1 + N[(i / n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision] * 100.0), $MachinePrecision] / N[(i / n), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 1000000000.0], N[(n * N[(N[(N[(t$95$0 * 100.0), $MachinePrecision] + -100.0), $MachinePrecision] / i), $MachinePrecision]), $MachinePrecision], N[(100.0 * N[(n / N[(1.0 + N[(i * N[(-0.5 + N[(i * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\begin{array}{l}
t_0 := {\left(1 + \frac{i}{n}\right)}^{n}\\
t_1 := \frac{t_0 + -1}{\frac{i}{n}}\\
\mathbf{if}\;t_1 \leq 5 \cdot 10^{-310}:\\
\;\;\;\;\frac{\mathsf{expm1}\left(n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)\right) \cdot 100}{\frac{i}{n}}\\
\mathbf{elif}\;t_1 \leq 1000000000:\\
\;\;\;\;n \cdot \frac{t_0 \cdot 100 + -100}{i}\\
\mathbf{else}:\\
\;\;\;\;100 \cdot \frac{n}{1 + i \cdot \left(-0.5 + i \cdot 0.08333333333333333\right)}\\
\end{array}
Results
| Original | 74.86% |
|---|---|
| Target | 74.94% |
| Herbie | 0.82% |
if (/.f64 (-.f64 (pow.f64 (+.f64 1 (/.f64 i n)) n) 1) (/.f64 i n)) < 4.999999999999985e-310Initial program 72.39
Applied egg-rr0.46
if 4.999999999999985e-310 < (/.f64 (-.f64 (pow.f64 (+.f64 1 (/.f64 i n)) n) 1) (/.f64 i n)) < 1e9Initial program 5.57
Simplified5.42
[Start]5.57 | \[ 100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\] |
|---|---|
associate-/r/ [=>]5.54 | \[ 100 \cdot \color{blue}{\left(\frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{i} \cdot n\right)}
\] |
associate-*r* [=>]5.56 | \[ \color{blue}{\left(100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{i}\right) \cdot n}
\] |
*-lft-identity [<=]5.56 | \[ \left(100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{i}\right) \cdot \color{blue}{\left(1 \cdot n\right)}
\] |
associate-*r* [=>]5.56 | \[ \color{blue}{\left(\left(100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{i}\right) \cdot 1\right) \cdot n}
\] |
*-commutative [=>]5.56 | \[ \color{blue}{n \cdot \left(\left(100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{i}\right) \cdot 1\right)}
\] |
*-commutative [=>]5.56 | \[ n \cdot \color{blue}{\left(1 \cdot \left(100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{i}\right)\right)}
\] |
*-lft-identity [=>]5.56 | \[ n \cdot \color{blue}{\left(100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{i}\right)}
\] |
associate-*r/ [=>]5.42 | \[ n \cdot \color{blue}{\frac{100 \cdot \left({\left(1 + \frac{i}{n}\right)}^{n} - 1\right)}{i}}
\] |
sub-neg [=>]5.42 | \[ n \cdot \frac{100 \cdot \color{blue}{\left({\left(1 + \frac{i}{n}\right)}^{n} + \left(-1\right)\right)}}{i}
\] |
distribute-lft-in [=>]5.53 | \[ n \cdot \frac{\color{blue}{100 \cdot {\left(1 + \frac{i}{n}\right)}^{n} + 100 \cdot \left(-1\right)}}{i}
\] |
fma-def [=>]5.42 | \[ n \cdot \frac{\color{blue}{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, 100 \cdot \left(-1\right)\right)}}{i}
\] |
metadata-eval [=>]5.42 | \[ n \cdot \frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, 100 \cdot \color{blue}{-1}\right)}{i}
\] |
metadata-eval [=>]5.42 | \[ n \cdot \frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, \color{blue}{-100}\right)}{i}
\] |
Applied egg-rr5.53
if 1e9 < (/.f64 (-.f64 (pow.f64 (+.f64 1 (/.f64 i n)) n) 1) (/.f64 i n)) Initial program 99.11
Taylor expanded in n around inf 97.63
Simplified21.92
[Start]97.63 | \[ 100 \cdot \frac{n \cdot \left(e^{i} - 1\right)}{i}
\] |
|---|---|
*-commutative [=>]97.63 | \[ \color{blue}{\frac{n \cdot \left(e^{i} - 1\right)}{i} \cdot 100}
\] |
associate-/l* [=>]97.63 | \[ \color{blue}{\frac{n}{\frac{i}{e^{i} - 1}}} \cdot 100
\] |
expm1-def [=>]21.92 | \[ \frac{n}{\frac{i}{\color{blue}{\mathsf{expm1}\left(i\right)}}} \cdot 100
\] |
Taylor expanded in i around 0 1.03
Simplified1.03
[Start]1.03 | \[ \frac{n}{1 + \left(0.08333333333333333 \cdot {i}^{2} + -0.5 \cdot i\right)} \cdot 100
\] |
|---|---|
+-commutative [=>]1.03 | \[ \frac{n}{1 + \color{blue}{\left(-0.5 \cdot i + 0.08333333333333333 \cdot {i}^{2}\right)}} \cdot 100
\] |
*-commutative [=>]1.03 | \[ \frac{n}{1 + \left(\color{blue}{i \cdot -0.5} + 0.08333333333333333 \cdot {i}^{2}\right)} \cdot 100
\] |
*-commutative [=>]1.03 | \[ \frac{n}{1 + \left(i \cdot -0.5 + \color{blue}{{i}^{2} \cdot 0.08333333333333333}\right)} \cdot 100
\] |
unpow2 [=>]1.03 | \[ \frac{n}{1 + \left(i \cdot -0.5 + \color{blue}{\left(i \cdot i\right)} \cdot 0.08333333333333333\right)} \cdot 100
\] |
associate-*l* [=>]1.03 | \[ \frac{n}{1 + \left(i \cdot -0.5 + \color{blue}{i \cdot \left(i \cdot 0.08333333333333333\right)}\right)} \cdot 100
\] |
distribute-lft-out [=>]1.03 | \[ \frac{n}{1 + \color{blue}{i \cdot \left(-0.5 + i \cdot 0.08333333333333333\right)}} \cdot 100
\] |
Final simplification0.82
| Alternative 1 | |
|---|---|
| Error | 1.95% |
| Cost | 21768 |
| Alternative 2 | |
|---|---|
| Error | 1.8% |
| Cost | 21768 |
| Alternative 3 | |
|---|---|
| Error | 18.92% |
| Cost | 7244 |
| Alternative 4 | |
|---|---|
| Error | 18.53% |
| Cost | 7244 |
| Alternative 5 | |
|---|---|
| Error | 18.63% |
| Cost | 7244 |
| Alternative 6 | |
|---|---|
| Error | 21.21% |
| Cost | 6980 |
| Alternative 7 | |
|---|---|
| Error | 21.44% |
| Cost | 6980 |
| Alternative 8 | |
|---|---|
| Error | 28.82% |
| Cost | 1224 |
| Alternative 9 | |
|---|---|
| Error | 28.9% |
| Cost | 1096 |
| Alternative 10 | |
|---|---|
| Error | 31% |
| Cost | 972 |
| Alternative 11 | |
|---|---|
| Error | 29% |
| Cost | 968 |
| Alternative 12 | |
|---|---|
| Error | 29.7% |
| Cost | 841 |
| Alternative 13 | |
|---|---|
| Error | 29.7% |
| Cost | 840 |
| Alternative 14 | |
|---|---|
| Error | 29.7% |
| Cost | 713 |
| Alternative 15 | |
|---|---|
| Error | 33.06% |
| Cost | 712 |
| Alternative 16 | |
|---|---|
| Error | 33.51% |
| Cost | 585 |
| Alternative 17 | |
|---|---|
| Error | 33.41% |
| Cost | 584 |
| Alternative 18 | |
|---|---|
| Error | 32.86% |
| Cost | 448 |
| Alternative 19 | |
|---|---|
| Error | 96.98% |
| Cost | 192 |
| Alternative 20 | |
|---|---|
| Error | 43.96% |
| Cost | 192 |
herbie shell --seed 2023089
(FPCore (i n)
:name "Compound Interest"
:precision binary64
:herbie-target
(* 100.0 (/ (- (exp (* n (if (== (+ 1.0 (/ i n)) 1.0) (/ i n) (/ (* (/ i n) (log (+ 1.0 (/ i n)))) (- (+ (/ i n) 1.0) 1.0))))) 1.0) (/ i n)))
(* 100.0 (/ (- (pow (+ 1.0 (/ i n)) n) 1.0) (/ i n))))