Average Error: 43.1 → 22.2
Time: 14.7s
Precision: 64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
\[\begin{array}{l} \mathbf{if}\;n \le -2.23087318151625042 \cdot 10^{136}:\\ \;\;\;\;\left(\left(100 \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot n\\ \mathbf{elif}\;n \le -1.09580041647786345 \cdot 10^{86}:\\ \;\;\;\;\frac{100 \cdot \left({\left(1 + \frac{i}{n}\right)}^{n} - 1\right)}{\frac{i}{n}}\\ \mathbf{elif}\;n \le -35142169422648208:\\ \;\;\;\;\left(\left(100 \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot n\\ \mathbf{elif}\;n \le -1.2431085101949388 \cdot 10^{-214}:\\ \;\;\;\;100 \cdot \frac{\log \left(e^{{\left(1 + \frac{i}{n}\right)}^{n} - 1}\right)}{\frac{i}{n}}\\ \mathbf{elif}\;n \le 2.0850330521039454 \cdot 10^{-172}:\\ \;\;\;\;100 \cdot \frac{\mathsf{fma}\left(1, i, \mathsf{fma}\left(\log 1, n, 1\right)\right) - 1}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;\left(\left(100 \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot n\\ \end{array}\]
100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\begin{array}{l}
\mathbf{if}\;n \le -2.23087318151625042 \cdot 10^{136}:\\
\;\;\;\;\left(\left(100 \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot n\\

\mathbf{elif}\;n \le -1.09580041647786345 \cdot 10^{86}:\\
\;\;\;\;\frac{100 \cdot \left({\left(1 + \frac{i}{n}\right)}^{n} - 1\right)}{\frac{i}{n}}\\

\mathbf{elif}\;n \le -35142169422648208:\\
\;\;\;\;\left(\left(100 \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot n\\

\mathbf{elif}\;n \le -1.2431085101949388 \cdot 10^{-214}:\\
\;\;\;\;100 \cdot \frac{\log \left(e^{{\left(1 + \frac{i}{n}\right)}^{n} - 1}\right)}{\frac{i}{n}}\\

\mathbf{elif}\;n \le 2.0850330521039454 \cdot 10^{-172}:\\
\;\;\;\;100 \cdot \frac{\mathsf{fma}\left(1, i, \mathsf{fma}\left(\log 1, n, 1\right)\right) - 1}{\frac{i}{n}}\\

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

\end{array}
double f(double i, double n) {
        double r162783 = 100.0;
        double r162784 = 1.0;
        double r162785 = i;
        double r162786 = n;
        double r162787 = r162785 / r162786;
        double r162788 = r162784 + r162787;
        double r162789 = pow(r162788, r162786);
        double r162790 = r162789 - r162784;
        double r162791 = r162790 / r162787;
        double r162792 = r162783 * r162791;
        return r162792;
}

double f(double i, double n) {
        double r162793 = n;
        double r162794 = -2.2308731815162504e+136;
        bool r162795 = r162793 <= r162794;
        double r162796 = 100.0;
        double r162797 = i;
        double r162798 = 1.0;
        double r162799 = 0.5;
        double r162800 = 2.0;
        double r162801 = pow(r162797, r162800);
        double r162802 = log(r162798);
        double r162803 = r162802 * r162793;
        double r162804 = fma(r162799, r162801, r162803);
        double r162805 = r162801 * r162802;
        double r162806 = r162799 * r162805;
        double r162807 = r162804 - r162806;
        double r162808 = fma(r162797, r162798, r162807);
        double r162809 = r162808 / r162797;
        double r162810 = sqrt(r162809);
        double r162811 = r162796 * r162810;
        double r162812 = r162811 * r162810;
        double r162813 = r162812 * r162793;
        double r162814 = -1.0958004164778634e+86;
        bool r162815 = r162793 <= r162814;
        double r162816 = r162797 / r162793;
        double r162817 = r162798 + r162816;
        double r162818 = pow(r162817, r162793);
        double r162819 = r162818 - r162798;
        double r162820 = r162796 * r162819;
        double r162821 = r162820 / r162816;
        double r162822 = -3.514216942264821e+16;
        bool r162823 = r162793 <= r162822;
        double r162824 = -1.2431085101949388e-214;
        bool r162825 = r162793 <= r162824;
        double r162826 = exp(r162819);
        double r162827 = log(r162826);
        double r162828 = r162827 / r162816;
        double r162829 = r162796 * r162828;
        double r162830 = 2.0850330521039454e-172;
        bool r162831 = r162793 <= r162830;
        double r162832 = 1.0;
        double r162833 = fma(r162802, r162793, r162832);
        double r162834 = fma(r162798, r162797, r162833);
        double r162835 = r162834 - r162798;
        double r162836 = r162835 / r162816;
        double r162837 = r162796 * r162836;
        double r162838 = r162831 ? r162837 : r162813;
        double r162839 = r162825 ? r162829 : r162838;
        double r162840 = r162823 ? r162813 : r162839;
        double r162841 = r162815 ? r162821 : r162840;
        double r162842 = r162795 ? r162813 : r162841;
        return r162842;
}

Error

Bits error versus i

Bits error versus n

Target

Original43.1
Target42.9
Herbie22.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 n < -2.2308731815162504e+136 or -1.0958004164778634e+86 < n < -3.514216942264821e+16 or 2.0850330521039454e-172 < n

    1. Initial program 54.1

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

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

      \[\leadsto 100 \cdot \frac{\color{blue}{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}}{\frac{i}{n}}\]
    4. Using strategy rm
    5. Applied associate-/r/22.0

      \[\leadsto 100 \cdot \color{blue}{\left(\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i} \cdot n\right)}\]
    6. Applied associate-*r*22.0

      \[\leadsto \color{blue}{\left(100 \cdot \frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}\right) \cdot n}\]
    7. Using strategy rm
    8. Applied add-sqr-sqrt22.1

      \[\leadsto \left(100 \cdot \color{blue}{\left(\sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}} \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right)}\right) \cdot n\]
    9. Applied associate-*r*22.1

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

    if -2.2308731815162504e+136 < n < -1.0958004164778634e+86

    1. Initial program 35.1

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Using strategy rm
    3. Applied associate-*r/35.0

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

    if -3.514216942264821e+16 < n < -1.2431085101949388e-214

    1. Initial program 19.3

      \[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
    2. Using strategy rm
    3. Applied add-log-exp19.3

      \[\leadsto 100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - \color{blue}{\log \left(e^{1}\right)}}{\frac{i}{n}}\]
    4. Applied add-log-exp19.4

      \[\leadsto 100 \cdot \frac{\color{blue}{\log \left(e^{{\left(1 + \frac{i}{n}\right)}^{n}}\right)} - \log \left(e^{1}\right)}{\frac{i}{n}}\]
    5. Applied diff-log19.4

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

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

    if -1.2431085101949388e-214 < n < 2.0850330521039454e-172

    1. Initial program 28.7

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

      \[\leadsto 100 \cdot \frac{\color{blue}{\left(1 \cdot i + \left(\log 1 \cdot n + 1\right)\right)} - 1}{\frac{i}{n}}\]
    3. Simplified21.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \le -2.23087318151625042 \cdot 10^{136}:\\ \;\;\;\;\left(\left(100 \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot n\\ \mathbf{elif}\;n \le -1.09580041647786345 \cdot 10^{86}:\\ \;\;\;\;\frac{100 \cdot \left({\left(1 + \frac{i}{n}\right)}^{n} - 1\right)}{\frac{i}{n}}\\ \mathbf{elif}\;n \le -35142169422648208:\\ \;\;\;\;\left(\left(100 \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot n\\ \mathbf{elif}\;n \le -1.2431085101949388 \cdot 10^{-214}:\\ \;\;\;\;100 \cdot \frac{\log \left(e^{{\left(1 + \frac{i}{n}\right)}^{n} - 1}\right)}{\frac{i}{n}}\\ \mathbf{elif}\;n \le 2.0850330521039454 \cdot 10^{-172}:\\ \;\;\;\;100 \cdot \frac{\mathsf{fma}\left(1, i, \mathsf{fma}\left(\log 1, n, 1\right)\right) - 1}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;\left(\left(100 \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot \sqrt{\frac{\mathsf{fma}\left(i, 1, \mathsf{fma}\left(0.5, {i}^{2}, \log 1 \cdot n\right) - 0.5 \cdot \left({i}^{2} \cdot \log 1\right)\right)}{i}}\right) \cdot n\\ \end{array}\]

Reproduce

herbie shell --seed 2020034 +o rules:numerics
(FPCore (i n)
  :name "Compound Interest"
  :precision binary64

  :herbie-target
  (* 100 (/ (- (exp (* n (if (== (+ 1 (/ i n)) 1) (/ i n) (/ (* (/ i n) (log (+ 1 (/ i n)))) (- (+ (/ i n) 1) 1))))) 1) (/ i n)))

  (* 100 (/ (- (pow (+ 1 (/ i n)) n) 1) (/ i n))))