Average Error: 47.3 → 18.1
Time: 3.5m
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 -2.2966207429113903 \cdot 10^{-07}:\\ \;\;\;\;100 \cdot \frac{\log \left(e^{{\left(1 + \frac{i}{n}\right)}^{n} - 1}\right)}{\frac{i}{n}}\\ \mathbf{if}\;i \le 1.2022812913263827 \cdot 10^{+48}:\\ \;\;\;\;1 \cdot \frac{\left(n \cdot 100\right) \cdot \left(1 + \frac{1}{2} \cdot i\right)}{1}\\ \mathbf{else}:\\ \;\;\;\;100 \cdot \frac{e^{\frac{\log n - \log i}{n}} - 1}{\frac{i}{n}}\\ \end{array}\]

Error

Bits error versus i

Bits error versus n

Target

Original47.3
Target46.9
Herbie18.1
\[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 i < -2.2966207429113903e-07

    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-log-exp28.8

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

    if -2.2966207429113903e-07 < i < 1.2022812913263827e+48

    1. Initial program 56.7

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

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

      \[\leadsto \color{blue}{\frac{i + i \cdot \left(i \cdot \frac{1}{2}\right)}{\frac{\frac{i}{n}}{100}}}\]
    4. Using strategy rm
    5. Applied *-un-lft-identity27.5

      \[\leadsto \frac{i + i \cdot \left(i \cdot \frac{1}{2}\right)}{\color{blue}{1 \cdot \frac{\frac{i}{n}}{100}}}\]
    6. Applied *-un-lft-identity27.5

      \[\leadsto \frac{\color{blue}{1 \cdot \left(i + i \cdot \left(i \cdot \frac{1}{2}\right)\right)}}{1 \cdot \frac{\frac{i}{n}}{100}}\]
    7. Applied times-frac27.5

      \[\leadsto \color{blue}{\frac{1}{1} \cdot \frac{i + i \cdot \left(i \cdot \frac{1}{2}\right)}{\frac{\frac{i}{n}}{100}}}\]
    8. Applied simplify27.5

      \[\leadsto \color{blue}{1} \cdot \frac{i + i \cdot \left(i \cdot \frac{1}{2}\right)}{\frac{\frac{i}{n}}{100}}\]
    9. Applied simplify10.8

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

    if 1.2022812913263827e+48 < i

    1. Initial program 31.5

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

      \[\leadsto 100 \cdot \frac{\color{blue}{e^{\frac{\log n - \log i}{n}} - 1}}{\frac{i}{n}}\]
  3. Recombined 3 regimes into one program.
  4. Removed slow pow expressions.

Runtime

Time bar (total: 3.5m)Debug logProfile

herbie shell --seed '#(1063313015 2771194459 1594909340 1344785158 2223560818 546365448)' 
(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))))