Average Error: 42.3 → 12.1
Time: 28.5s
Precision: 64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
\[\begin{array}{l} \mathbf{if}\;i \le -6.985621920368718 \cdot 10^{-06}:\\ \;\;\;\;\frac{\mathsf{fma}\left(100, e^{\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n}, -100\right)}{i} \cdot n\\ \mathbf{elif}\;i \le 0.5685613637685243:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(\frac{1}{200}, \frac{\frac{i}{n}}{n} - \frac{i}{n}, \frac{\frac{1}{100}}{n}\right)}\\ \mathbf{elif}\;i \le 2.695991510921252 \cdot 10^{+276}:\\ \;\;\;\;\frac{\mathsf{fma}\left(n \cdot n, \left(\log n \cdot \log n\right) \cdot 50, \mathsf{fma}\left(\frac{50}{3} \cdot \left(n \cdot \left(n \cdot n\right)\right), \left(\log i \cdot \log i\right) \cdot \log i, \mathsf{fma}\left(100 \cdot \log i, n, \mathsf{fma}\left(n \cdot n, 50 \cdot \left(\log i \cdot \log i\right), \left(\left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \log i\right) \cdot \left(\log n \cdot \log n\right)\right) \cdot 50\right)\right) - \mathsf{fma}\left(\frac{100}{3} \cdot \left(n \cdot \left(n \cdot n\right)\right), \log n \cdot \left(\log i \cdot \log i\right), \mathsf{fma}\left(\left(\left(n \cdot n\right) \cdot \log n\right) \cdot \log i, 100, \mathsf{fma}\left(100 \cdot n, \log n, \left(\frac{50}{3} \cdot \left(n \cdot \left(n \cdot n\right)\right)\right) \cdot \mathsf{fma}\left(\log n \cdot \log n, \log n, \log n \cdot \left(\log i \cdot \log i\right)\right)\right)\right)\right)\right)\right)}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, -100\right)}{i} \cdot n\\ \end{array}\]
100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\begin{array}{l}
\mathbf{if}\;i \le -6.985621920368718 \cdot 10^{-06}:\\
\;\;\;\;\frac{\mathsf{fma}\left(100, e^{\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n}, -100\right)}{i} \cdot n\\

\mathbf{elif}\;i \le 0.5685613637685243:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(\frac{1}{200}, \frac{\frac{i}{n}}{n} - \frac{i}{n}, \frac{\frac{1}{100}}{n}\right)}\\

\mathbf{elif}\;i \le 2.695991510921252 \cdot 10^{+276}:\\
\;\;\;\;\frac{\mathsf{fma}\left(n \cdot n, \left(\log n \cdot \log n\right) \cdot 50, \mathsf{fma}\left(\frac{50}{3} \cdot \left(n \cdot \left(n \cdot n\right)\right), \left(\log i \cdot \log i\right) \cdot \log i, \mathsf{fma}\left(100 \cdot \log i, n, \mathsf{fma}\left(n \cdot n, 50 \cdot \left(\log i \cdot \log i\right), \left(\left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \log i\right) \cdot \left(\log n \cdot \log n\right)\right) \cdot 50\right)\right) - \mathsf{fma}\left(\frac{100}{3} \cdot \left(n \cdot \left(n \cdot n\right)\right), \log n \cdot \left(\log i \cdot \log i\right), \mathsf{fma}\left(\left(\left(n \cdot n\right) \cdot \log n\right) \cdot \log i, 100, \mathsf{fma}\left(100 \cdot n, \log n, \left(\frac{50}{3} \cdot \left(n \cdot \left(n \cdot n\right)\right)\right) \cdot \mathsf{fma}\left(\log n \cdot \log n, \log n, \log n \cdot \left(\log i \cdot \log i\right)\right)\right)\right)\right)\right)\right)}{\frac{i}{n}}\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, -100\right)}{i} \cdot n\\

\end{array}
double f(double i, double n) {
        double r5248803 = 100.0;
        double r5248804 = 1.0;
        double r5248805 = i;
        double r5248806 = n;
        double r5248807 = r5248805 / r5248806;
        double r5248808 = r5248804 + r5248807;
        double r5248809 = pow(r5248808, r5248806);
        double r5248810 = r5248809 - r5248804;
        double r5248811 = r5248810 / r5248807;
        double r5248812 = r5248803 * r5248811;
        return r5248812;
}

