Average Error: 48.2 → 11.5
Time: 9.6s
Precision: binary64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}} \]
\[\begin{array}{l} \mathbf{if}\;i \leq -0.0013849874952766889:\\ \;\;\;\;100 \cdot \left(\frac{n}{i} \cdot e^{i} - \frac{n}{i}\right) + -50 \cdot \left(i \cdot e^{i}\right)\\ \mathbf{elif}\;i \leq 88.6904340203227:\\ \;\;\;\;\left(i \cdot \left(i \cdot \mathsf{fma}\left(n, 16.666666666666668, \frac{33.333333333333336}{n}\right)\right) + {i}^{3} \cdot \mathsf{fma}\left(n, 4.166666666666667, \frac{45.833333333333336}{n}\right)\right) + \left(n \cdot \mathsf{fma}\left(i, 50, 100\right) + \mathsf{fma}\left(-50, \mathsf{fma}\left(i, i, i\right), {i}^{3} \cdot -25\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{n}{\frac{i}{n}} \cdot \left(100 \cdot \left(\log i - \log n\right)\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 -0.0013849874952766889)
   (+ (* 100.0 (- (* (/ n i) (exp i)) (/ n i))) (* -50.0 (* i (exp i))))
   (if (<= i 88.6904340203227)
     (+
      (+
       (* i (* i (fma n 16.666666666666668 (/ 33.333333333333336 n))))
       (* (pow i 3.0) (fma n 4.166666666666667 (/ 45.833333333333336 n))))
      (+
       (* n (fma i 50.0 100.0))
       (fma -50.0 (fma i i i) (* (pow i 3.0) -25.0))))
     (* (/ n (/ i n)) (* 100.0 (- (log i) (log 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 <= -0.0013849874952766889) {
		tmp = (100.0 * (((n / i) * exp(i)) - (n / i))) + (-50.0 * (i * exp(i)));
	} else if (i <= 88.6904340203227) {
		tmp = ((i * (i * fma(n, 16.666666666666668, (33.333333333333336 / n)))) + (pow(i, 3.0) * fma(n, 4.166666666666667, (45.833333333333336 / n)))) + ((n * fma(i, 50.0, 100.0)) + fma(-50.0, fma(i, i, i), (pow(i, 3.0) * -25.0)));
	} else {
		tmp = (n / (i / n)) * (100.0 * (log(i) - log(n)));
	}
	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)
	tmp = 0.0
	if (i <= -0.0013849874952766889)
		tmp = Float64(Float64(100.0 * Float64(Float64(Float64(n / i) * exp(i)) - Float64(n / i))) + Float64(-50.0 * Float64(i * exp(i))));
	elseif (i <= 88.6904340203227)
		tmp = Float64(Float64(Float64(i * Float64(i * fma(n, 16.666666666666668, Float64(33.333333333333336 / n)))) + Float64((i ^ 3.0) * fma(n, 4.166666666666667, Float64(45.833333333333336 / n)))) + Float64(Float64(n * fma(i, 50.0, 100.0)) + fma(-50.0, fma(i, i, i), Float64((i ^ 3.0) * -25.0))));
	else
		tmp = Float64(Float64(n / Float64(i / n)) * Float64(100.0 * Float64(log(i) - log(n))));
	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_] := If[LessEqual[i, -0.0013849874952766889], N[(N[(100.0 * N[(N[(N[(n / i), $MachinePrecision] * N[Exp[i], $MachinePrecision]), $MachinePrecision] - N[(n / i), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-50.0 * N[(i * N[Exp[i], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[i, 88.6904340203227], N[(N[(N[(i * N[(i * N[(n * 16.666666666666668 + N[(33.333333333333336 / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Power[i, 3.0], $MachinePrecision] * N[(n * 4.166666666666667 + N[(45.833333333333336 / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(n * N[(i * 50.0 + 100.0), $MachinePrecision]), $MachinePrecision] + N[(-50.0 * N[(i * i + i), $MachinePrecision] + N[(N[Power[i, 3.0], $MachinePrecision] * -25.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(n / N[(i / n), $MachinePrecision]), $MachinePrecision] * N[(100.0 * N[(N[Log[i], $MachinePrecision] - N[Log[n], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\begin{array}{l}
\mathbf{if}\;i \leq -0.0013849874952766889:\\
\;\;\;\;100 \cdot \left(\frac{n}{i} \cdot e^{i} - \frac{n}{i}\right) + -50 \cdot \left(i \cdot e^{i}\right)\\

\mathbf{elif}\;i \leq 88.6904340203227:\\
\;\;\;\;\left(i \cdot \left(i \cdot \mathsf{fma}\left(n, 16.666666666666668, \frac{33.333333333333336}{n}\right)\right) + {i}^{3} \cdot \mathsf{fma}\left(n, 4.166666666666667, \frac{45.833333333333336}{n}\right)\right) + \left(n \cdot \mathsf{fma}\left(i, 50, 100\right) + \mathsf{fma}\left(-50, \mathsf{fma}\left(i, i, i\right), {i}^{3} \cdot -25\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{n}{\frac{i}{n}} \cdot \left(100 \cdot \left(\log i - \log n\right)\right)\\


\end{array}

Error

Bits error versus i

Bits error versus n

Target

Original48.2
Target47.7
Herbie11.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 3 regimes
  2. if i < -0.0013849874952766889

    1. Initial program 29.3

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

      \[\leadsto \color{blue}{100 \cdot \frac{n \cdot e^{i}}{i} - \left(100 \cdot \frac{n}{i} + 50 \cdot \left(e^{i} \cdot i\right)\right)} \]
    4. Simplified11.8

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

    if -0.0013849874952766889 < i < 88.690434020322698

    1. Initial program 58.1

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

      \[\leadsto \color{blue}{\left(100 \cdot n + \left(45.833333333333336 \cdot \frac{{i}^{3}}{n} + \left(50 \cdot \left(n \cdot i\right) + \left(4.166666666666667 \cdot \left(n \cdot {i}^{3}\right) + \left(16.666666666666668 \cdot \left(n \cdot {i}^{2}\right) + 33.333333333333336 \cdot \frac{{i}^{2}}{n}\right)\right)\right)\right)\right) - \left(25 \cdot {i}^{3} + \left(50 \cdot i + \left(50 \cdot {i}^{2} + 25 \cdot \frac{{i}^{3}}{{n}^{2}}\right)\right)\right)} \]
    4. Simplified9.3

      \[\leadsto \color{blue}{\mathsf{fma}\left(n, 100, \mathsf{fma}\left(\frac{{i}^{3}}{n}, 45.833333333333336, \mathsf{fma}\left(50, i \cdot n, \mathsf{fma}\left(4.166666666666667, n \cdot {i}^{3}, \mathsf{fma}\left(\frac{i}{\frac{n}{i}}, 33.333333333333336, 16.666666666666668 \cdot \left(\left(i \cdot i\right) \cdot n\right)\right)\right)\right)\right)\right) - \mathsf{fma}\left({i}^{3}, 25, \mathsf{fma}\left(50, i + i \cdot i, \frac{{i}^{3}}{n} \cdot \frac{25}{n}\right)\right)} \]
    5. Taylor expanded in n around inf 9.3

      \[\leadsto \color{blue}{\left(100 \cdot n + \left(45.833333333333336 \cdot \frac{{i}^{3}}{n} + \left(50 \cdot \left(n \cdot i\right) + \left(4.166666666666667 \cdot \left(n \cdot {i}^{3}\right) + \left(16.666666666666668 \cdot \left(n \cdot {i}^{2}\right) + 33.333333333333336 \cdot \frac{{i}^{2}}{n}\right)\right)\right)\right)\right) - \left(25 \cdot {i}^{3} + \left(50 \cdot i + 50 \cdot {i}^{2}\right)\right)} \]
    6. Simplified9.2

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

    if 88.690434020322698 < i

    1. Initial program 32.7

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

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

      \[\leadsto \color{blue}{\frac{n}{\frac{i}{n}} \cdot \left(100 \cdot \left(\log i - \log n\right)\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification11.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;i \leq -0.0013849874952766889:\\ \;\;\;\;100 \cdot \left(\frac{n}{i} \cdot e^{i} - \frac{n}{i}\right) + -50 \cdot \left(i \cdot e^{i}\right)\\ \mathbf{elif}\;i \leq 88.6904340203227:\\ \;\;\;\;\left(i \cdot \left(i \cdot \mathsf{fma}\left(n, 16.666666666666668, \frac{33.333333333333336}{n}\right)\right) + {i}^{3} \cdot \mathsf{fma}\left(n, 4.166666666666667, \frac{45.833333333333336}{n}\right)\right) + \left(n \cdot \mathsf{fma}\left(i, 50, 100\right) + \mathsf{fma}\left(-50, \mathsf{fma}\left(i, i, i\right), {i}^{3} \cdot -25\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{n}{\frac{i}{n}} \cdot \left(100 \cdot \left(\log i - \log n\right)\right)\\ \end{array} \]

Reproduce

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