Average Error: 47.4 → 10.5
Time: 17.4s
Precision: binary64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}} \]
\[\begin{array}{l} \mathbf{if}\;i \leq -3.9261148805468635:\\ \;\;\;\;100 \cdot \frac{e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)} - 1}{\frac{i}{n}}\\ \mathbf{elif}\;i \leq 339.51119695591177:\\ \;\;\;\;\frac{100 \cdot n}{\frac{i}{\mathsf{expm1}\left(i\right)}}\\ \mathbf{elif}\;i \leq 6.869232008206506 \cdot 10^{+129}:\\ \;\;\;\;100 \cdot \frac{\mathsf{fma}\left(0.5, \left(n \cdot n\right) \cdot {\log n}^{2}, \mathsf{fma}\left(n, \log i, \mathsf{fma}\left(0.5, \left(n \cdot n\right) \cdot {\log i}^{2}, \frac{n \cdot n}{i}\right)\right)\right) - \mathsf{fma}\left(n, \log n, \left(n \cdot n\right) \cdot \left(\log n \cdot \log i\right)\right)}{\frac{i}{n}}\\ \mathbf{elif}\;i \leq 5.028729354167085 \cdot 10^{+265}:\\ \;\;\;\;100 \cdot \left(n \cdot \frac{{\left(\frac{i}{n} + 1\right)}^{n} - 1}{i}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{-100}{\mathsf{fma}\left(0.5, \frac{i}{n}, \frac{-1}{n}\right)}\\ \end{array} \]
100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\begin{array}{l}
\mathbf{if}\;i \leq -3.9261148805468635:\\
\;\;\;\;100 \cdot \frac{e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)} - 1}{\frac{i}{n}}\\

\mathbf{elif}\;i \leq 339.51119695591177:\\
\;\;\;\;\frac{100 \cdot n}{\frac{i}{\mathsf{expm1}\left(i\right)}}\\

\mathbf{elif}\;i \leq 6.869232008206506 \cdot 10^{+129}:\\
\;\;\;\;100 \cdot \frac{\mathsf{fma}\left(0.5, \left(n \cdot n\right) \cdot {\log n}^{2}, \mathsf{fma}\left(n, \log i, \mathsf{fma}\left(0.5, \left(n \cdot n\right) \cdot {\log i}^{2}, \frac{n \cdot n}{i}\right)\right)\right) - \mathsf{fma}\left(n, \log n, \left(n \cdot n\right) \cdot \left(\log n \cdot \log i\right)\right)}{\frac{i}{n}}\\

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

\mathbf{else}:\\
\;\;\;\;\frac{-100}{\mathsf{fma}\left(0.5, \frac{i}{n}, \frac{-1}{n}\right)}\\


\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.9261148805468635)
   (* 100.0 (/ (- (exp (* n (log1p (/ i n)))) 1.0) (/ i n)))
   (if (<= i 339.51119695591177)
     (/ (* 100.0 n) (/ i (expm1 i)))
     (if (<= i 6.869232008206506e+129)
       (*
        100.0
        (/
         (-
          (fma
           0.5
           (* (* n n) (pow (log n) 2.0))
           (fma
            n
            (log i)
            (fma 0.5 (* (* n n) (pow (log i) 2.0)) (/ (* n n) i))))
          (fma n (log n) (* (* n n) (* (log n) (log i)))))
         (/ i n)))
       (if (<= i 5.028729354167085e+265)
         (* 100.0 (* n (/ (- (pow (+ (/ i n) 1.0) n) 1.0) i)))
         (/ -100.0 (fma 0.5 (/ i n) (/ -1.0 n))))))))
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.9261148805468635) {
		tmp = 100.0 * ((exp(n * log1p(i / n)) - 1.0) / (i / n));
	} else if (i <= 339.51119695591177) {
		tmp = (100.0 * n) / (i / expm1(i));
	} else if (i <= 6.869232008206506e+129) {
		tmp = 100.0 * ((fma(0.5, ((n * n) * pow(log(n), 2.0)), fma(n, log(i), fma(0.5, ((n * n) * pow(log(i), 2.0)), ((n * n) / i)))) - fma(n, log(n), ((n * n) * (log(n) * log(i))))) / (i / n));
	} else if (i <= 5.028729354167085e+265) {
		tmp = 100.0 * (n * ((pow(((i / n) + 1.0), n) - 1.0) / i));
	} else {
		tmp = -100.0 / fma(0.5, (i / n), (-1.0 / n));
	}
	return tmp;
}

Error

Bits error versus i

Bits error versus n

Target

