Average Error: 47.4 → 15.0
Time: 14.8s
Precision: binary64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
\[\begin{array}{l} \mathbf{if}\;i \leq -21149.344081563468:\\ \;\;\;\;100 \cdot \left(\frac{n}{i} \cdot \left(-1 + {\left(\frac{i}{n}\right)}^{n}\right)\right)\\ \mathbf{elif}\;i \leq 0.16917926315367307:\\ \;\;\;\;n \cdot \left(100 + i \cdot \left(50 + i \cdot 16.666666666666668\right)\right)\\ \mathbf{elif}\;i \leq 3.704250745838463 \cdot 10^{+223}:\\ \;\;\;\;n \cdot \left(100 \cdot \frac{-1 + {\left(\frac{i}{n} + 1\right)}^{n}}{i}\right)\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array}\]
100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\begin{array}{l}
\mathbf{if}\;i \leq -21149.344081563468:\\
\;\;\;\;100 \cdot \left(\frac{n}{i} \cdot \left(-1 + {\left(\frac{i}{n}\right)}^{n}\right)\right)\\

\mathbf{elif}\;i \leq 0.16917926315367307:\\
\;\;\;\;n \cdot \left(100 + i \cdot \left(50 + i \cdot 16.666666666666668\right)\right)\\

\mathbf{elif}\;i \leq 3.704250745838463 \cdot 10^{+223}:\\
\;\;\;\;n \cdot \left(100 \cdot \frac{-1 + {\left(\frac{i}{n} + 1\right)}^{n}}{i}\right)\\

\mathbf{else}:\\
\;\;\;\;0\\

\end{array}
(FPCore (i n)
 :precision binary64
 (* 100.0 (/ (- (pow (+ 1.0 (/ i n)) n) 1.0) (/ i n))))
(FPCore (i n)
 :precision binary64
 (if (<= i -21149.344081563468)
   (* 100.0 (* (/ n i) (+ -1.0 (pow (/ i n) n))))
   (if (<= i 0.16917926315367307)
     (* n (+ 100.0 (* i (+ 50.0 (* i 16.666666666666668)))))
     (if (<= i 3.704250745838463e+223)
       (* n (* 100.0 (/ (+ -1.0 (pow (+ (/ i n) 1.0) n)) i)))
       0.0))))
double code(double i, double n) {
	return 100.0 * ((pow((1.0 + (i / n)), n) - 1.0) / (i / n));
}
double code(double i, double n) {
	double tmp;
	if (i <= -21149.344081563468) {
		tmp = 100.0 * ((n / i) * (-1.0 + pow((i / n), n)));
	} else if (i <= 0.16917926315367307) {
		tmp = n * (100.0 + (i * (50.0 + (i * 16.666666666666668))));
	} else if (i <= 3.704250745838463e+223) {
		tmp = n * (100.0 * ((-1.0 + pow(((i / n) + 1.0), n)) / i));
	} else {
		tmp = 0.0;
	}
	return tmp;
}

Error

Bits error versus i

Bits error versus n

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original47.4
Target47.8
Herbie15.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 4 regimes
  2. if i < -21149.344081563468

    1. Initial program 27.0

      \[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{n \cdot \left(e^{\left(\log \left(\frac{1}{n}\right) - \log \left(\frac{1}{i}\right)\right) \cdot n} - 1\right)}{i}}\]
    3. Simplified18.8

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

    if -21149.344081563468 < i < 0.169179263153673071

    1. Initial program 57.9

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

      \[\leadsto 100 \cdot \frac{\color{blue}{i + \left(0.16666666666666666 \cdot {i}^{3} + 0.5 \cdot {i}^{2}\right)}}{\frac{i}{n}}\]
    3. Simplified26.2

      \[\leadsto 100 \cdot \frac{\color{blue}{i + \left(i \cdot i\right) \cdot \left(0.5 + 0.16666666666666666 \cdot i\right)}}{\frac{i}{n}}\]
    4. Taylor expanded around 0 9.8

      \[\leadsto \color{blue}{50 \cdot \left(i \cdot n\right) + \left(100 \cdot n + 16.666666666666668 \cdot \left({i}^{2} \cdot n\right)\right)}\]
    5. Simplified9.8

      \[\leadsto \color{blue}{n \cdot \left(100 + i \cdot \left(50 + 16.666666666666668 \cdot i\right)\right)}\]

    if 0.169179263153673071 < i < 3.70425074583846301e223

    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 associate-/r/_binary64_346533.2

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

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

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

    if 3.70425074583846301e223 < i

    1. Initial program 31.5

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

      \[\leadsto \color{blue}{0}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification15.0

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \leq -21149.344081563468:\\ \;\;\;\;100 \cdot \left(\frac{n}{i} \cdot \left(-1 + {\left(\frac{i}{n}\right)}^{n}\right)\right)\\ \mathbf{elif}\;i \leq 0.16917926315367307:\\ \;\;\;\;n \cdot \left(100 + i \cdot \left(50 + i \cdot 16.666666666666668\right)\right)\\ \mathbf{elif}\;i \leq 3.704250745838463 \cdot 10^{+223}:\\ \;\;\;\;n \cdot \left(100 \cdot \frac{-1 + {\left(\frac{i}{n} + 1\right)}^{n}}{i}\right)\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array}\]

Reproduce

herbie shell --seed 2020280 
(FPCore (i n)
  :name "Compound Interest"
  :precision binary64

  :herbie-target
  (* 100.0 (/ (- (exp (* n (if (== (+ 1.0 (/ i n)) 1.0) (/ i n) (/ (* (/ i n) (log (+ 1.0 (/ i n)))) (- (+ (/ i n) 1.0) 1.0))))) 1.0) (/ i n)))

  (* 100.0 (/ (- (pow (+ 1.0 (/ i n)) n) 1.0) (/ i n))))