Average Error: 47.1 → 10.7
Time: 10.9s
Precision: binary64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}} \]
\[\begin{array}{l} t_0 := {\log n}^{2}\\ t_1 := {\log i}^{2}\\ t_2 := \frac{{n}^{3}}{i}\\ \mathbf{if}\;i \leq -8.837972227835183 \cdot 10^{-14}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}, -100\right)}{i}\\ \mathbf{elif}\;i \leq 1.8157777999019126 \cdot 10^{-23}:\\ \;\;\;\;\mathsf{fma}\left(-50, \mathsf{fma}\left(i, i, i\right), \mathsf{fma}\left(n, \mathsf{fma}\left(i, 50, \mathsf{fma}\left(i, i \cdot 16.666666666666668, 100\right)\right), \frac{i}{\frac{\frac{n}{33.333333333333336}}{i}}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(50, \left(n \cdot n\right) \cdot t_1, \mathsf{fma}\left(16.666666666666668, {n}^{3} \cdot {\log i}^{3}, \mathsf{fma}\left(50, \left(n \cdot n\right) \cdot t_0, \mathsf{fma}\left(100, \frac{n \cdot n}{i}, \mathsf{fma}\left(100, n \cdot \log i, \mathsf{fma}\left(50, t_0 \cdot \left(\log i \cdot {n}^{3}\right), 100 \cdot \left(\log i \cdot t_2\right)\right)\right)\right)\right)\right)\right) - \mathsf{fma}\left(16.666666666666668, {n}^{3} \cdot {\log n}^{3}, \mathsf{fma}\left(50, {n}^{3} \cdot \left(t_1 \cdot \log n\right), \mathsf{fma}\left(100, \log n \cdot t_2, \mathsf{fma}\left(100, \left(n \cdot n\right) \cdot \left(\log i \cdot \log n\right), \mathsf{fma}\left(50, \frac{{n}^{3}}{i \cdot i}, \log n \cdot \left(n \cdot 100\right)\right)\right)\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
 (let* ((t_0 (pow (log n) 2.0))
        (t_1 (pow (log i) 2.0))
        (t_2 (/ (pow n 3.0) i)))
   (if (<= i -8.837972227835183e-14)
     (* n (/ (fma 100.0 (exp (* n (log1p (/ i n)))) -100.0) i))
     (if (<= i 1.8157777999019126e-23)
       (fma
        -50.0
        (fma i i i)
        (fma
         n
         (fma i 50.0 (fma i (* i 16.666666666666668) 100.0))
         (/ i (/ (/ n 33.333333333333336) i))))
       (*
        n
        (/
         (-
          (fma
           50.0
           (* (* n n) t_1)
           (fma
            16.666666666666668
            (* (pow n 3.0) (pow (log i) 3.0))
            (fma
             50.0
             (* (* n n) t_0)
             (fma
              100.0
              (/ (* n n) i)
              (fma
               100.0
               (* n (log i))
               (fma
                50.0
                (* t_0 (* (log i) (pow n 3.0)))
                (* 100.0 (* (log i) t_2))))))))
          (fma
           16.666666666666668
           (* (pow n 3.0) (pow (log n) 3.0))
           (fma
            50.0
            (* (pow n 3.0) (* t_1 (log n)))
            (fma
             100.0
             (* (log n) t_2)
             (fma
              100.0
              (* (* n n) (* (log i) (log n)))
              (fma 50.0 (/ (pow n 3.0) (* i i)) (* (log n) (* n 100.0))))))))
         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 t_0 = pow(log(n), 2.0);
	double t_1 = pow(log(i), 2.0);
	double t_2 = pow(n, 3.0) / i;
	double tmp;
	if (i <= -8.837972227835183e-14) {
		tmp = n * (fma(100.0, exp((n * log1p((i / n)))), -100.0) / i);
	} else if (i <= 1.8157777999019126e-23) {
		tmp = fma(-50.0, fma(i, i, i), fma(n, fma(i, 50.0, fma(i, (i * 16.666666666666668), 100.0)), (i / ((n / 33.333333333333336) / i))));
	} else {
		tmp = n * ((fma(50.0, ((n * n) * t_1), fma(16.666666666666668, (pow(n, 3.0) * pow(log(i), 3.0)), fma(50.0, ((n * n) * t_0), fma(100.0, ((n * n) / i), fma(100.0, (n * log(i)), fma(50.0, (t_0 * (log(i) * pow(n, 3.0))), (100.0 * (log(i) * t_2)))))))) - fma(16.666666666666668, (pow(n, 3.0) * pow(log(n), 3.0)), fma(50.0, (pow(n, 3.0) * (t_1 * log(n))), fma(100.0, (log(n) * t_2), fma(100.0, ((n * n) * (log(i) * log(n))), fma(50.0, (pow(n, 3.0) / (i * i)), (log(n) * (n * 100.0)))))))) / i);
	}
	return tmp;
}
function code(i, n)
	return Float64(100.0 * Float64(Float64((Float64(1.0 + Float64(i / n)) ^ n) - 1.0) / Float64(i / n)))
end
function code(i, n)
	t_0 = log(n) ^ 2.0
	t_1 = log(i) ^ 2.0
	t_2 = Float64((n ^ 3.0) / i)
	tmp = 0.0
	if (i <= -8.837972227835183e-14)
		tmp = Float64(n * Float64(fma(100.0, exp(Float64(n * log1p(Float64(i / n)))), -100.0) / i));
	elseif (i <= 1.8157777999019126e-23)
		tmp = fma(-50.0, fma(i, i, i), fma(n, fma(i, 50.0, fma(i, Float64(i * 16.666666666666668), 100.0)), Float64(i / Float64(Float64(n / 33.333333333333336) / i))));
	else
		tmp = Float64(n * Float64(Float64(fma(50.0, Float64(Float64(n * n) * t_1), fma(16.666666666666668, Float64((n ^ 3.0) * (log(i) ^ 3.0)), fma(50.0, Float64(Float64(n * n) * t_0), fma(100.0, Float64(Float64(n * n) / i), fma(100.0, Float64(n * log(i)), fma(50.0, Float64(t_0 * Float64(log(i) * (n ^ 3.0))), Float64(100.0 * Float64(log(i) * t_2)))))))) - fma(16.666666666666668, Float64((n ^ 3.0) * (log(n) ^ 3.0)), fma(50.0, Float64((n ^ 3.0) * Float64(t_1 * log(n))), fma(100.0, Float64(log(n) * t_2), fma(100.0, Float64(Float64(n * n) * Float64(log(i) * log(n))), fma(50.0, Float64((n ^ 3.0) / Float64(i * i)), Float64(log(n) * Float64(n * 100.0)))))))) / i));
	end
	return tmp
end
code[i_, n_] := N[(100.0 * N[(N[(N[Power[N[(1.0 + N[(i / n), $MachinePrecision]), $MachinePrecision], n], $MachinePrecision] - 1.0), $MachinePrecision] / N[(i / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[i_, n_] := Block[{t$95$0 = N[Power[N[Log[n], $MachinePrecision], 2.0], $MachinePrecision]}, Block[{t$95$1 = N[Power[N[Log[i], $MachinePrecision], 2.0], $MachinePrecision]}, Block[{t$95$2 = N[(N[Power[n, 3.0], $MachinePrecision] / i), $MachinePrecision]}, If[LessEqual[i, -8.837972227835183e-14], N[(n * N[(N[(100.0 * N[Exp[N[(n * N[Log[1 + N[(i / n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + -100.0), $MachinePrecision] / i), $MachinePrecision]), $MachinePrecision], If[LessEqual[i, 1.8157777999019126e-23], N[(-50.0 * N[(i * i + i), $MachinePrecision] + N[(n * N[(i * 50.0 + N[(i * N[(i * 16.666666666666668), $MachinePrecision] + 100.0), $MachinePrecision]), $MachinePrecision] + N[(i / N[(N[(n / 33.333333333333336), $MachinePrecision] / i), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(n * N[(N[(N[(50.0 * N[(N[(n * n), $MachinePrecision] * t$95$1), $MachinePrecision] + N[(16.666666666666668 * N[(N[Power[n, 3.0], $MachinePrecision] * N[Power[N[Log[i], $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision] + N[(50.0 * N[(N[(n * n), $MachinePrecision] * t$95$0), $MachinePrecision] + N[(100.0 * N[(N[(n * n), $MachinePrecision] / i), $MachinePrecision] + N[(100.0 * N[(n * N[Log[i], $MachinePrecision]), $MachinePrecision] + N[(50.0 * N[(t$95$0 * N[(N[Log[i], $MachinePrecision] * N[Power[n, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(100.0 * N[(N[Log[i], $MachinePrecision] * t$95$2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(16.666666666666668 * N[(N[Power[n, 3.0], $MachinePrecision] * N[Power[N[Log[n], $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision] + N[(50.0 * N[(N[Power[n, 3.0], $MachinePrecision] * N[(t$95$1 * N[Log[n], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(100.0 * N[(N[Log[n], $MachinePrecision] * t$95$2), $MachinePrecision] + N[(100.0 * N[(N[(n * n), $MachinePrecision] * N[(N[Log[i], $MachinePrecision] * N[Log[n], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(50.0 * N[(N[Power[n, 3.0], $MachinePrecision] / N[(i * i), $MachinePrecision]), $MachinePrecision] + N[(N[Log[n], $MachinePrecision] * N[(n * 100.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / i), $MachinePrecision]), $MachinePrecision]]]]]]
100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\begin{array}{l}
t_0 := {\log n}^{2}\\
t_1 := {\log i}^{2}\\
t_2 := \frac{{n}^{3}}{i}\\
\mathbf{if}\;i \leq -8.837972227835183 \cdot 10^{-14}:\\
\;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}, -100\right)}{i}\\

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

\mathbf{else}:\\
\;\;\;\;n \cdot \frac{\mathsf{fma}\left(50, \left(n \cdot n\right) \cdot t_1, \mathsf{fma}\left(16.666666666666668, {n}^{3} \cdot {\log i}^{3}, \mathsf{fma}\left(50, \left(n \cdot n\right) \cdot t_0, \mathsf{fma}\left(100, \frac{n \cdot n}{i}, \mathsf{fma}\left(100, n \cdot \log i, \mathsf{fma}\left(50, t_0 \cdot \left(\log i \cdot {n}^{3}\right), 100 \cdot \left(\log i \cdot t_2\right)\right)\right)\right)\right)\right)\right) - \mathsf{fma}\left(16.666666666666668, {n}^{3} \cdot {\log n}^{3}, \mathsf{fma}\left(50, {n}^{3} \cdot \left(t_1 \cdot \log n\right), \mathsf{fma}\left(100, \log n \cdot t_2, \mathsf{fma}\left(100, \left(n \cdot n\right) \cdot \left(\log i \cdot \log n\right), \mathsf{fma}\left(50, \frac{{n}^{3}}{i \cdot i}, \log n \cdot \left(n \cdot 100\right)\right)\right)\right)\right)\right)}{i}\\


\end{array}

Error

Bits error versus i

Bits error versus n

Target

Original47.1
Target47.3
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 < -8.83797222783518273e-14

    1. Initial program 27.4

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

      \[\leadsto \color{blue}{n \cdot \frac{\mathsf{fma}\left(100, {\left(1 + \frac{i}{n}\right)}^{n}, -100\right)}{i}} \]
    3. Applied egg-rr6.6

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

    if -8.83797222783518273e-14 < i < 1.8157777999019126e-23

    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 12.8

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

      \[\leadsto n \cdot \color{blue}{\left(100 + \left(\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) + -50 \cdot \left(\frac{i}{n} + \frac{i \cdot i}{n}\right)\right)\right)} \]
    5. Taylor expanded in n around 0 8.5

      \[\leadsto \color{blue}{\left(100 \cdot n + \left(16.666666666666668 \cdot \left(n \cdot {i}^{2}\right) + \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)} \]
    6. Simplified8.5

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

    if 1.8157777999019126e-23 < i

    1. Initial program 33.5

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \leq -8.837972227835183 \cdot 10^{-14}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(100, e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}, -100\right)}{i}\\ \mathbf{elif}\;i \leq 1.8157777999019126 \cdot 10^{-23}:\\ \;\;\;\;\mathsf{fma}\left(-50, \mathsf{fma}\left(i, i, i\right), \mathsf{fma}\left(n, \mathsf{fma}\left(i, 50, \mathsf{fma}\left(i, i \cdot 16.666666666666668, 100\right)\right), \frac{i}{\frac{\frac{n}{33.333333333333336}}{i}}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;n \cdot \frac{\mathsf{fma}\left(50, \left(n \cdot n\right) \cdot {\log i}^{2}, \mathsf{fma}\left(16.666666666666668, {n}^{3} \cdot {\log i}^{3}, \mathsf{fma}\left(50, \left(n \cdot n\right) \cdot {\log n}^{2}, \mathsf{fma}\left(100, \frac{n \cdot n}{i}, \mathsf{fma}\left(100, n \cdot \log i, \mathsf{fma}\left(50, {\log n}^{2} \cdot \left(\log i \cdot {n}^{3}\right), 100 \cdot \left(\log i \cdot \frac{{n}^{3}}{i}\right)\right)\right)\right)\right)\right)\right) - \mathsf{fma}\left(16.666666666666668, {n}^{3} \cdot {\log n}^{3}, \mathsf{fma}\left(50, {n}^{3} \cdot \left({\log i}^{2} \cdot \log n\right), \mathsf{fma}\left(100, \log n \cdot \frac{{n}^{3}}{i}, \mathsf{fma}\left(100, \left(n \cdot n\right) \cdot \left(\log i \cdot \log n\right), \mathsf{fma}\left(50, \frac{{n}^{3}}{i \cdot i}, \log n \cdot \left(n \cdot 100\right)\right)\right)\right)\right)\right)}{i}\\ \end{array} \]

Reproduce

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