double f(double i, double n) {
        double r5248813 = i;
        double r5248814 = -6.985621920368718e-06;
        bool r5248815 = r5248813 <= r5248814;
        double r5248816 = 100.0;
        double r5248817 = n;
        double r5248818 = r5248813 / r5248817;
        double r5248819 = log1p(r5248818);
        double r5248820 = r5248819 * r5248817;
        double r5248821 = exp(r5248820);
        double r5248822 = -100.0;
        double r5248823 = fma(r5248816, r5248821, r5248822);
        double r5248824 = r5248823 / r5248813;
        double r5248825 = r5248824 * r5248817;
        double r5248826 = 0.5685613637685243;
        bool r5248827 = r5248813 <= r5248826;
        double r5248828 = 1.0;
        double r5248829 = 0.005;
        double r5248830 = r5248818 / r5248817;
        double r5248831 = r5248830 - r5248818;
        double r5248832 = 0.01;
        double r5248833 = r5248832 / r5248817;
        double r5248834 = fma(r5248829, r5248831, r5248833);
        double r5248835 = r5248828 / r5248834;
        double r5248836 = 2.695991510921252e+276;
        bool r5248837 = r5248813 <= r5248836;
        double r5248838 = r5248817 * r5248817;
        double r5248839 = log(r5248817);
        double r5248840 = r5248839 * r5248839;
        double r5248841 = 50.0;
        double r5248842 = r5248840 * r5248841;
        double r5248843 = 16.666666666666668;
        double r5248844 = r5248817 * r5248838;
        double r5248845 = r5248843 * r5248844;
        double r5248846 = log(r5248813);
        double r5248847 = r5248846 * r5248846;
        double r5248848 = r5248847 * r5248846;
        double r5248849 = r5248816 * r5248846;
        double r5248850 = r5248841 * r5248847;
        double r5248851 = r5248844 * r5248846;
        double r5248852 = r5248851 * r5248840;
        double r5248853 = r5248852 * r5248841;
        double r5248854 = fma(r5248838, r5248850, r5248853);
        double r5248855 = fma(r5248849, r5248817, r5248854);
        double r5248856 = 33.333333333333336;
        double r5248857 = r5248856 * r5248844;
        double r5248858 = r5248839 * r5248847;
        double r5248859 = r5248838 * r5248839;
        double r5248860 = r5248859 * r5248846;
        double r5248861 = r5248816 * r5248817;
        double r5248862 = fma(r5248840, r5248839, r5248858);
        double r5248863 = r5248845 * r5248862;
        double r5248864 = fma(r5248861, r5248839, r5248863);
        double r5248865 = fma(r5248860, r5248816, r5248864);
        double r5248866 = fma(r5248857, r5248858, r5248865);
        double r5248867 = r5248855 - r5248866;
        double r5248868 = fma(r5248845, r5248848, r5248867);
        double r5248869 = fma(r5248838, r5248842, r5248868);
        double r5248870 = r5248869 / r5248818;
        double r5248871 = r5248828 + r5248818;
        double r5248872 = pow(r5248871, r5248817);
        double r5248873 = fma(r5248816, r5248872, r5248822);
        double r5248874 = r5248873 / r5248813;
        double r5248875 = r5248874 * r5248817;
        double r5248876 = r5248837 ? r5248870 : r5248875;
        double r5248877 = r5248827 ? r5248835 : r5248876;
        double r5248878 = r5248815 ? r5248825 : r5248877;
        return r5248878;
}

Error

Bits error versus i

Bits error versus n

Target

