Average Error: 46.9 → 10.2
Time: 2.1m
Precision: 64
Internal Precision: 3200
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
\[\begin{array}{l} \mathbf{if}\;i \le -0.00012047330594640052:\\ \;\;\;\;\frac{100 \cdot n}{i} \cdot (e^{\log_* (1 + \frac{i}{n}) \cdot n} - 1)^*\\ \mathbf{if}\;i \le 1.0456180401234767:\\ \;\;\;\;(\frac{50}{3} \cdot \left(n \cdot \left(i \cdot i\right)\right) + \left(n \cdot (i \cdot 50 + 100)_*\right))_*\\ \mathbf{if}\;i \le 2.2741584621052686 \cdot 10^{+149}:\\ \;\;\;\;\frac{100 \cdot n}{i} \cdot (e^{\log_* (1 + \frac{i}{n}) \cdot n} - 1)^*\\ \mathbf{else}:\\ \;\;\;\;\frac{100 \cdot n}{i} \cdot (e^{\frac{\log n - \log i}{n}} - 1)^*\\ \end{array}\]

Error

Bits error versus i

Bits error versus n

Target

Original46.9
Target46.7
Herbie10.2
\[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 < -0.00012047330594640052

    1. Initial program 27.6

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Using strategy rm
    3. Applied add-exp-log27.6

      \[\leadsto 100 \cdot \frac{{\color{blue}{\left(e^{\log \left(1 + \frac{i}{n}\right)}\right)}}^{n} - 1}{\frac{i}{n}}\]
    4. Applied pow-exp27.6

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

      \[\leadsto 100 \cdot \frac{e^{\color{blue}{n \cdot \log_* (1 + \frac{i}{n})}} - 1}{\frac{i}{n}}\]

    if -0.00012047330594640052 < i < 1.0456180401234767

    1. Initial program 57.6

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

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

      \[\leadsto \color{blue}{\frac{(\left(i \cdot i\right) \cdot \left((i \cdot \frac{1}{6} + \frac{1}{2})_*\right) + i)_*}{\frac{\frac{i}{n}}{100}}}\]
    4. Taylor expanded around 0 9.2

      \[\leadsto \color{blue}{\frac{50}{3} \cdot \left(n \cdot {i}^{2}\right) + \left(100 \cdot n + 50 \cdot \left(n \cdot i\right)\right)}\]
    5. Applied simplify9.2

      \[\leadsto \color{blue}{(\frac{50}{3} \cdot \left(i \cdot \left(i \cdot n\right)\right) + \left(n \cdot (i \cdot 50 + 100)_*\right))_*}\]

    if 1.0456180401234767 < i < 2.2741584621052686e+149

    1. Initial program 28.8

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Using strategy rm
    3. Applied add-exp-log40.7

      \[\leadsto 100 \cdot \frac{{\color{blue}{\left(e^{\log \left(1 + \frac{i}{n}\right)}\right)}}^{n} - 1}{\frac{i}{n}}\]
    4. Applied pow-exp40.7

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

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

    if 2.2741584621052686e+149 < i

    1. Initial program 32.0

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

      \[\leadsto 100 \cdot \frac{\color{blue}{e^{\frac{\log n - \log i}{n}} - 1}}{\frac{i}{n}}\]
    3. Applied simplify35.1

      \[\leadsto \color{blue}{\left(100 \cdot \frac{n}{i}\right) \cdot (e^{\frac{\log n - \log i}{n}} - 1)^*}\]
  3. Recombined 4 regimes into one program.
  4. Removed slow pow expressions.
  5. Applied simplify10.2

    \[\leadsto \color{blue}{\begin{array}{l} \mathbf{if}\;i \le -0.00012047330594640052:\\ \;\;\;\;\frac{100 \cdot n}{i} \cdot (e^{\log_* (1 + \frac{i}{n}) \cdot n} - 1)^*\\ \mathbf{if}\;i \le 1.0456180401234767:\\ \;\;\;\;(\frac{50}{3} \cdot \left(n \cdot \left(i \cdot i\right)\right) + \left(n \cdot (i \cdot 50 + 100)_*\right))_*\\ \mathbf{if}\;i \le 2.2741584621052686 \cdot 10^{+149}:\\ \;\;\;\;\frac{100 \cdot n}{i} \cdot (e^{\log_* (1 + \frac{i}{n}) \cdot n} - 1)^*\\ \mathbf{else}:\\ \;\;\;\;\frac{100 \cdot n}{i} \cdot (e^{\frac{\log n - \log i}{n}} - 1)^*\\ \end{array}}\]

Runtime

Time bar (total: 2.1m)Debug log

herbie shell --seed '#(1889797285 268396849 4100589100 2067516092 3019009300 3748763710)' +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))))