Average Error: 53.1 → 10.7
Time: 2.0m
Precision: 64
Internal Precision: 2432
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
↓
\[\begin{array}{l}
\mathbf{if}\;i \le -2.849201036281836 \cdot 10^{-09}:\\
\;\;\;\;100 \cdot \frac{\left(\sqrt[3]{{\left(1 + \frac{i}{n}\right)}^{n} - 1} \cdot \sqrt[3]{{\left(1 + \frac{i}{n}\right)}^{n} - 1}\right) \cdot \sqrt[3]{{\left(1 + \frac{i}{n}\right)}^{n} - 1}}{\frac{i}{n}}\\
\mathbf{if}\;i \le 1.1570910217973246 \cdot 10^{-07}:\\
\;\;\;\;\left(50 \cdot i + 100\right) \cdot n\\
\mathbf{if}\;i \le 6.896033226216904 \cdot 10^{+172}:\\
\;\;\;\;100 \cdot \frac{\left(\sqrt[3]{{\left(1 + \frac{i}{n}\right)}^{n} - 1} \cdot \sqrt[3]{{\left(1 + \frac{i}{n}\right)}^{n} - 1}\right) \cdot \sqrt[3]{{\left(1 + \frac{i}{n}\right)}^{n} - 1}}{\frac{i}{n}}\\
\mathbf{else}:\\
\;\;\;\;100 \cdot \frac{e^{\frac{\log n - \log i}{n}} - 1}{\frac{i}{n}}\\
\end{array}\]
Target
| Original | 53.1 |
|---|
| Target | 51.0 |
|---|
| Herbie | 10.7 |
|---|
\[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 3 regimes
if i < -2.849201036281836e-09 or 1.1570910217973246e-07 < i < 6.896033226216904e+172
Initial program 30.2
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
- Using strategy
rm Applied add-cube-cbrt30.2
\[\leadsto 100 \cdot \frac{\color{blue}{\left(\sqrt[3]{{\left(1 + \frac{i}{n}\right)}^{n} - 1} \cdot \sqrt[3]{{\left(1 + \frac{i}{n}\right)}^{n} - 1}\right) \cdot \sqrt[3]{{\left(1 + \frac{i}{n}\right)}^{n} - 1}}}{\frac{i}{n}}\]
if -2.849201036281836e-09 < i < 1.1570910217973246e-07
Initial program 61.8
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
Taylor expanded around 0 60.7
\[\leadsto 100 \cdot \frac{\color{blue}{\left(\frac{1}{2} \cdot {i}^{2} + \left(1 + i\right)\right)} - 1}{\frac{i}{n}}\]
Applied simplify15.2
\[\leadsto \color{blue}{\frac{i + i \cdot \left(i \cdot \frac{1}{2}\right)}{\frac{\frac{i}{n}}{100}}}\]
Taylor expanded around 0 0.0
\[\leadsto \color{blue}{100 \cdot n + 50 \cdot \left(n \cdot i\right)}\]
Applied simplify0.0
\[\leadsto \color{blue}{\left(50 \cdot i + 100\right) \cdot n}\]
if 6.896033226216904e+172 < i
Initial program 62.4
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
Taylor expanded around inf 29.9
\[\leadsto 100 \cdot \frac{\color{blue}{e^{\frac{\log n - \log i}{n}} - 1}}{\frac{i}{n}}\]
- Recombined 3 regimes into one program.
- Removed slow
pow expressions.
Runtime
herbie shell --seed '#(1567391828 2030694642 2833800258 828025724 3004380912 3532991858)' +o setup:early-exit
(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))))