Original42.3
Target41.9
Herbie12.1
\[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 i < -6.985621920368718e-06

    1. Initial program 28.5

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Simplified28.5

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, -100\right)}{\frac{i}{n}}}\]
    3. Using strategy rm
    4. Applied add-exp-log28.6

      \[\leadsto \frac{\mathsf{fma}\left(100, {\color{blue}{\left(e^{\log \left(1 + \frac{i}{n}\right)}\right)}}^{n}, -100\right)}{\frac{i}{n}}\]
    5. Applied pow-exp28.6

      \[\leadsto \frac{\mathsf{fma}\left(100, \color{blue}{e^{\log \left(1 + \frac{i}{n}\right) \cdot n}}, -100\right)}{\frac{i}{n}}\]
    6. Simplified6.3

      \[\leadsto \frac{\mathsf{fma}\left(100, e^{\color{blue}{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}}, -100\right)}{\frac{i}{n}}\]
    7. Using strategy rm
    8. Applied associate-/r/6.8

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(100, e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}, -100\right)}{i} \cdot n}\]

    if -6.985621920368718e-06 < i < 0.5685613637685243

    1. Initial program 49.6

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Simplified49.6

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, -100\right)}{\frac{i}{n}}}\]
    3. Using strategy rm
    4. Applied add-exp-log49.6

      \[\leadsto \frac{\mathsf{fma}\left(100, {\color{blue}{\left(e^{\log \left(1 + \frac{i}{n}\right)}\right)}}^{n}, -100\right)}{\frac{i}{n}}\]
    5. Applied pow-exp49.6

      \[\leadsto \frac{\mathsf{fma}\left(100, \color{blue}{e^{\log \left(1 + \frac{i}{n}\right) \cdot n}}, -100\right)}{\frac{i}{n}}\]
    6. Simplified48.9

      \[\leadsto \frac{\mathsf{fma}\left(100, e^{\color{blue}{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}}, -100\right)}{\frac{i}{n}}\]
    7. Using strategy rm
    8. Applied clear-num48.9

      \[\leadsto \color{blue}{\frac{1}{\frac{\frac{i}{n}}{\mathsf{fma}\left(100, e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}, -100\right)}}}\]
    9. Taylor expanded around 0 13.8

      \[\leadsto \frac{1}{\color{blue}{\left(\frac{1}{200} \cdot \frac{i}{{n}^{2}} + \frac{1}{100} \cdot \frac{1}{n}\right) - \frac{1}{200} \cdot \frac{i}{n}}}\]
    10. Simplified13.8

      \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(\frac{i}{n}, \frac{-1}{200}, \mathsf{fma}\left(\frac{i}{n \cdot n}, \frac{1}{200}, \frac{\frac{1}{100}}{n}\right)\right)}}\]
    11. Using strategy rm
    12. Applied clear-num13.8

      \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{fma}\left(\frac{i}{n}, \frac{-1}{200}, \mathsf{fma}\left(\frac{i}{n \cdot n}, \frac{1}{200}, \frac{\frac{1}{100}}{n}\right)\right)}{1}}}\]
    13. Simplified12.7

      \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(\frac{1}{200}, \frac{\frac{i}{n}}{n} - \frac{i}{n}, \frac{\frac{1}{100}}{n}\right)}}\]

    if 0.5685613637685243 < i < 2.695991510921252e+276

    1. Initial program 32.7

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Simplified32.7

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, -100\right)}{\frac{i}{n}}}\]
    3. Using strategy rm
    4. Applied add-exp-log49.3

      \[\leadsto \frac{\mathsf{fma}\left(100, {\color{blue}{\left(e^{\log \left(1 + \frac{i}{n}\right)}\right)}}^{n}, -100\right)}{\frac{i}{n}}\]
    5. Applied pow-exp49.3

      \[\leadsto \frac{\mathsf{fma}\left(100, \color{blue}{e^{\log \left(1 + \frac{i}{n}\right) \cdot n}}, -100\right)}{\frac{i}{n}}\]
    6. Simplified48.1

      \[\leadsto \frac{\mathsf{fma}\left(100, e^{\color{blue}{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}}, -100\right)}{\frac{i}{n}}\]
    7. Taylor expanded around 0 18.4

      \[\leadsto \frac{\color{blue}{\left(50 \cdot \left({n}^{2} \cdot {\left(\log n\right)}^{2}\right) + \left(\frac{50}{3} \cdot \left({n}^{3} \cdot {\left(\log i\right)}^{3}\right) + \left(100 \cdot \left(n \cdot \log i\right) + \left(\frac{100}{3} \cdot \left({n}^{3} \cdot \left({\left(\log n\right)}^{2} \cdot \log i\right)\right) + \left(\frac{50}{3} \cdot \left({n}^{3} \cdot \left(\log i \cdot {\left(\log n\right)}^{2}\right)\right) + 50 \cdot \left({n}^{2} \cdot {\left(\log i\right)}^{2}\right)\right)\right)\right)\right)\right) - \left(\frac{100}{3} \cdot \left({n}^{3} \cdot \left({\left(\log i\right)}^{2} \cdot \log n\right)\right) + \left(50 \cdot \left({n}^{2} \cdot \left(\log n \cdot \log i\right)\right) + \left(50 \cdot \left({n}^{2} \cdot \left(\log i \cdot \log n\right)\right) + \left(\frac{50}{3} \cdot \left({n}^{3} \cdot {\left(\log n\right)}^{3}\right) + \left(\frac{50}{3} \cdot \left({n}^{3} \cdot \left(\log n \cdot {\left(\log i\right)}^{2}\right)\right) + 100 \cdot \left(n \cdot \log n\right)\right)\right)\right)\right)\right)}}{\frac{i}{n}}\]
    8. Simplified18.4

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(n \cdot n, \left(\log n \cdot \log n\right) \cdot 50, \mathsf{fma}\left(\frac{50}{3} \cdot \left(\left(n \cdot n\right) \cdot n\right), \log i \cdot \left(\log i \cdot \log i\right), \mathsf{fma}\left(100 \cdot \log i, n, \mathsf{fma}\left(n \cdot n, \left(\log i \cdot \log i\right) \cdot 50, \left(\left(\left(\left(n \cdot n\right) \cdot n\right) \cdot \log i\right) \cdot \left(\log n \cdot \log n\right)\right) \cdot \left(\frac{100}{3} + \frac{50}{3}\right)\right)\right) - \mathsf{fma}\left(\frac{100}{3} \cdot \left(\left(n \cdot n\right) \cdot n\right), \log n \cdot \left(\log i \cdot \log i\right), \mathsf{fma}\left(\log i \cdot \left(\log n \cdot \left(n \cdot n\right)\right), 100, \mathsf{fma}\left(100 \cdot n, \log n, \left(\frac{50}{3} \cdot \left(\left(n \cdot n\right) \cdot n\right)\right) \cdot \mathsf{fma}\left(\log n \cdot \log n, \log n, \log n \cdot \left(\log i \cdot \log i\right)\right)\right)\right)\right)\right)\right)}}{\frac{i}{n}}\]

    if 2.695991510921252e+276 < i

    1. Initial program 30.6

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Simplified30.5

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, -100\right)}{\frac{i}{n}}}\]
    3. Using strategy rm
    4. Applied associate-/r/30.5

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, -100\right)}{i} \cdot n}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification12.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \le -6.985621920368718 \cdot 10^{-06}:\\ \;\;\;\;\frac{\mathsf{fma}\left(100, e^{\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n}, -100\right)}{i} \cdot n\\ \mathbf{elif}\;i \le 0.5685613637685243:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(\frac{1}{200}, \frac{\frac{i}{n}}{n} - \frac{i}{n}, \frac{\frac{1}{100}}{n}\right)}\\ \mathbf{elif}\;i \le 2.695991510921252 \cdot 10^{+276}:\\ \;\;\;\;\frac{\mathsf{fma}\left(n \cdot n, \left(\log n \cdot \log n\right) \cdot 50, \mathsf{fma}\left(\frac{50}{3} \cdot \left(n \cdot \left(n \cdot n\right)\right), \left(\log i \cdot \log i\right) \cdot \log i, \mathsf{fma}\left(100 \cdot \log i, n, \mathsf{fma}\left(n \cdot n, 50 \cdot \left(\log i \cdot \log i\right), \left(\left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \log i\right) \cdot \left(\log n \cdot \log n\right)\right) \cdot 50\right)\right) - \mathsf{fma}\left(\frac{100}{3} \cdot \left(n \cdot \left(n \cdot n\right)\right), \log n \cdot \left(\log i \cdot \log i\right), \mathsf{fma}\left(\left(\left(n \cdot n\right) \cdot \log n\right) \cdot \log i, 100, \mathsf{fma}\left(100 \cdot n, \log n, \left(\frac{50}{3} \cdot \left(n \cdot \left(n \cdot n\right)\right)\right) \cdot \mathsf{fma}\left(\log n \cdot \log n, \log n, \log n \cdot \left(\log i \cdot \log i\right)\right)\right)\right)\right)\right)\right)}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, -100\right)}{i} \cdot n\\ \end{array}\]

Reproduce

herbie shell --seed 2019164 +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))))