Average Error: 43.0 → 22.9
Time: 13.4s
Precision: 64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
\[\begin{array}{l} \mathbf{if}\;n \le -7.76693767745361788861631953913126532955 \cdot 10^{94}:\\ \;\;\;\;\left(100 \cdot \frac{\mathsf{fma}\left(i, 1, \sqrt{\mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right)} \cdot \sqrt{\mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right)} - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}\right) \cdot n\\ \mathbf{elif}\;n \le -9.519350012649904306163831713839410413139 \cdot 10^{-251}:\\ \;\;\;\;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.613414883038850832801467887537631845006 \cdot 10^{-130}:\\ \;\;\;\;100 \cdot \frac{\mathsf{fma}\left(1, i, \mathsf{fma}\left(\log 1, n, 1\right)\right) - 1}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;\left(100 \cdot \frac{\mathsf{fma}\left(i, 1, \sqrt{\mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right)} \cdot \sqrt{\mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right)} - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}\right) \cdot n\\ \end{array}\]
100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\begin{array}{l}
\mathbf{if}\;n \le -7.76693767745361788861631953913126532955 \cdot 10^{94}:\\
\;\;\;\;\left(100 \cdot \frac{\mathsf{fma}\left(i, 1, \sqrt{\mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right)} \cdot \sqrt{\mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right)} - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}\right) \cdot n\\

\mathbf{elif}\;n \le -9.519350012649904306163831713839410413139 \cdot 10^{-251}:\\
\;\;\;\;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.613414883038850832801467887537631845006 \cdot 10^{-130}:\\
\;\;\;\;100 \cdot \frac{\mathsf{fma}\left(1, i, \mathsf{fma}\left(\log 1, n, 1\right)\right) - 1}{\frac{i}{n}}\\

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

\end{array}
double f(double i, double n) {
        double r142134 = 100.0;
        double r142135 = 1.0;
        double r142136 = i;
        double r142137 = n;
        double r142138 = r142136 / r142137;
        double r142139 = r142135 + r142138;
        double r142140 = pow(r142139, r142137);
        double r142141 = r142140 - r142135;
        double r142142 = r142141 / r142138;
        double r142143 = r142134 * r142142;
        return r142143;
}

double f(double i, double n) {
        double r142144 = n;
        double r142145 = -7.766937677453618e+94;
        bool r142146 = r142144 <= r142145;
        double r142147 = 100.0;
        double r142148 = i;
        double r142149 = 1.0;
        double r142150 = 0.5;
        double r142151 = 2.0;
        double r142152 = pow(r142148, r142151);
        double r142153 = log(r142149);
        double r142154 = r142153 * r142144;
        double r142155 = fma(r142150, r142152, r142154);
        double r142156 = sqrt(r142155);
        double r142157 = r142156 * r142156;
        double r142158 = r142152 * r142153;
        double r142159 = r142150 * r142158;
        double r142160 = r142157 - r142159;
        double r142161 = fma(r142148, r142149, r142160);
        double r142162 = r142161 / r142148;
        double r142163 = r142147 * r142162;
        double r142164 = r142163 * r142144;
        double r142165 = -9.519350012649904e-251;
        bool r142166 = r142144 <= r142165;
        double r142167 = r142148 / r142144;
        double r142168 = r142149 + r142167;
        double r142169 = pow(r142168, r142144);
        double r142170 = r142169 / r142167;
        double r142171 = r142149 / r142167;
        double r142172 = r142170 - r142171;
        double r142173 = r142147 * r142172;
        double r142174 = 1.6134148830388508e-130;
        bool r142175 = r142144 <= r142174;
        double r142176 = 1.0;
        double r142177 = fma(r142153, r142144, r142176);
        double r142178 = fma(r142149, r142148, r142177);
        double r142179 = r142178 - r142149;
        double r142180 = r142179 / r142167;
        double r142181 = r142147 * r142180;
        double r142182 = r142175 ? r142181 : r142164;
        double r142183 = r142166 ? r142173 : r142182;
        double r142184 = r142146 ? r142164 : r142183;
        return r142184;
}

Error

Bits error versus i

Bits error versus n

Target

Original43.0
Target42.8
Herbie22.9
\[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 3 regimes
  2. if n < -7.766937677453618e+94 or 1.6134148830388508e-130 < n

    1. Initial program 55.3

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

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

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

      \[\leadsto \left(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)}}{i}\right) \cdot n\]
    6. Simplified21.5

      \[\leadsto \left(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)}}{i}\right) \cdot n\]
    7. Using strategy rm
    8. Applied add-sqr-sqrt21.5

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

    if -7.766937677453618e+94 < n < -9.519350012649904e-251

    1. Initial program 23.8

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

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

    if -9.519350012649904e-251 < n < 1.6134148830388508e-130

    1. Initial program 35.4

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

      \[\leadsto 100 \cdot \frac{\color{blue}{\left(1 \cdot i + \left(\log 1 \cdot n + 1\right)\right)} - 1}{\frac{i}{n}}\]
    3. Simplified26.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \le -7.76693767745361788861631953913126532955 \cdot 10^{94}:\\ \;\;\;\;\left(100 \cdot \frac{\mathsf{fma}\left(i, 1, \sqrt{\mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right)} \cdot \sqrt{\mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right)} - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}\right) \cdot n\\ \mathbf{elif}\;n \le -9.519350012649904306163831713839410413139 \cdot 10^{-251}:\\ \;\;\;\;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.613414883038850832801467887537631845006 \cdot 10^{-130}:\\ \;\;\;\;100 \cdot \frac{\mathsf{fma}\left(1, i, \mathsf{fma}\left(\log 1, n, 1\right)\right) - 1}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;\left(100 \cdot \frac{\mathsf{fma}\left(i, 1, \sqrt{\mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right)} \cdot \sqrt{\mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right)} - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}\right) \cdot n\\ \end{array}\]

Reproduce

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