Average Error: 47.8 → 17.3
Time: 13.4s
Precision: 64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
\[\begin{array}{l} \mathbf{if}\;i \le -0.0281548125542239701 \lor \neg \left(i \le 9.41291569922092328 \cdot 10^{-13}\right):\\ \;\;\;\;\frac{100 \cdot \left({\left(1 + \frac{i}{n}\right)}^{n} - 1\right)}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;\left(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)}{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}\;i \le -0.0281548125542239701 \lor \neg \left(i \le 9.41291569922092328 \cdot 10^{-13}\right):\\
\;\;\;\;\frac{100 \cdot \left({\left(1 + \frac{i}{n}\right)}^{n} - 1\right)}{\frac{i}{n}}\\

\mathbf{else}:\\
\;\;\;\;\left(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)}{i}\right) \cdot n\\

\end{array}
double f(double i, double n) {
        double r126406 = 100.0;
        double r126407 = 1.0;
        double r126408 = i;
        double r126409 = n;
        double r126410 = r126408 / r126409;
        double r126411 = r126407 + r126410;
        double r126412 = pow(r126411, r126409);
        double r126413 = r126412 - r126407;
        double r126414 = r126413 / r126410;
        double r126415 = r126406 * r126414;
        return r126415;
}

double f(double i, double n) {
        double r126416 = i;
        double r126417 = -0.02815481255422397;
        bool r126418 = r126416 <= r126417;
        double r126419 = 9.412915699220923e-13;
        bool r126420 = r126416 <= r126419;
        double r126421 = !r126420;
        bool r126422 = r126418 || r126421;
        double r126423 = 100.0;
        double r126424 = 1.0;
        double r126425 = n;
        double r126426 = r126416 / r126425;
        double r126427 = r126424 + r126426;
        double r126428 = pow(r126427, r126425);
        double r126429 = r126428 - r126424;
        double r126430 = r126423 * r126429;
        double r126431 = r126430 / r126426;
        double r126432 = 0.5;
        double r126433 = 2.0;
        double r126434 = pow(r126416, r126433);
        double r126435 = log(r126424);
        double r126436 = r126435 * r126425;
        double r126437 = fma(r126432, r126434, r126436);
        double r126438 = r126434 * r126435;
        double r126439 = r126432 * r126438;
        double r126440 = r126437 - r126439;
        double r126441 = fma(r126416, r126424, r126440);
        double r126442 = r126441 / r126416;
        double r126443 = r126423 * r126442;
        double r126444 = r126443 * r126425;
        double r126445 = r126422 ? r126431 : r126444;
        return r126445;
}

Error

Bits error versus i

Bits error versus n

Target

Original47.8
Target47.2
Herbie17.3
\[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 2 regimes
  2. if i < -0.02815481255422397 or 9.412915699220923e-13 < i

    1. Initial program 30.8

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

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

    if -0.02815481255422397 < i < 9.412915699220923e-13

    1. Initial program 58.2

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

      \[\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. Simplified26.5

      \[\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}}\]
    4. Using strategy rm
    5. Applied associate-/r/9.2

      \[\leadsto 100 \cdot \color{blue}{\left(\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)}{i} \cdot n\right)}\]
    6. Applied associate-*r*9.2

      \[\leadsto \color{blue}{\left(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)}{i}\right) \cdot n}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification17.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \le -0.0281548125542239701 \lor \neg \left(i \le 9.41291569922092328 \cdot 10^{-13}\right):\\ \;\;\;\;\frac{100 \cdot \left({\left(1 + \frac{i}{n}\right)}^{n} - 1\right)}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;\left(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)}{i}\right) \cdot n\\ \end{array}\]

Reproduce

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