Average Error: 42.9 → 10.6
Time: 37.0s
Precision: 64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
\[\begin{array}{l} \mathbf{if}\;n \le -5.946757000840379:\\ \;\;\;\;\frac{\mathsf{expm1}\left(i\right)}{i} \cdot \left(100 \cdot n\right)\\ \mathbf{elif}\;n \le -7.207093076091718 \cdot 10^{-232}:\\ \;\;\;\;100 \cdot \frac{1}{\frac{\frac{i}{n}}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n\right)}}\\ \mathbf{elif}\;n \le 7.328121567592983 \cdot 10^{-186}:\\ \;\;\;\;0\\ \mathbf{elif}\;n \le 228.0818158074512:\\ \;\;\;\;\frac{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n\right)}{\frac{i}{n}} \cdot 100\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{expm1}\left(i\right)}{i} \cdot \left(100 \cdot n\right)\\ \end{array}\]
100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\begin{array}{l}
\mathbf{if}\;n \le -5.946757000840379:\\
\;\;\;\;\frac{\mathsf{expm1}\left(i\right)}{i} \cdot \left(100 \cdot n\right)\\

\mathbf{elif}\;n \le -7.207093076091718 \cdot 10^{-232}:\\
\;\;\;\;100 \cdot \frac{1}{\frac{\frac{i}{n}}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n\right)}}\\

\mathbf{elif}\;n \le 7.328121567592983 \cdot 10^{-186}:\\
\;\;\;\;0\\

\mathbf{elif}\;n \le 228.0818158074512:\\
\;\;\;\;\frac{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n\right)}{\frac{i}{n}} \cdot 100\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{expm1}\left(i\right)}{i} \cdot \left(100 \cdot n\right)\\

\end{array}
double f(double i, double n) {
        double r3720872 = 100.0;
        double r3720873 = 1.0;
        double r3720874 = i;
        double r3720875 = n;
        double r3720876 = r3720874 / r3720875;
        double r3720877 = r3720873 + r3720876;
        double r3720878 = pow(r3720877, r3720875);
        double r3720879 = r3720878 - r3720873;
        double r3720880 = r3720879 / r3720876;
        double r3720881 = r3720872 * r3720880;
        return r3720881;
}

double f(double i, double n) {
        double r3720882 = n;
        double r3720883 = -5.946757000840379;
        bool r3720884 = r3720882 <= r3720883;
        double r3720885 = i;
        double r3720886 = expm1(r3720885);
        double r3720887 = r3720886 / r3720885;
        double r3720888 = 100.0;
        double r3720889 = r3720888 * r3720882;
        double r3720890 = r3720887 * r3720889;
        double r3720891 = -7.207093076091718e-232;
        bool r3720892 = r3720882 <= r3720891;
        double r3720893 = 1.0;
        double r3720894 = r3720885 / r3720882;
        double r3720895 = log1p(r3720894);
        double r3720896 = r3720895 * r3720882;
        double r3720897 = expm1(r3720896);
        double r3720898 = r3720894 / r3720897;
        double r3720899 = r3720893 / r3720898;
        double r3720900 = r3720888 * r3720899;
        double r3720901 = 7.328121567592983e-186;
        bool r3720902 = r3720882 <= r3720901;
        double r3720903 = 0.0;
        double r3720904 = 228.0818158074512;
        bool r3720905 = r3720882 <= r3720904;
        double r3720906 = r3720897 / r3720894;
        double r3720907 = r3720906 * r3720888;
        double r3720908 = r3720905 ? r3720907 : r3720890;
        double r3720909 = r3720902 ? r3720903 : r3720908;
        double r3720910 = r3720892 ? r3720900 : r3720909;
        double r3720911 = r3720884 ? r3720890 : r3720910;
        return r3720911;
}

Error

Bits error versus i

