Average Error: 42.1 → 8.6
Time: 4.0m
Precision: 64
Internal Precision: 128
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
\[\begin{array}{l} \mathbf{if}\;n \le -0.0028807950915930402:\\ \;\;\;\;100 \cdot \left(n \cdot \frac{(e^{i} - 1)^*}{i}\right)\\ \mathbf{elif}\;n \le -9.561193099745693 \cdot 10^{-89}:\\ \;\;\;\;n \cdot \left(100 \cdot \frac{{\left(\frac{i}{n} + 1\right)}^{n} - 1}{i}\right)\\ \mathbf{elif}\;n \le -8.242118670938044 \cdot 10^{-303}:\\ \;\;\;\;\frac{100 \cdot (e^{\log_* (1 + \frac{i}{n}) \cdot n} - 1)^*}{\frac{i}{n}}\\ \mathbf{elif}\;n \le 1.2567813770355597 \cdot 10^{-256}:\\ \;\;\;\;0\\ \mathbf{elif}\;n \le 58.259053346479476:\\ \;\;\;\;100 \cdot \frac{(e^{\log_* (1 + \frac{i}{n}) \cdot n} - 1)^*}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;100 \cdot \left(n \cdot \frac{(e^{i} - 1)^*}{i}\right)\\ \end{array}\]

Error

Bits error versus i

Bits error versus n

Target

Original42.1
Target41.5
Herbie8.6
\[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 n < -0.0028807950915930402 or 58.259053346479476 < n

    1. Initial program 50.4

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

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

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

      \[\leadsto 100 \cdot \frac{(e^{\color{blue}{n \cdot \log_* (1 + \frac{i}{n})}} - 1)^*}{\frac{i}{n}}\]
    6. Taylor expanded around -inf 44.1

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

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

    if -0.0028807950915930402 < n < -9.561193099745693e-89

    1. Initial program 20.6

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

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

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

    if -9.561193099745693e-89 < n < -8.242118670938044e-303

    1. Initial program 13.7

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

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

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

      \[\leadsto 100 \cdot \frac{(e^{\color{blue}{n \cdot \log_* (1 + \frac{i}{n})}} - 1)^*}{\frac{i}{n}}\]
    6. Using strategy rm
    7. Applied associate-*r/15.1

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

    if -8.242118670938044e-303 < n < 1.2567813770355597e-256

    1. Initial program 33.2

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

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

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

      \[\leadsto 100 \cdot \frac{(e^{\color{blue}{n \cdot \log_* (1 + \frac{i}{n})}} - 1)^*}{\frac{i}{n}}\]
    6. Using strategy rm
    7. Applied *-commutative25.7

      \[\leadsto \color{blue}{\frac{(e^{n \cdot \log_* (1 + \frac{i}{n})} - 1)^*}{\frac{i}{n}} \cdot 100}\]
    8. Taylor expanded around 0 7.6

      \[\leadsto \color{blue}{0}\]

    if 1.2567813770355597e-256 < n < 58.259053346479476

    1. Initial program 49.9

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

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

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

      \[\leadsto 100 \cdot \frac{(e^{\color{blue}{n \cdot \log_* (1 + \frac{i}{n})}} - 1)^*}{\frac{i}{n}}\]
    6. Using strategy rm
    7. Applied *-commutative9.4

      \[\leadsto \color{blue}{\frac{(e^{n \cdot \log_* (1 + \frac{i}{n})} - 1)^*}{\frac{i}{n}} \cdot 100}\]
    8. Using strategy rm
    9. Applied *-commutative9.4

      \[\leadsto \color{blue}{100 \cdot \frac{(e^{n \cdot \log_* (1 + \frac{i}{n})} - 1)^*}{\frac{i}{n}}}\]
  3. Recombined 5 regimes into one program.
  4. Final simplification8.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \le -0.0028807950915930402:\\ \;\;\;\;100 \cdot \left(n \cdot \frac{(e^{i} - 1)^*}{i}\right)\\ \mathbf{elif}\;n \le -9.561193099745693 \cdot 10^{-89}:\\ \;\;\;\;n \cdot \left(100 \cdot \frac{{\left(\frac{i}{n} + 1\right)}^{n} - 1}{i}\right)\\ \mathbf{elif}\;n \le -8.242118670938044 \cdot 10^{-303}:\\ \;\;\;\;\frac{100 \cdot (e^{\log_* (1 + \frac{i}{n}) \cdot n} - 1)^*}{\frac{i}{n}}\\ \mathbf{elif}\;n \le 1.2567813770355597 \cdot 10^{-256}:\\ \;\;\;\;0\\ \mathbf{elif}\;n \le 58.259053346479476:\\ \;\;\;\;100 \cdot \frac{(e^{\log_* (1 + \frac{i}{n}) \cdot n} - 1)^*}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;100 \cdot \left(n \cdot \frac{(e^{i} - 1)^*}{i}\right)\\ \end{array}\]

Reproduce

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