Average Error: 46.4 → 12.2
Time: 7.0s
Precision: binary64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}} \]
\[\begin{array}{l} \mathbf{if}\;i \leq -3.81143365456763 \cdot 10^{+96}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, {\left(\frac{i}{n}\right)}^{n}, -100\right)}{i}\\ \mathbf{elif}\;i \leq -8.204085708724183 \cdot 10^{-11}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{i}, -100\right)}{i}\\ \mathbf{elif}\;i \leq 0.585668199925356:\\ \;\;\;\;n \cdot 100\\ \mathbf{else}:\\ \;\;\;\;\log \left({\left(e^{100}\right)}^{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.81143365456763 \cdot 10^{+96}:\\
\;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, {\left(\frac{i}{n}\right)}^{n}, -100\right)}{i}\\

\mathbf{elif}\;i \leq -8.204085708724183 \cdot 10^{-11}:\\
\;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{i}, -100\right)}{i}\\

\mathbf{elif}\;i \leq 0.585668199925356:\\
\;\;\;\;n \cdot 100\\

\mathbf{else}:\\
\;\;\;\;\log \left({\left(e^{100}\right)}^{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.81143365456763e+96)
   (* n (/ (fma 100.0 (pow (/ i n) n) -100.0) i))
   (if (<= i -8.204085708724183e-11)
     (* n (/ (fma 100.0 (exp i) -100.0) i))
     (if (<= i 0.585668199925356) (* n 100.0) (log (pow (exp 100.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.81143365456763e+96) {
		tmp = n * (fma(100.0, pow((i / n), n), -100.0) / i);
	} else if (i <= -8.204085708724183e-11) {
		tmp = n * (fma(100.0, exp(i), -100.0) / i);
	} else if (i <= 0.585668199925356) {
		tmp = n * 100.0;
	} else {
		tmp = log(pow(exp(100.0), n));
	}
	return tmp;
}

Error

Bits error versus i

Bits error versus n

Target

Original46.4
Target42.3
Herbie12.2
\[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 < -3.81143365456762995e96

    1. Initial program 19.6

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

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

    if -3.81143365456762995e96 < i < -8.20408570872418269e-11

    1. Initial program 44.1

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

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

    if -8.20408570872418269e-11 < i < 0.585668199925356014

    1. Initial program 58.4

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

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

      \[\leadsto n \cdot \frac{\color{blue}{100 \cdot i}}{i} \]
    4. Taylor expanded in i around 0 9.4

      \[\leadsto n \cdot \color{blue}{100} \]

    if 0.585668199925356014 < i

    1. Initial program 33.5

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

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

      \[\leadsto n \cdot \frac{\color{blue}{100 \cdot i}}{i} \]
    4. Applied div-inv_binary6460.3

      \[\leadsto n \cdot \color{blue}{\left(\left(100 \cdot i\right) \cdot \frac{1}{i}\right)} \]
    5. Applied add-log-exp_binary6416.0

      \[\leadsto \color{blue}{\log \left(e^{n \cdot \left(\left(100 \cdot i\right) \cdot \frac{1}{i}\right)}\right)} \]
    6. Simplified15.8

      \[\leadsto \log \color{blue}{\left({\left(e^{100}\right)}^{n}\right)} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification12.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \leq -3.81143365456763 \cdot 10^{+96}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, {\left(\frac{i}{n}\right)}^{n}, -100\right)}{i}\\ \mathbf{elif}\;i \leq -8.204085708724183 \cdot 10^{-11}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{i}, -100\right)}{i}\\ \mathbf{elif}\;i \leq 0.585668199925356:\\ \;\;\;\;n \cdot 100\\ \mathbf{else}:\\ \;\;\;\;\log \left({\left(e^{100}\right)}^{n}\right)\\ \end{array} \]

Reproduce

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