Average Error: 43.1 → 19.0
Time: 32.9s
Precision: 64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
\[\begin{array}{l} \mathbf{if}\;i \le -42606.74875587941642152145504951477050781:\\ \;\;\;\;100 \cdot \frac{n}{\frac{i}{{\left(\frac{i}{n}\right)}^{n} - 1}}\\ \mathbf{elif}\;i \le 2.857502826738759766942954155696909688723 \cdot 10^{-25}:\\ \;\;\;\;100 \cdot \left(\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(\log 1, n, \left(i \cdot i\right) \cdot \left(0.5 - 0.5 \cdot \log 1\right)\right)\right)}{i} \cdot n\right)\\ \mathbf{elif}\;i \le 1.494091961828663302085378658485801643661 \cdot 10^{142}:\\ \;\;\;\;\left(100 \cdot n\right) \cdot \frac{1}{\frac{i}{\left(\mathsf{fma}\left(\frac{1}{2}, {\left(\log i\right)}^{2} \cdot {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)\right) - \log n \cdot \mathsf{fma}\left(n \cdot n, \log i, n\right)\right) - \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)}}\\ \mathbf{elif}\;i \le 1.804564161742964970188249145928450773024 \cdot 10^{231}:\\ \;\;\;\;100 \cdot \left(\frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{i} \cdot n\right)\\ \mathbf{else}:\\ \;\;\;\;100 \cdot \frac{n}{\frac{i}{\mathsf{fma}\left(\frac{1}{2}, {\left(\log i\right)}^{2} \cdot {n}^{2}, \left(\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) - \log n \cdot \left(\log i \cdot {n}^{2} + n\right)\right) - \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)}}\\ \end{array}\]
100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\begin{array}{l}
\mathbf{if}\;i \le -42606.74875587941642152145504951477050781:\\
\;\;\;\;100 \cdot \frac{n}{\frac{i}{{\left(\frac{i}{n}\right)}^{n} - 1}}\\

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

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

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

\mathbf{else}:\\
\;\;\;\;100 \cdot \frac{n}{\frac{i}{\mathsf{fma}\left(\frac{1}{2}, {\left(\log i\right)}^{2} \cdot {n}^{2}, \left(\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) - \log n \cdot \left(\log i \cdot {n}^{2} + n\right)\right) - \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)}}\\

\end{array}
double f(double i, double n) {
        double r131485 = 100.0;
        double r131486 = 1.0;
        double r131487 = i;
        double r131488 = n;
        double r131489 = r131487 / r131488;
        double r131490 = r131486 + r131489;
        double r131491 = pow(r131490, r131488);
        double r131492 = r131491 - r131486;
        double r131493 = r131492 / r131489;
        double r131494 = r131485 * r131493;
        return r131494;
}

double f(double i, double n) {
        double r131495 = i;
        double r131496 = -42606.74875587942;
        bool r131497 = r131495 <= r131496;
        double r131498 = 100.0;
        double r131499 = n;
        double r131500 = r131495 / r131499;
        double r131501 = pow(r131500, r131499);
        double r131502 = 1.0;
        double r131503 = r131501 - r131502;
        double r131504 = r131495 / r131503;
        double r131505 = r131499 / r131504;
        double r131506 = r131498 * r131505;
        double r131507 = 2.8575028267387598e-25;
        bool r131508 = r131495 <= r131507;
        double r131509 = log(r131502);
        double r131510 = r131495 * r131495;
        double r131511 = 0.5;
        double r131512 = r131511 * r131509;
        double r131513 = r131511 - r131512;
        double r131514 = r131510 * r131513;
        double r131515 = fma(r131509, r131499, r131514);
        double r131516 = fma(r131495, r131502, r131515);
        double r131517 = r131516 / r131495;
        double r131518 = r131517 * r131499;
        double r131519 = r131498 * r131518;
        double r131520 = 1.4940919618286633e+142;
        bool r131521 = r131495 <= r131520;
        double r131522 = r131498 * r131499;
        double r131523 = 1.0;
        double r131524 = 0.5;
        double r131525 = log(r131495);
        double r131526 = 2.0;
        double r131527 = pow(r131525, r131526);
        double r131528 = pow(r131499, r131526);
        double r131529 = r131527 * r131528;
        double r131530 = log(r131499);
        double r131531 = pow(r131530, r131526);
        double r131532 = r131528 * r131531;
        double r131533 = 0.16666666666666666;
        double r131534 = 3.0;
        double r131535 = pow(r131525, r131534);
        double r131536 = pow(r131499, r131534);
        double r131537 = r131535 * r131536;
        double r131538 = r131536 * r131531;
        double r131539 = r131525 * r131538;
        double r131540 = r131524 * r131539;
        double r131541 = fma(r131525, r131499, r131540);
        double r131542 = fma(r131533, r131537, r131541);
        double r131543 = fma(r131524, r131532, r131542);
        double r131544 = fma(r131524, r131529, r131543);
        double r131545 = r131499 * r131499;
        double r131546 = fma(r131545, r131525, r131499);
        double r131547 = r131530 * r131546;
        double r131548 = r131544 - r131547;
        double r131549 = r131536 * r131530;
        double r131550 = r131527 * r131549;
        double r131551 = pow(r131530, r131534);
        double r131552 = r131536 * r131551;
        double r131553 = r131533 * r131552;
        double r131554 = fma(r131524, r131550, r131553);
        double r131555 = r131548 - r131554;
        double r131556 = r131495 / r131555;
        double r131557 = r131523 / r131556;
        double r131558 = r131522 * r131557;
        double r131559 = 1.804564161742965e+231;
        bool r131560 = r131495 <= r131559;
        double r131561 = r131502 + r131500;
        double r131562 = pow(r131561, r131499);
        double r131563 = r131562 - r131502;
        double r131564 = r131563 / r131495;
        double r131565 = r131564 * r131499;
        double r131566 = r131498 * r131565;
        double r131567 = r131525 * r131528;
        double r131568 = r131567 + r131499;
        double r131569 = r131530 * r131568;
        double r131570 = r131543 - r131569;
        double r131571 = r131570 - r131554;
        double r131572 = fma(r131524, r131529, r131571);
        double r131573 = r131495 / r131572;
        double r131574 = r131499 / r131573;
        double r131575 = r131498 * r131574;
        double r131576 = r131560 ? r131566 : r131575;
        double r131577 = r131521 ? r131558 : r131576;
        double r131578 = r131508 ? r131519 : r131577;
        double r131579 = r131497 ? r131506 : r131578;
        return r131579;
}