Original47.4
Target46.8
Herbie10.5
\[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 5 regimes
  2. if i < -3.92611488054686353

    1. Initial program 27.8

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

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

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

    if -3.92611488054686353 < i < 339.511196955911771

    1. Initial program 58.0

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

      \[\leadsto 100 \cdot \color{blue}{\frac{n \cdot \left(e^{i} - 1\right)}{i}} \]
    3. Simplified14.8

      \[\leadsto 100 \cdot \color{blue}{\frac{n \cdot \mathsf{expm1}\left(i\right)}{i}} \]
    4. Taylor expanded in i around -inf 56.7

      \[\leadsto \color{blue}{-1 \cdot \frac{100 \cdot n - 100 \cdot \left(n \cdot e^{i}\right)}{i}} \]
    5. Simplified15.0

      \[\leadsto \color{blue}{\frac{-100}{\frac{i}{-n \cdot \mathsf{expm1}\left(i\right)}}} \]
    6. Applied distribute-lft-neg-in_binary6415.0

      \[\leadsto \frac{-100}{\frac{i}{\color{blue}{\left(-n\right) \cdot \mathsf{expm1}\left(i\right)}}} \]
    7. Applied *-un-lft-identity_binary6415.0

      \[\leadsto \frac{-100}{\frac{\color{blue}{1 \cdot i}}{\left(-n\right) \cdot \mathsf{expm1}\left(i\right)}} \]
    8. Applied times-frac_binary649.7

      \[\leadsto \frac{-100}{\color{blue}{\frac{1}{-n} \cdot \frac{i}{\mathsf{expm1}\left(i\right)}}} \]
    9. Applied associate-/r*_binary649.7

      \[\leadsto \color{blue}{\frac{\frac{-100}{\frac{1}{-n}}}{\frac{i}{\mathsf{expm1}\left(i\right)}}} \]
    10. Simplified9.6

      \[\leadsto \frac{\color{blue}{n \cdot 100}}{\frac{i}{\mathsf{expm1}\left(i\right)}} \]

    if 339.511196955911771 < i < 6.8692320082065063e129

    1. Initial program 27.3

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

      \[\leadsto 100 \cdot \frac{\color{blue}{\left(0.5 \cdot \left({n}^{2} \cdot {\log n}^{2}\right) + \left(\frac{{n}^{2}}{i} + \left(0.5 \cdot \left({\log i}^{2} \cdot {n}^{2}\right) + n \cdot \log i\right)\right)\right) - \left(\log i \cdot \left({n}^{2} \cdot \log n\right) + n \cdot \log n\right)}}{\frac{i}{n}} \]
    3. Simplified9.4

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

    if 6.8692320082065063e129 < i < 5.02872935416708484e265

    1. Initial program 33.1

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}} \]
    2. Applied associate-/r/_binary6433.1

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

    if 5.02872935416708484e265 < i

    1. Initial program 33.6

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

      \[\leadsto 100 \cdot \color{blue}{\frac{n \cdot \left(e^{i} - 1\right)}{i}} \]
    3. Simplified64.0

      \[\leadsto 100 \cdot \color{blue}{\frac{n \cdot \mathsf{expm1}\left(i\right)}{i}} \]
    4. Taylor expanded in i around -inf 64.0

      \[\leadsto \color{blue}{-1 \cdot \frac{100 \cdot n - 100 \cdot \left(n \cdot e^{i}\right)}{i}} \]
    5. Simplified64.0

      \[\leadsto \color{blue}{\frac{-100}{\frac{i}{-n \cdot \mathsf{expm1}\left(i\right)}}} \]
    6. Taylor expanded in i around 0 25.5

      \[\leadsto \frac{-100}{\color{blue}{0.5 \cdot \frac{i}{n} - \frac{1}{n}}} \]
    7. Simplified25.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \leq -3.9261148805468635:\\ \;\;\;\;100 \cdot \frac{e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)} - 1}{\frac{i}{n}}\\ \mathbf{elif}\;i \leq 339.51119695591177:\\ \;\;\;\;\frac{100 \cdot n}{\frac{i}{\mathsf{expm1}\left(i\right)}}\\ \mathbf{elif}\;i \leq 6.869232008206506 \cdot 10^{+129}:\\ \;\;\;\;100 \cdot \frac{\mathsf{fma}\left(0.5, \left(n \cdot n\right) \cdot {\log n}^{2}, \mathsf{fma}\left(n, \log i, \mathsf{fma}\left(0.5, \left(n \cdot n\right) \cdot {\log i}^{2}, \frac{n \cdot n}{i}\right)\right)\right) - \mathsf{fma}\left(n, \log n, \left(n \cdot n\right) \cdot \left(\log n \cdot \log i\right)\right)}{\frac{i}{n}}\\ \mathbf{elif}\;i \leq 5.028729354167085 \cdot 10^{+265}:\\ \;\;\;\;100 \cdot \left(n \cdot \frac{{\left(\frac{i}{n} + 1\right)}^{n} - 1}{i}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{-100}{\mathsf{fma}\left(0.5, \frac{i}{n}, \frac{-1}{n}\right)}\\ \end{array} \]

Reproduce

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