Average Error: 42.9 → 29.8
Time: 15.6s
Precision: 64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
\[\begin{array}{l} \mathbf{if}\;n \le -2.51457813685989335 \cdot 10^{153}:\\ \;\;\;\;100 \cdot \frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{\frac{i}{n}}\\ \mathbf{elif}\;n \le -854986621658212610:\\ \;\;\;\;\left(100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{i}\right) \cdot n\\ \mathbf{elif}\;n \le -29.9716390321119768:\\ \;\;\;\;\frac{\mathsf{fma}\left(i, 100, \mathsf{fma}\left(50, {i}^{2}, 100 \cdot \left(\log 1 \cdot n\right)\right) - 50 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{\frac{i}{n}}\\ \mathbf{elif}\;n \le -7.70416426394191 \cdot 10^{-311}:\\ \;\;\;\;100 \cdot \left(\frac{{\left(1 + \frac{i}{n}\right)}^{n}}{\frac{i}{n}} - \frac{1}{\frac{i}{n}}\right)\\ \mathbf{elif}\;n \le 1.86219659931192698 \cdot 10^{-104}:\\ \;\;\;\;\frac{100 \cdot \mathsf{fma}\left(\frac{1}{2} \cdot {\left(\log i\right)}^{2}, {n}^{2}, \mathsf{fma}\left(\frac{1}{2}, {n}^{2} \cdot {\left(\log n\right)}^{2}, \mathsf{fma}\left(\frac{1}{6}, {\left(\log i\right)}^{3} \cdot {n}^{3}, \mathsf{fma}\left(\log i, n, \frac{1}{2} \cdot \left(\log i \cdot \left({n}^{3} \cdot {\left(\log n\right)}^{2}\right)\right)\right)\right)\right) - \mathsf{fma}\left(n, \log n, \mathsf{fma}\left(\log i, {n}^{2} \cdot \log n, \mathsf{fma}\left(\frac{1}{2}, {\left(\log i\right)}^{2} \cdot \left({n}^{3} \cdot \log n\right), \frac{1}{6} \cdot \left({n}^{3} \cdot {\left(\log n\right)}^{3}\right)\right)\right)\right)\right)}{\frac{i}{n}}\\ \mathbf{elif}\;n \le 4.7215330661381895 \cdot 10^{175}:\\ \;\;\;\;100 \cdot \frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;\left(100 \cdot \left({\left(\frac{i}{n}\right)}^{n} - 1\right)\right) \cdot \frac{n}{i}\\ \end{array}\]
100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\begin{array}{l}
\mathbf{if}\;n \le -2.51457813685989335 \cdot 10^{153}:\\
\;\;\;\;100 \cdot \frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{\frac{i}{n}}\\

\mathbf{elif}\;n \le -854986621658212610:\\
\;\;\;\;\left(100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{i}\right) \cdot n\\

\mathbf{elif}\;n \le -29.9716390321119768:\\
\;\;\;\;\frac{\mathsf{fma}\left(i, 100, \mathsf{fma}\left(50, {i}^{2}, 100 \cdot \left(\log 1 \cdot n\right)\right) - 50 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{\frac{i}{n}}\\

\mathbf{elif}\;n \le -7.70416426394191 \cdot 10^{-311}:\\
\;\;\;\;100 \cdot \left(\frac{{\left(1 + \frac{i}{n}\right)}^{n}}{\frac{i}{n}} - \frac{1}{\frac{i}{n}}\right)\\

\mathbf{elif}\;n \le 1.86219659931192698 \cdot 10^{-104}:\\
\;\;\;\;\frac{100 \cdot \mathsf{fma}\left(\frac{1}{2} \cdot {\left(\log i\right)}^{2}, {n}^{2}, \mathsf{fma}\left(\frac{1}{2}, {n}^{2} \cdot {\left(\log n\right)}^{2}, \mathsf{fma}\left(\frac{1}{6}, {\left(\log i\right)}^{3} \cdot {n}^{3}, \mathsf{fma}\left(\log i, n, \frac{1}{2} \cdot \left(\log i \cdot \left({n}^{3} \cdot {\left(\log n\right)}^{2}\right)\right)\right)\right)\right) - \mathsf{fma}\left(n, \log n, \mathsf{fma}\left(\log i, {n}^{2} \cdot \log n, \mathsf{fma}\left(\frac{1}{2}, {\left(\log i\right)}^{2} \cdot \left({n}^{3} \cdot \log n\right), \frac{1}{6} \cdot \left({n}^{3} \cdot {\left(\log n\right)}^{3}\right)\right)\right)\right)\right)}{\frac{i}{n}}\\

\mathbf{elif}\;n \le 4.7215330661381895 \cdot 10^{175}:\\
\;\;\;\;100 \cdot \frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{\frac{i}{n}}\\

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

\end{array}
double f(double i, double n) {
        double r153109 = 100.0;
        double r153110 = 1.0;
        double r153111 = i;
        double r153112 = n;
        double r153113 = r153111 / r153112;
        double r153114 = r153110 + r153113;
        double r153115 = pow(r153114, r153112);
        double r153116 = r153115 - r153110;
        double r153117 = r153116 / r153113;
        double r153118 = r153109 * r153117;
        return r153118;
}

double f(double i, double n) {
        double r153119 = n;
        double r153120 = -2.5145781368598933e+153;
        bool r153121 = r153119 <= r153120;
        double r153122 = 100.0;
        double r153123 = i;
        double r153124 = 1.0;
        double r153125 = 0.5;
        double r153126 = 2.0;
        double r153127 = pow(r153123, r153126);
        double r153128 = log(r153124);
        double r153129 = r153128 * r153119;
        double r153130 = fma(r153125, r153127, r153129);
        double r153131 = r153127 * r153128;
        double r153132 = r153125 * r153131;
        double r153133 = r153130 - r153132;
        double r153134 = fma(r153123, r153124, r153133);
        double r153135 = r153123 / r153119;
        double r153136 = r153134 / r153135;
        double r153137 = r153122 * r153136;
        double r153138 = -8.549866216582126e+17;
        bool r153139 = r153119 <= r153138;
        double r153140 = r153124 + r153135;
        double r153141 = pow(r153140, r153119);
        double r153142 = r153141 - r153124;
        double r153143 = r153142 / r153123;
        double r153144 = r153122 * r153143;
        double r153145 = r153144 * r153119;
        double r153146 = -29.971639032111977;
        bool r153147 = r153119 <= r153146;
        double r153148 = 50.0;
        double r153149 = r153122 * r153129;
        double r153150 = fma(r153148, r153127, r153149);
        double r153151 = r153148 * r153131;
        double r153152 = r153150 - r153151;
        double r153153 = fma(r153123, r153122, r153152);
        double r153154 = r153153 / r153135;
        double r153155 = -7.7041642639419e-311;
        bool r153156 = r153119 <= r153155;
        double r153157 = r153141 / r153135;
        double r153158 = r153124 / r153135;
        double r153159 = r153157 - r153158;
        double r153160 = r153122 * r153159;
        double r153161 = 1.862196599311927e-104;
        bool r153162 = r153119 <= r153161;
        double r153163 = 0.5;
        double r153164 = log(r153123);
        double r153165 = pow(r153164, r153126);
        double r153166 = r153163 * r153165;
        double r153167 = pow(r153119, r153126);
        double r153168 = log(r153119);
        double r153169 = pow(r153168, r153126);
        double r153170 = r153167 * r153169;
        double r153171 = 0.16666666666666666;
        double r153172 = 3.0;
        double r153173 = pow(r153164, r153172);
        double r153174 = pow(r153119, r153172);
        double r153175 = r153173 * r153174;
        double r153176 = r153174 * r153169;
        double r153177 = r153164 * r153176;
        double r153178 = r153163 * r153177;
        double r153179 = fma(r153164, r153119, r153178);
        double r153180 = fma(r153171, r153175, r153179);
        double r153181 = fma(r153163, r153170, r153180);
        double r153182 = r153167 * r153168;
        double r153183 = r153174 * r153168;
        double r153184 = r153165 * r153183;
        double r153185 = pow(r153168, r153172);
        double r153186 = r153174 * r153185;
        double r153187 = r153171 * r153186;
        double r153188 = fma(r153163, r153184, r153187);
        double r153189 = fma(r153164, r153182, r153188);
        double r153190 = fma(r153119, r153168, r153189);
        double r153191 = r153181 - r153190;
        double r153192 = fma(r153166, r153167, r153191);
        double r153193 = r153122 * r153192;
        double r153194 = r153193 / r153135;
        double r153195 = 4.7215330661381895e+175;
        bool r153196 = r153119 <= r153195;
        double r153197 = pow(r153135, r153119);
        double r153198 = r153197 - r153124;
        double r153199 = r153122 * r153198;
        double r153200 = r153119 / r153123;
        double r153201 = r153199 * r153200;
        double r153202 = r153196 ? r153137 : r153201;
        double r153203 = r153162 ? r153194 : r153202;
        double r153204 = r153156 ? r153160 : r153203;
        double r153205 = r153147 ? r153154 : r153204;
        double r153206 = r153139 ? r153145 : r153205;
        double r153207 = r153121 ? r153137 : r153206;
        return r153207;
}

Error

Bits error versus i

Bits error versus n

Target

Original42.9
Target42.2
Herbie29.8
\[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 6 regimes
  2. if n < -2.5145781368598933e+153 or 1.862196599311927e-104 < n < 4.7215330661381895e+175

    1. Initial program 56.5

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

      \[\leadsto 100 \cdot \frac{\color{blue}{\left(1 \cdot i + \left(0.5 \cdot {i}^{2} + \log 1 \cdot n\right)\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)}}{\frac{i}{n}}\]
    3. Simplified36.6

      \[\leadsto 100 \cdot \frac{\color{blue}{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}}{\frac{i}{n}}\]

    if -2.5145781368598933e+153 < n < -8.549866216582126e+17

    1. Initial program 37.9

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Using strategy rm
    3. Applied associate-/r/37.7

      \[\leadsto 100 \cdot \color{blue}{\left(\frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{i} \cdot n\right)}\]
    4. Applied associate-*r*37.7

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

    if -8.549866216582126e+17 < n < -29.971639032111977

    1. Initial program 38.8

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Using strategy rm
    3. Applied associate-*r/38.8

      \[\leadsto \color{blue}{\frac{100 \cdot \left({\left(1 + \frac{i}{n}\right)}^{n} - 1\right)}{\frac{i}{n}}}\]
    4. Taylor expanded around 0 24.8

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

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

    if -29.971639032111977 < n < -7.7041642639419e-311

    1. Initial program 15.7

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

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

    if -7.7041642639419e-311 < n < 1.862196599311927e-104

    1. Initial program 45.1

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Using strategy rm
    3. Applied associate-*r/45.1

      \[\leadsto \color{blue}{\frac{100 \cdot \left({\left(1 + \frac{i}{n}\right)}^{n} - 1\right)}{\frac{i}{n}}}\]
    4. Taylor expanded around inf 26.7

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

      \[\leadsto \frac{100 \cdot \left(\color{blue}{{\left(\frac{i}{n}\right)}^{n}} - 1\right)}{\frac{i}{n}}\]
    6. Taylor expanded around 0 18.1

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

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

    if 4.7215330661381895e+175 < n

    1. Initial program 62.3

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Using strategy rm
    3. Applied associate-*r/62.3

      \[\leadsto \color{blue}{\frac{100 \cdot \left({\left(1 + \frac{i}{n}\right)}^{n} - 1\right)}{\frac{i}{n}}}\]
    4. Taylor expanded around inf 64.0

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

      \[\leadsto \frac{100 \cdot \left(\color{blue}{{\left(\frac{i}{n}\right)}^{n}} - 1\right)}{\frac{i}{n}}\]
    6. Using strategy rm
    7. Applied div-inv44.2

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

      \[\leadsto \left(100 \cdot \left({\left(\frac{i}{n}\right)}^{n} - 1\right)\right) \cdot \color{blue}{\frac{n}{i}}\]
  3. Recombined 6 regimes into one program.
  4. Final simplification29.8

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \le -2.51457813685989335 \cdot 10^{153}:\\ \;\;\;\;100 \cdot \frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{\frac{i}{n}}\\ \mathbf{elif}\;n \le -854986621658212610:\\ \;\;\;\;\left(100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{i}\right) \cdot n\\ \mathbf{elif}\;n \le -29.9716390321119768:\\ \;\;\;\;\frac{\mathsf{fma}\left(i, 100, \mathsf{fma}\left(50, {i}^{2}, 100 \cdot \left(\log 1 \cdot n\right)\right) - 50 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{\frac{i}{n}}\\ \mathbf{elif}\;n \le -7.70416426394191 \cdot 10^{-311}:\\ \;\;\;\;100 \cdot \left(\frac{{\left(1 + \frac{i}{n}\right)}^{n}}{\frac{i}{n}} - \frac{1}{\frac{i}{n}}\right)\\ \mathbf{elif}\;n \le 1.86219659931192698 \cdot 10^{-104}:\\ \;\;\;\;\frac{100 \cdot \mathsf{fma}\left(\frac{1}{2} \cdot {\left(\log i\right)}^{2}, {n}^{2}, \mathsf{fma}\left(\frac{1}{2}, {n}^{2} \cdot {\left(\log n\right)}^{2}, \mathsf{fma}\left(\frac{1}{6}, {\left(\log i\right)}^{3} \cdot {n}^{3}, \mathsf{fma}\left(\log i, n, \frac{1}{2} \cdot \left(\log i \cdot \left({n}^{3} \cdot {\left(\log n\right)}^{2}\right)\right)\right)\right)\right) - \mathsf{fma}\left(n, \log n, \mathsf{fma}\left(\log i, {n}^{2} \cdot \log n, \mathsf{fma}\left(\frac{1}{2}, {\left(\log i\right)}^{2} \cdot \left({n}^{3} \cdot \log n\right), \frac{1}{6} \cdot \left({n}^{3} \cdot {\left(\log n\right)}^{3}\right)\right)\right)\right)\right)}{\frac{i}{n}}\\ \mathbf{elif}\;n \le 4.7215330661381895 \cdot 10^{175}:\\ \;\;\;\;100 \cdot \frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;\left(100 \cdot \left({\left(\frac{i}{n}\right)}^{n} - 1\right)\right) \cdot \frac{n}{i}\\ \end{array}\]

Reproduce

herbie shell --seed 2020035 +o rules:numerics
(FPCore (i n)
  :name "Compound Interest"
  :precision binary64

  :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))))