Average Error: 47.6 → 10.7
Time: 11.3s
Precision: binary64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}} \]
\[\begin{array}{l} \mathbf{if}\;i \leq -3.182851271538301 \cdot 10^{-7}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}, -100\right)}{i}\\ \mathbf{elif}\;i \leq 9.097051035597713 \cdot 10^{-35}:\\ \;\;\;\;\mathsf{fma}\left(n, 100 + \mathsf{fma}\left(i, 50, \mathsf{fma}\left(i \cdot i, 16.666666666666668, 33.333333333333336 \cdot \left(\frac{i}{n} \cdot \frac{i}{n}\right)\right)\right), n \cdot \left(\left(\frac{i}{n} + \frac{i \cdot i}{n}\right) \cdot -50\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{100 \cdot \left(n \cdot \mathsf{expm1}\left(n \cdot \mathsf{fma}\left(-1, \log n, \log i\right)\right)\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 -3.182851271538301 \cdot 10^{-7}:\\
\;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}, -100\right)}{i}\\

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

\mathbf{else}:\\
\;\;\;\;\frac{100 \cdot \left(n \cdot \mathsf{expm1}\left(n \cdot \mathsf{fma}\left(-1, \log n, \log i\right)\right)\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 -3.182851271538301e-7)
   (* n (/ (fma 100.0 (exp (* n (log1p (/ i n)))) -100.0) i))
   (if (<= i 9.097051035597713e-35)
     (fma
      n
      (+
       100.0
       (fma
        i
        50.0
        (fma
         (* i i)
         16.666666666666668
         (* 33.333333333333336 (* (/ i n) (/ i n))))))
      (* n (* (+ (/ i n) (/ (* i i) n)) -50.0)))
     (/ (* 100.0 (* n (expm1 (* n (fma -1.0 (log n) (log i)))))) 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 <= -3.182851271538301e-7) {
		tmp = n * (fma(100.0, exp((n * log1p((i / n)))), -100.0) / i);
	} else if (i <= 9.097051035597713e-35) {
		tmp = fma(n, (100.0 + fma(i, 50.0, fma((i * i), 16.666666666666668, (33.333333333333336 * ((i / n) * (i / n)))))), (n * (((i / n) + ((i * i) / n)) * -50.0)));
	} else {
		tmp = (100.0 * (n * expm1((n * fma(-1.0, log(n), log(i)))))) / i;
	}
	return tmp;
}

Error

Bits error versus i

Bits error versus n

Target

Original47.6
Target47.7
Herbie10.7
\[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 < -3.1828512715383011e-7

    1. Initial program 27.8

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

      \[\leadsto \color{blue}{n \cdot \frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, -100\right)}{i}} \]
    3. Applied pow-to-exp_binary6428.4

      \[\leadsto n \cdot \frac{\mathsf{fma}\left(100, \color{blue}{e^{\log \left(1 + \frac{i}{n}\right) \cdot n}}, -100\right)}{i} \]
    4. Simplified7.2

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

    if -3.1828512715383011e-7 < i < 9.0970510355977131e-35

    1. Initial program 58.6

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

      \[\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 12.9

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

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

      \[\leadsto n \cdot \color{blue}{\left(\left(100 + \mathsf{fma}\left(50, i, \mathsf{fma}\left(16.666666666666668, i \cdot i, 33.333333333333336 \cdot \left(\frac{i}{n} \cdot \frac{i}{n}\right)\right)\right)\right) + \left(-50 \cdot \left(\frac{i}{n} + \frac{i \cdot i}{n}\right)\right)\right)} \]
    6. Applied distribute-rgt-in_binary648.3

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

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

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

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

    if 9.0970510355977131e-35 < i

    1. Initial program 36.3

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

      \[\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 inf 34.0

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

      \[\leadsto \color{blue}{\frac{100 \cdot \left(n \cdot \mathsf{expm1}\left(n \cdot \mathsf{fma}\left(-1, \log n, \log i\right)\right)\right)}{i}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification10.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \leq -3.182851271538301 \cdot 10^{-7}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}, -100\right)}{i}\\ \mathbf{elif}\;i \leq 9.097051035597713 \cdot 10^{-35}:\\ \;\;\;\;\mathsf{fma}\left(n, 100 + \mathsf{fma}\left(i, 50, \mathsf{fma}\left(i \cdot i, 16.666666666666668, 33.333333333333336 \cdot \left(\frac{i}{n} \cdot \frac{i}{n}\right)\right)\right), n \cdot \left(\left(\frac{i}{n} + \frac{i \cdot i}{n}\right) \cdot -50\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{100 \cdot \left(n \cdot \mathsf{expm1}\left(n \cdot \mathsf{fma}\left(-1, \log n, \log i\right)\right)\right)}{i}\\ \end{array} \]

Reproduce

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