Average Error: 47.4 → 17.6
Time: 3.3m
Precision: 64
Internal Precision: 3456
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
↓
\[\begin{array}{l}
\mathbf{if}\;i \le -9.496970096237412 \cdot 10^{+24}:\\
\;\;\;\;\frac{100 \cdot \left({\left(1 + \frac{i}{n}\right)}^{n} - 1\right)}{\frac{i}{n}}\\
\mathbf{if}\;i \le 0.9728556327625646:\\
\;\;\;\;100 \cdot \left(\frac{\left(\frac{1}{6} \cdot i + \frac{1}{2}\right) \cdot \left(i \cdot i\right) + i}{i} \cdot n\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{100 \cdot \left({\left(1 + \frac{i}{n}\right)}^{n} - 1\right)}{\frac{i}{n}}\\
\end{array}\]
Target
| Original | 47.4 |
|---|
| Target | 46.8 |
|---|
| Herbie | 17.6 |
|---|
\[100 \cdot \frac{e^{n \cdot \begin{array}{l}
\mathbf{if}\;1 + \frac{i}{n} = 1:\\
\;\;\;\;\frac{i}{n}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{i}{n} \cdot \log \left(1 + \frac{i}{n}\right)}{\left(\frac{i}{n} + 1\right) - 1}\\
\end{array}} - 1}{\frac{i}{n}}\]
Derivation
- Split input into 2 regimes
if i < -9.496970096237412e+24 or 0.9728556327625646 < i
Initial program 29.0
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
- Using strategy
rm Applied associate-*r/29.0
\[\leadsto \color{blue}{\frac{100 \cdot \left({\left(1 + \frac{i}{n}\right)}^{n} - 1\right)}{\frac{i}{n}}}\]
if -9.496970096237412e+24 < i < 0.9728556327625646
Initial program 56.9
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
Taylor expanded around 0 27.3
\[\leadsto 100 \cdot \frac{\color{blue}{\frac{1}{2} \cdot {i}^{2} + \left(\frac{1}{6} \cdot {i}^{3} + i\right)}}{\frac{i}{n}}\]
- Using strategy
rm Applied associate-/r/11.7
\[\leadsto 100 \cdot \color{blue}{\left(\frac{\frac{1}{2} \cdot {i}^{2} + \left(\frac{1}{6} \cdot {i}^{3} + i\right)}{i} \cdot n\right)}\]
Applied simplify11.7
\[\leadsto 100 \cdot \left(\color{blue}{\frac{\left(\frac{1}{6} \cdot i + \frac{1}{2}\right) \cdot \left(i \cdot i\right) + i}{i}} \cdot n\right)\]
- Recombined 2 regimes into one program.
- Removed slow
pow expressions.
Runtime
herbie shell --seed '#(1063282112 2455465480 4141627379 3773598652 1647277307 776739644)'
(FPCore (i n)
:name "Compound Interest"
:herbie-target
(* 100 (/ (- (exp (* n (if (== (+ 1 (/ i n)) 1) (/ i n) (/ (* (/ i n) (log (+ 1 (/ i n)))) (- (+ (/ i n) 1) 1))))) 1) (/ i n)))
(* 100 (/ (- (pow (+ 1 (/ i n)) n) 1) (/ i n))))