Bits error versus n

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original42.9
Target42.2
Herbie10.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

  1. Split input into 4 regimes
  2. if n < -5.946757000840379 or 228.0818158074512 < n

    1. Initial program 51.3

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Using strategy rm
    3. Applied add-exp-log55.8

      \[\leadsto 100 \cdot \frac{{\color{blue}{\left(e^{\log \left(1 + \frac{i}{n}\right)}\right)}}^{n} - 1}{\frac{i}{n}}\]
    4. Applied pow-exp55.8

      \[\leadsto 100 \cdot \frac{\color{blue}{e^{\log \left(1 + \frac{i}{n}\right) \cdot n}} - 1}{\frac{i}{n}}\]
    5. Simplified44.9

      \[\leadsto 100 \cdot \frac{e^{\color{blue}{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}} - 1}{\frac{i}{n}}\]
    6. Taylor expanded around inf 45.2

      \[\leadsto 100 \cdot \color{blue}{\frac{\left(e^{i} - 1\right) \cdot n}{i}}\]
    7. Simplified4.8

      \[\leadsto 100 \cdot \color{blue}{\frac{n \cdot \mathsf{expm1}\left(i\right)}{i}}\]
    8. Using strategy rm
    9. Applied *-un-lft-identity4.8

      \[\leadsto 100 \cdot \frac{n \cdot \mathsf{expm1}\left(i\right)}{\color{blue}{1 \cdot i}}\]
    10. Applied times-frac4.8

      \[\leadsto 100 \cdot \color{blue}{\left(\frac{n}{1} \cdot \frac{\mathsf{expm1}\left(i\right)}{i}\right)}\]
    11. Applied associate-*r*5.0

      \[\leadsto \color{blue}{\left(100 \cdot \frac{n}{1}\right) \cdot \frac{\mathsf{expm1}\left(i\right)}{i}}\]
    12. Simplified5.0

      \[\leadsto \color{blue}{\left(100 \cdot n\right)} \cdot \frac{\mathsf{expm1}\left(i\right)}{i}\]

    if -5.946757000840379 < n < -7.207093076091718e-232

    1. Initial program 17.0

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Using strategy rm
    3. Applied add-exp-log17.0

      \[\leadsto 100 \cdot \frac{{\color{blue}{\left(e^{\log \left(1 + \frac{i}{n}\right)}\right)}}^{n} - 1}{\frac{i}{n}}\]
    4. Applied pow-exp17.0

      \[\leadsto 100 \cdot \frac{\color{blue}{e^{\log \left(1 + \frac{i}{n}\right) \cdot n}} - 1}{\frac{i}{n}}\]
    5. Simplified17.0

      \[\leadsto 100 \cdot \frac{e^{\color{blue}{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}} - 1}{\frac{i}{n}}\]
    6. Using strategy rm
    7. Applied clear-num17.0

      \[\leadsto 100 \cdot \color{blue}{\frac{1}{\frac{\frac{i}{n}}{e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)} - 1}}}\]
    8. Simplified27.5

      \[\leadsto 100 \cdot \frac{1}{\color{blue}{\frac{\frac{i}{n}}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n\right)}}}\]

    if -7.207093076091718e-232 < n < 7.328121567592983e-186

    1. Initial program 30.6

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Taylor expanded around 0 14.7

      \[\leadsto \color{blue}{0}\]

    if 7.328121567592983e-186 < n < 228.0818158074512

    1. Initial program 53.5

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Using strategy rm
    3. Applied add-exp-log53.5

      \[\leadsto 100 \cdot \frac{{\color{blue}{\left(e^{\log \left(1 + \frac{i}{n}\right)}\right)}}^{n} - 1}{\frac{i}{n}}\]
    4. Applied pow-exp53.5

      \[\leadsto 100 \cdot \frac{\color{blue}{e^{\log \left(1 + \frac{i}{n}\right) \cdot n}} - 1}{\frac{i}{n}}\]
    5. Simplified53.5

      \[\leadsto 100 \cdot \frac{e^{\color{blue}{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}} - 1}{\frac{i}{n}}\]
    6. Using strategy rm
    7. Applied *-un-lft-identity53.5

      \[\leadsto 100 \cdot \frac{e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)} - 1}{\frac{i}{\color{blue}{1 \cdot n}}}\]
    8. Applied *-un-lft-identity53.5

      \[\leadsto 100 \cdot \frac{e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)} - 1}{\frac{\color{blue}{1 \cdot i}}{1 \cdot n}}\]
    9. Applied times-frac53.5

      \[\leadsto 100 \cdot \frac{e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)} - 1}{\color{blue}{\frac{1}{1} \cdot \frac{i}{n}}}\]
    10. Applied associate-/r*53.5

      \[\leadsto 100 \cdot \color{blue}{\frac{\frac{e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)} - 1}{\frac{1}{1}}}{\frac{i}{n}}}\]
    11. Simplified7.6

      \[\leadsto 100 \cdot \frac{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n\right)}}{\frac{i}{n}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification10.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \le -5.946757000840379:\\ \;\;\;\;\frac{\mathsf{expm1}\left(i\right)}{i} \cdot \left(100 \cdot n\right)\\ \mathbf{elif}\;n \le -7.207093076091718 \cdot 10^{-232}:\\ \;\;\;\;100 \cdot \frac{1}{\frac{\frac{i}{n}}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n\right)}}\\ \mathbf{elif}\;n \le 7.328121567592983 \cdot 10^{-186}:\\ \;\;\;\;0\\ \mathbf{elif}\;n \le 228.0818158074512:\\ \;\;\;\;\frac{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n\right)}{\frac{i}{n}} \cdot 100\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{expm1}\left(i\right)}{i} \cdot \left(100 \cdot n\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2019146 +o rules:numerics
(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))))