Average Error: 47.2 → 10.3
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 -6.23013280765247 \cdot 10^{-16}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}, -100\right)}{i}\\ \mathbf{elif}\;i \leq 0.057649679874463414:\\ \;\;\;\;\mathsf{fma}\left(16.666666666666668, n \cdot \left(i \cdot i\right), \mathsf{fma}\left(100, n, \mathsf{fma}\left(33.333333333333336, i \cdot \frac{i}{n}, 50 \cdot \left(i \cdot n\right)\right)\right)\right) - 50 \cdot \left(i + i \cdot i\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 -6.23013280765247 \cdot 10^{-16}:\\
\;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}, -100\right)}{i}\\

\mathbf{elif}\;i \leq 0.057649679874463414:\\
\;\;\;\;\mathsf{fma}\left(16.666666666666668, n \cdot \left(i \cdot i\right), \mathsf{fma}\left(100, n, \mathsf{fma}\left(33.333333333333336, i \cdot \frac{i}{n}, 50 \cdot \left(i \cdot n\right)\right)\right)\right) - 50 \cdot \left(i + i \cdot i\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 -6.23013280765247e-16)
   (* n (/ (fma 100.0 (exp (* n (log1p (/ i n)))) -100.0) i))
   (if (<= i 0.057649679874463414)
     (-
      (fma
       16.666666666666668
       (* n (* i i))
       (fma 100.0 n (fma 33.333333333333336 (* i (/ i n)) (* 50.0 (* i n)))))
      (* 50.0 (+ i (* i i))))
     (/ (* 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 <= -6.23013280765247e-16) {
		tmp = n * (fma(100.0, exp(n * log1p(i / n)), -100.0) / i);
	} else if (i <= 0.057649679874463414) {
		tmp = fma(16.666666666666668, (n * (i * i)), fma(100.0, n, fma(33.333333333333336, (i * (i / n)), (50.0 * (i * n))))) - (50.0 * (i + (i * i)));
	} 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.2
Target47.5
Herbie10.3
\[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 < -6.2301328076524699e-16

    1. Initial program 29.0

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

      \[\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_binary6429.6

      \[\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 -6.2301328076524699e-16 < i < 0.057649679874463414

    1. Initial program 58.2

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

      \[\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.2

      \[\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.2

      \[\leadsto \color{blue}{\mathsf{fma}\left(16.666666666666668, \left(i \cdot i\right) \cdot n, \mathsf{fma}\left(100, n, \mathsf{fma}\left(33.333333333333336, \frac{i \cdot i}{n}, 50 \cdot \left(i \cdot n\right)\right)\right)\right) - 50 \cdot \left(i + i \cdot i\right)} \]
    5. Applied *-un-lft-identity_binary649.2

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

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

    if 0.057649679874463414 < i

    1. Initial program 30.9

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}} \]
    2. Simplified30.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 i around inf 30.1

      \[\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. Simplified21.4

      \[\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.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \leq -6.23013280765247 \cdot 10^{-16}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}, -100\right)}{i}\\ \mathbf{elif}\;i \leq 0.057649679874463414:\\ \;\;\;\;\mathsf{fma}\left(16.666666666666668, n \cdot \left(i \cdot i\right), \mathsf{fma}\left(100, n, \mathsf{fma}\left(33.333333333333336, i \cdot \frac{i}{n}, 50 \cdot \left(i \cdot n\right)\right)\right)\right) - 50 \cdot \left(i + i \cdot i\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 2022081 
(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))))