Average Error: 47.3 → 11.9
Time: 8.9s
Precision: binary64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}} \]
\[\begin{array}{l} \mathbf{if}\;i \leq -0.0001322261859959142:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{i}, -100\right)}{i}\\ \mathbf{elif}\;i \leq 8.722702919778021 \cdot 10^{-6}:\\ \;\;\;\;\mathsf{fma}\left(16.666666666666668, n \cdot \left(i \cdot i\right), \mathsf{fma}\left(n, 100, \mathsf{fma}\left(\frac{i \cdot i}{n}, 33.333333333333336, 50 \cdot \left(i \cdot n\right)\right)\right)\right) - 50 \cdot \left(i + i \cdot i\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{{n}^{2} \cdot \left(100 \cdot \log i - 100 \cdot \log n\right)}{i}\\ \end{array} \]
100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\begin{array}{l}
\mathbf{if}\;i \leq -0.0001322261859959142:\\
\;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{i}, -100\right)}{i}\\

\mathbf{elif}\;i \leq 8.722702919778021 \cdot 10^{-6}:\\
\;\;\;\;\mathsf{fma}\left(16.666666666666668, n \cdot \left(i \cdot i\right), \mathsf{fma}\left(n, 100, \mathsf{fma}\left(\frac{i \cdot i}{n}, 33.333333333333336, 50 \cdot \left(i \cdot n\right)\right)\right)\right) - 50 \cdot \left(i + i \cdot i\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{{n}^{2} \cdot \left(100 \cdot \log i - 100 \cdot \log n\right)}{i}\\


\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 -0.0001322261859959142)
   (* n (/ (fma 100.0 (exp i) -100.0) i))
   (if (<= i 8.722702919778021e-6)
     (-
      (fma
       16.666666666666668
       (* n (* i i))
       (fma n 100.0 (fma (/ (* i i) n) 33.333333333333336 (* 50.0 (* i n)))))
      (* 50.0 (+ i (* i i))))
     (/ (* (pow n 2.0) (- (* 100.0 (log i)) (* 100.0 (log n)))) i))))
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 <= -0.0001322261859959142) {
		tmp = n * (fma(100.0, exp(i), -100.0) / i);
	} else if (i <= 8.722702919778021e-6) {
		tmp = fma(16.666666666666668, (n * (i * i)), fma(n, 100.0, fma(((i * i) / n), 33.333333333333336, (50.0 * (i * n))))) - (50.0 * (i + (i * i)));
	} else {
		tmp = (pow(n, 2.0) * ((100.0 * log(i)) - (100.0 * log(n)))) / i;
	}
	return tmp;
}

Error

Bits error versus i

Bits error versus n

Target

Original47.3
Target47.7
Herbie11.9
\[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 < -1.3222618599591419e-4

    1. Initial program 28.3

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}} \]
    2. Simplified28.9

      \[\leadsto \color{blue}{n \cdot \frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, -100\right)}{i}} \]
    3. Taylor expanded in n around inf 13.0

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

    if -1.3222618599591419e-4 < i < 8.7227029197780214e-6

    1. Initial program 57.9

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

      \[\leadsto \color{blue}{n \cdot \frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, -100\right)}{i}} \]
    3. Taylor expanded in i around 0 9.0

      \[\leadsto \color{blue}{\left(16.666666666666668 \cdot \left(n \cdot {i}^{2}\right) + \left(100 \cdot n + \left(33.333333333333336 \cdot \frac{{i}^{2}}{n} + 50 \cdot \left(n \cdot i\right)\right)\right)\right) - \left(50 \cdot i + 50 \cdot {i}^{2}\right)} \]
    4. Simplified9.0

      \[\leadsto \color{blue}{\mathsf{fma}\left(16.666666666666668, \left(i \cdot i\right) \cdot n, \mathsf{fma}\left(n, 100, \mathsf{fma}\left(\frac{i \cdot i}{n}, 33.333333333333336, 50 \cdot \left(i \cdot n\right)\right)\right)\right) - 50 \cdot \left(i + i \cdot i\right)} \]

    if 8.7227029197780214e-6 < i

    1. Initial program 32.7

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}} \]
    2. Simplified32.7

      \[\leadsto \color{blue}{n \cdot \frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, -100\right)}{i}} \]
    3. Taylor expanded in n around 0 22.6

      \[\leadsto \color{blue}{\frac{{n}^{2} \cdot \left(100 \cdot \log i - 100 \cdot \log n\right)}{i}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification11.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \leq -0.0001322261859959142:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{i}, -100\right)}{i}\\ \mathbf{elif}\;i \leq 8.722702919778021 \cdot 10^{-6}:\\ \;\;\;\;\mathsf{fma}\left(16.666666666666668, n \cdot \left(i \cdot i\right), \mathsf{fma}\left(n, 100, \mathsf{fma}\left(\frac{i \cdot i}{n}, 33.333333333333336, 50 \cdot \left(i \cdot n\right)\right)\right)\right) - 50 \cdot \left(i + i \cdot i\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{{n}^{2} \cdot \left(100 \cdot \log i - 100 \cdot \log n\right)}{i}\\ \end{array} \]

Reproduce

herbie shell --seed 2021313 
(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))))