Average Error: 50.4 → 10.5
Time: 52.3s
Precision: 64
Internal precision: 128
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
⬇
\[\begin{array}{l}
\mathbf{if}\;i \le -3.1308280513783317 \cdot 10^{-09}:\\
\;\;\;\;100 \cdot \frac{{\left({\left(1 + \frac{i}{n}\right)}^{n}\right)}^{3} - {1}^{3}}{\frac{i}{n} \cdot \left({\left({\left(\frac{i}{n} + 1\right)}^{n}\right)}^2 + \left(1 + {\left(\frac{i}{n} + 1\right)}^{n}\right)\right)}\\
\mathbf{if}\;i \le 3.203941410402244 \cdot 10^{-15}:\\
\;\;\;\;\left(i \cdot \frac{1}{2} + 1\right) \cdot \left(100 \cdot n\right)\\
\mathbf{else}:\\
\;\;\;\;100 \cdot \frac{{\left({\left(1 + \frac{i}{n}\right)}^{n}\right)}^{3} - {1}^{3}}{\frac{i}{n} \cdot \left({\left({\left(\frac{i}{n} + 1\right)}^{n}\right)}^2 + \left(1 + {\left(\frac{i}{n} + 1\right)}^{n}\right)\right)}\\
\end{array}\]
Target
| Original | 50.4 |
| Comparison | 50.2 |
| Herbie | 10.5 |
\[ 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 < -3.1308280513783317e-09 or 3.203941410402244e-15 < i
Initial program 29.7
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
- Using strategy
rm
Applied flip3-- 29.7
\[\leadsto 100 \cdot \frac{\color{blue}{\frac{{\left({\left(1 + \frac{i}{n}\right)}^{n}\right)}^{3} - {1}^{3}}{{\left({\left(1 + \frac{i}{n}\right)}^{n}\right)}^2 + \left({1}^2 + {\left(1 + \frac{i}{n}\right)}^{n} \cdot 1\right)}}}{\frac{i}{n}}\]
Applied associate-/l/ 29.8
\[\leadsto 100 \cdot \color{blue}{\frac{{\left({\left(1 + \frac{i}{n}\right)}^{n}\right)}^{3} - {1}^{3}}{\frac{i}{n} \cdot \left({\left({\left(1 + \frac{i}{n}\right)}^{n}\right)}^2 + \left({1}^2 + {\left(1 + \frac{i}{n}\right)}^{n} \cdot 1\right)\right)}}\]
Applied simplify 29.8
\[\leadsto 100 \cdot \frac{{\left({\left(1 + \frac{i}{n}\right)}^{n}\right)}^{3} - {1}^{3}}{\color{blue}{\frac{i}{n} \cdot \left({\left({\left(\frac{i}{n} + 1\right)}^{n}\right)}^2 + \left(1 + {\left(\frac{i}{n} + 1\right)}^{n}\right)\right)}}\]
if -3.1308280513783317e-09 < i < 3.203941410402244e-15
Initial program 61.7
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
Applied taylor 61.0
\[\leadsto 100 \cdot \frac{\left(\frac{1}{2} \cdot {i}^2 + \left(1 + i\right)\right) - 1}{\frac{i}{n}}\]
Taylor expanded around 0 61.0
\[\leadsto 100 \cdot \frac{\color{blue}{\left(\frac{1}{2} \cdot {i}^2 + \left(1 + i\right)\right)} - 1}{\frac{i}{n}}\]
Applied simplify 15.0
\[\leadsto \color{blue}{\left(i \cdot \frac{1}{2} + 1\right) \cdot \frac{i \cdot 100}{\frac{i}{n}}}\]
Applied taylor 0.0
\[\leadsto \left(i \cdot \frac{1}{2} + 1\right) \cdot \left(100 \cdot n\right)\]
Taylor expanded around 0 0.0
\[\leadsto \left(i \cdot \frac{1}{2} + 1\right) \cdot \color{blue}{\left(100 \cdot n\right)}\]
- Recombined 2 regimes into one program.
- Removed slow pow expressions
Runtime
Please include this information when filing a bug report:
herbie --seed '#(2668259955 678661771 1153533617 3222729755 1678190763 2684928584)'
(FPCore (i n)
:name "Compound Interest"
: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))))