Average Error: 47.6 → 10.4
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.349360149998456 \cdot 10^{-9}:\\ \;\;\;\;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.8560767434772578:\\ \;\;\;\;n \cdot \left(100 + \mathsf{fma}\left(i, 50, \left(i \cdot i\right) \cdot 16.666666666666668\right)\right)\\ \mathbf{elif}\;i \leq 2.3751934240026747 \cdot 10^{+240}:\\ \;\;\;\;n \cdot \frac{n \cdot \left(100 \cdot \log i - 100 \cdot \log n\right)}{i}\\ \mathbf{elif}\;i \leq 5.887413016264065 \cdot 10^{+274}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, {\left(\frac{\frac{-1}{n}}{\frac{-1}{i}}\right)}^{n}, -100\right)}{i}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(50, \frac{{n}^{3} \cdot {\log i}^{2}}{i}, \mathsf{fma}\left(100, \frac{\log i \cdot \left(n \cdot n\right)}{i}, \mathsf{fma}\left(100, \frac{{n}^{3}}{i \cdot i}, 50 \cdot \frac{{n}^{3} \cdot {\log n}^{2}}{i}\right)\right)\right) - 100 \cdot \left(\frac{{n}^{3} \cdot \left(\log i \cdot \log n\right)}{i} + \frac{\log n \cdot \left(n \cdot n\right)}{i}\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.349360149998456 \cdot 10^{-9}:\\
\;\;\;\;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.8560767434772578:\\
\;\;\;\;n \cdot \left(100 + \mathsf{fma}\left(i, 50, \left(i \cdot i\right) \cdot 16.666666666666668\right)\right)\\

\mathbf{elif}\;i \leq 2.3751934240026747 \cdot 10^{+240}:\\
\;\;\;\;n \cdot \frac{n \cdot \left(100 \cdot \log i - 100 \cdot \log n\right)}{i}\\

\mathbf{elif}\;i \leq 5.887413016264065 \cdot 10^{+274}:\\
\;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, {\left(\frac{\frac{-1}{n}}{\frac{-1}{i}}\right)}^{n}, -100\right)}{i}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(50, \frac{{n}^{3} \cdot {\log i}^{2}}{i}, \mathsf{fma}\left(100, \frac{\log i \cdot \left(n \cdot n\right)}{i}, \mathsf{fma}\left(100, \frac{{n}^{3}}{i \cdot i}, 50 \cdot \frac{{n}^{3} \cdot {\log n}^{2}}{i}\right)\right)\right) - 100 \cdot \left(\frac{{n}^{3} \cdot \left(\log i \cdot \log n\right)}{i} + \frac{\log n \cdot \left(n \cdot n\right)}{i}\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.349360149998456e-9)
   (* n (/ (fma 100.0 (exp (* n (log1p (/ i n)))) -100.0) i))
   (if (<= i 0.8560767434772578)
     (* n (+ 100.0 (fma i 50.0 (* (* i i) 16.666666666666668))))
     (if (<= i 2.3751934240026747e+240)
       (* n (/ (* n (- (* 100.0 (log i)) (* 100.0 (log n)))) i))
       (if (<= i 5.887413016264065e+274)
         (* n (/ (fma 100.0 (pow (/ (/ -1.0 n) (/ -1.0 i)) n) -100.0) i))
         (-
          (fma
           50.0
           (/ (* (pow n 3.0) (pow (log i) 2.0)) i)
           (fma
            100.0
            (/ (* (log i) (* n n)) i)
            (fma
             100.0
             (/ (pow n 3.0) (* i i))
             (* 50.0 (/ (* (pow n 3.0) (pow (log n) 2.0)) i)))))
          (*
           100.0
           (+
            (/ (* (pow n 3.0) (* (log i) (log n))) i)
            (/ (* (log n) (* n 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 <= -3.349360149998456e-9) {
		tmp = n * (fma(100.0, exp(n * log1p(i / n)), -100.0) / i);
	} else if (i <= 0.8560767434772578) {
		tmp = n * (100.0 + fma(i, 50.0, ((i * i) * 16.666666666666668)));
	} else if (i <= 2.3751934240026747e+240) {
		tmp = n * ((n * ((100.0 * log(i)) - (100.0 * log(n)))) / i);
	} else if (i <= 5.887413016264065e+274) {
		tmp = n * (fma(100.0, pow(((-1.0 / n) / (-1.0 / i)), n), -100.0) / i);
	} else {
		tmp = fma(50.0, ((pow(n, 3.0) * pow(log(i), 2.0)) / i), fma(100.0, ((log(i) * (n * n)) / i), fma(100.0, (pow(n, 3.0) / (i * i)), (50.0 * ((pow(n, 3.0) * pow(log(n), 2.0)) / i))))) - (100.0 * (((pow(n, 3.0) * (log(i) * log(n))) / i) + ((log(n) * (n * n)) / i)));
	}
	return tmp;
}

Error

Bits error versus i

Bits error versus n

Target

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

    1. Initial program 28.0

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

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

      \[\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.34936014999845581e-9 < i < 0.85607674347725782

    1. Initial program 58.3

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

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

      \[\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. Taylor expanded in n around inf 9.1

      \[\leadsto n \cdot \color{blue}{\left(100 + \left(50 \cdot i + 16.666666666666668 \cdot {i}^{2}\right)\right)} \]
    6. Simplified9.1

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

    if 0.85607674347725782 < i < 2.3751934240026747e240

    1. Initial program 33.2

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

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

    if 2.3751934240026747e240 < i < 5.8874130162640653e274

    1. Initial program 34.3

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

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

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

    if 5.8874130162640653e274 < i

    1. Initial program 29.1

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \leq -3.349360149998456 \cdot 10^{-9}:\\ \;\;\;\;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.8560767434772578:\\ \;\;\;\;n \cdot \left(100 + \mathsf{fma}\left(i, 50, \left(i \cdot i\right) \cdot 16.666666666666668\right)\right)\\ \mathbf{elif}\;i \leq 2.3751934240026747 \cdot 10^{+240}:\\ \;\;\;\;n \cdot \frac{n \cdot \left(100 \cdot \log i - 100 \cdot \log n\right)}{i}\\ \mathbf{elif}\;i \leq 5.887413016264065 \cdot 10^{+274}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, {\left(\frac{\frac{-1}{n}}{\frac{-1}{i}}\right)}^{n}, -100\right)}{i}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(50, \frac{{n}^{3} \cdot {\log i}^{2}}{i}, \mathsf{fma}\left(100, \frac{\log i \cdot \left(n \cdot n\right)}{i}, \mathsf{fma}\left(100, \frac{{n}^{3}}{i \cdot i}, 50 \cdot \frac{{n}^{3} \cdot {\log n}^{2}}{i}\right)\right)\right) - 100 \cdot \left(\frac{{n}^{3} \cdot \left(\log i \cdot \log n\right)}{i} + \frac{\log n \cdot \left(n \cdot n\right)}{i}\right)\\ \end{array} \]

Reproduce

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