Error

Bits error versus i

Bits error versus n

Target

Original43.1
Target42.8
Herbie19.0
\[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 5 regimes
  2. if i < -42606.74875587942

    1. Initial program 27.8

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

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

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

    if -42606.74875587942 < i < 2.8575028267387598e-25

    1. Initial program 50.7

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

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

      \[\leadsto 100 \cdot \left(\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} \cdot n\right)\]
    5. Simplified17.0

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

    if 2.8575028267387598e-25 < i < 1.4940919618286633e+142

    1. Initial program 38.2

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

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

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

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

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

      \[\leadsto \left(100 \cdot n\right) \cdot \frac{1}{\frac{i}{\color{blue}{\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)}}}\]
    8. Simplified22.0

      \[\leadsto \left(100 \cdot n\right) \cdot \frac{1}{\frac{i}{\color{blue}{\left(\mathsf{fma}\left(\frac{1}{2}, {\left(\log i\right)}^{2} \cdot {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)\right) - \log n \cdot \mathsf{fma}\left(n \cdot n, \log i, n\right)\right) - \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)}}}\]

    if 1.4940919618286633e+142 < i < 1.804564161742965e+231

    1. Initial program 32.1

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

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

    if 1.804564161742965e+231 < i

    1. Initial program 29.1

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

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

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

      \[\leadsto 100 \cdot \frac{n}{\frac{i}{\color{blue}{\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)}}}\]
    5. Simplified32.1

      \[\leadsto 100 \cdot \frac{n}{\frac{i}{\color{blue}{\mathsf{fma}\left(\frac{1}{2}, {\left(\log i\right)}^{2} \cdot {n}^{2}, \left(\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) - \log n \cdot \left(\log i \cdot {n}^{2} + n\right)\right) - \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)}}}\]
  3. Recombined 5 regimes into one program.
  4. Final simplification19.0

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \le -42606.74875587941642152145504951477050781:\\ \;\;\;\;100 \cdot \frac{n}{\frac{i}{{\left(\frac{i}{n}\right)}^{n} - 1}}\\ \mathbf{elif}\;i \le 2.857502826738759766942954155696909688723 \cdot 10^{-25}:\\ \;\;\;\;100 \cdot \left(\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(\log 1, n, \left(i \cdot i\right) \cdot \left(0.5 - 0.5 \cdot \log 1\right)\right)\right)}{i} \cdot n\right)\\ \mathbf{elif}\;i \le 1.494091961828663302085378658485801643661 \cdot 10^{142}:\\ \;\;\;\;\left(100 \cdot n\right) \cdot \frac{1}{\frac{i}{\left(\mathsf{fma}\left(\frac{1}{2}, {\left(\log i\right)}^{2} \cdot {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)\right) - \log n \cdot \mathsf{fma}\left(n \cdot n, \log i, n\right)\right) - \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)}}\\ \mathbf{elif}\;i \le 1.804564161742964970188249145928450773024 \cdot 10^{231}:\\ \;\;\;\;100 \cdot \left(\frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{i} \cdot n\right)\\ \mathbf{else}:\\ \;\;\;\;100 \cdot \frac{n}{\frac{i}{\mathsf{fma}\left(\frac{1}{2}, {\left(\log i\right)}^{2} \cdot {n}^{2}, \left(\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) - \log n \cdot \left(\log i \cdot {n}^{2} + n\right)\right) - \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)}}\\ \end{array}\]

Reproduce

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