Average Error: 42.8 → 27.7
Time: 34.9s
Precision: 64
\[100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}\]
\[\begin{array}{l} \mathbf{if}\;n \le -1.8734143464281 \cdot 10^{-311}:\\ \;\;\;\;\mathsf{fma}\left(e^{\left(\left(\mathsf{log1p}\left(\frac{i}{n}\right)\right)\right) \cdot n}, 100, -100\right) \cdot \frac{1}{\frac{i}{n}}\\ \mathbf{elif}\;n \le 1.512315662452518 \cdot 10^{-93}:\\ \;\;\;\;\frac{\mathsf{fma}\left(50 \cdot \left(n \cdot n\right), \log n \cdot \log n, \mathsf{fma}\left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \frac{50}{3}, \left(\log i \cdot \log i\right) \cdot \log i, \mathsf{fma}\left(n \cdot \log i, 100, \mathsf{fma}\left(\frac{100}{3}, \left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \log i\right) \cdot \left(\log n \cdot \log n\right), \mathsf{fma}\left(\frac{50}{3}, \left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \log i\right) \cdot \left(\log n \cdot \log n\right), \left(50 \cdot \left(n \cdot n\right)\right) \cdot \left(\log i \cdot \log i\right)\right)\right)\right)\right)\right) - \mathsf{fma}\left(\frac{100}{3} \cdot \left(n \cdot \left(n \cdot n\right)\right), \left(\log i \cdot \log i\right) \cdot \log n, \log n \cdot \left(n \cdot 100\right) + \left(\left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \frac{50}{3}\right) \cdot \left(\left(\log i \cdot \log i\right) \cdot \log n + \log n \cdot \left(\log n \cdot \log n\right)\right) + \left(\left(\log n \cdot \log i\right) \cdot \left(n \cdot n\right)\right) \cdot 100\right)\right)}{\frac{i}{n}}\\ \mathbf{elif}\;n \le 1.7502911309066923 \cdot 10^{+267}:\\ \;\;\;\;\frac{\mathsf{fma}\left(100, i, \left(i \cdot i\right) \cdot \left(50 + i \cdot \frac{50}{3}\right)\right)}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(e^{\left(\left(\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n\right)\right)}, 100, -100\right)}{\frac{i}{n}}\\ \end{array}\]
100 \cdot \frac{{\left(1 + \frac{i}{n}\right)}^{n} - 1}{\frac{i}{n}}
\begin{array}{l}
\mathbf{if}\;n \le -1.8734143464281 \cdot 10^{-311}:\\
\;\;\;\;\mathsf{fma}\left(e^{\left(\left(\mathsf{log1p}\left(\frac{i}{n}\right)\right)\right) \cdot n}, 100, -100\right) \cdot \frac{1}{\frac{i}{n}}\\

\mathbf{elif}\;n \le 1.512315662452518 \cdot 10^{-93}:\\
\;\;\;\;\frac{\mathsf{fma}\left(50 \cdot \left(n \cdot n\right), \log n \cdot \log n, \mathsf{fma}\left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \frac{50}{3}, \left(\log i \cdot \log i\right) \cdot \log i, \mathsf{fma}\left(n \cdot \log i, 100, \mathsf{fma}\left(\frac{100}{3}, \left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \log i\right) \cdot \left(\log n \cdot \log n\right), \mathsf{fma}\left(\frac{50}{3}, \left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \log i\right) \cdot \left(\log n \cdot \log n\right), \left(50 \cdot \left(n \cdot n\right)\right) \cdot \left(\log i \cdot \log i\right)\right)\right)\right)\right)\right) - \mathsf{fma}\left(\frac{100}{3} \cdot \left(n \cdot \left(n \cdot n\right)\right), \left(\log i \cdot \log i\right) \cdot \log n, \log n \cdot \left(n \cdot 100\right) + \left(\left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \frac{50}{3}\right) \cdot \left(\left(\log i \cdot \log i\right) \cdot \log n + \log n \cdot \left(\log n \cdot \log n\right)\right) + \left(\left(\log n \cdot \log i\right) \cdot \left(n \cdot n\right)\right) \cdot 100\right)\right)}{\frac{i}{n}}\\

\mathbf{elif}\;n \le 1.7502911309066923 \cdot 10^{+267}:\\
\;\;\;\;\frac{\mathsf{fma}\left(100, i, \left(i \cdot i\right) \cdot \left(50 + i \cdot \frac{50}{3}\right)\right)}{\frac{i}{n}}\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(e^{\left(\left(\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n\right)\right)}, 100, -100\right)}{\frac{i}{n}}\\

\end{array}
double f(double i, double n) {
        double r5543147 = 100.0;
        double r5543148 = 1.0;
        double r5543149 = i;
        double r5543150 = n;
        double r5543151 = r5543149 / r5543150;
        double r5543152 = r5543148 + r5543151;
        double r5543153 = pow(r5543152, r5543150);
        double r5543154 = r5543153 - r5543148;
        double r5543155 = r5543154 / r5543151;
        double r5543156 = r5543147 * r5543155;
        return r5543156;
}

double f(double i, double n) {
        double r5543157 = n;
        double r5543158 = -1.8734143464281e-311;
        bool r5543159 = r5543157 <= r5543158;
        double r5543160 = i;
        double r5543161 = r5543160 / r5543157;
        double r5543162 = log1p(r5543161);
        double r5543163 = /* ERROR: no posit support in C */;
        double r5543164 = /* ERROR: no posit support in C */;
        double r5543165 = r5543164 * r5543157;
        double r5543166 = exp(r5543165);
        double r5543167 = 100.0;
        double r5543168 = -100.0;
        double r5543169 = fma(r5543166, r5543167, r5543168);
        double r5543170 = 1.0;
        double r5543171 = r5543170 / r5543161;
        double r5543172 = r5543169 * r5543171;
        double r5543173 = 1.512315662452518e-93;
        bool r5543174 = r5543157 <= r5543173;
        double r5543175 = 50.0;
        double r5543176 = r5543157 * r5543157;
        double r5543177 = r5543175 * r5543176;
        double r5543178 = log(r5543157);
        double r5543179 = r5543178 * r5543178;
        double r5543180 = r5543157 * r5543176;
        double r5543181 = 16.666666666666668;
        double r5543182 = r5543180 * r5543181;
        double r5543183 = log(r5543160);
        double r5543184 = r5543183 * r5543183;
        double r5543185 = r5543184 * r5543183;
        double r5543186 = r5543157 * r5543183;
        double r5543187 = 33.333333333333336;
        double r5543188 = r5543180 * r5543183;
        double r5543189 = r5543188 * r5543179;
        double r5543190 = r5543177 * r5543184;
        double r5543191 = fma(r5543181, r5543189, r5543190);
        double r5543192 = fma(r5543187, r5543189, r5543191);
        double r5543193 = fma(r5543186, r5543167, r5543192);
        double r5543194 = fma(r5543182, r5543185, r5543193);
        double r5543195 = fma(r5543177, r5543179, r5543194);
        double r5543196 = r5543187 * r5543180;
        double r5543197 = r5543184 * r5543178;
        double r5543198 = r5543157 * r5543167;
        double r5543199 = r5543178 * r5543198;
        double r5543200 = r5543178 * r5543179;
        double r5543201 = r5543197 + r5543200;
        double r5543202 = r5543182 * r5543201;
        double r5543203 = r5543178 * r5543183;
        double r5543204 = r5543203 * r5543176;
        double r5543205 = r5543204 * r5543167;
        double r5543206 = r5543202 + r5543205;
        double r5543207 = r5543199 + r5543206;
        double r5543208 = fma(r5543196, r5543197, r5543207);
        double r5543209 = r5543195 - r5543208;
        double r5543210 = r5543209 / r5543161;
        double r5543211 = 1.7502911309066923e+267;
        bool r5543212 = r5543157 <= r5543211;
        double r5543213 = r5543160 * r5543160;
        double r5543214 = r5543160 * r5543181;
        double r5543215 = r5543175 + r5543214;
        double r5543216 = r5543213 * r5543215;
        double r5543217 = fma(r5543167, r5543160, r5543216);
        double r5543218 = r5543217 / r5543161;
        double r5543219 = r5543162 * r5543157;
        double r5543220 = /* ERROR: no posit support in C */;
        double r5543221 = /* ERROR: no posit support in C */;
        double r5543222 = exp(r5543221);
        double r5543223 = fma(r5543222, r5543167, r5543168);
        double r5543224 = r5543223 / r5543161;
        double r5543225 = r5543212 ? r5543218 : r5543224;
        double r5543226 = r5543174 ? r5543210 : r5543225;
        double r5543227 = r5543159 ? r5543172 : r5543226;
        return r5543227;
}

Error

Bits error versus i

Bits error versus n

Target

Original42.8
Target42.1
Herbie27.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 4 regimes
  2. if n < -1.8734143464281e-311

    1. Initial program 33.0

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left({\left(1 + \frac{i}{n}\right)}^{n}, 100, -100\right)}{\frac{i}{n}}}\]
    3. Using strategy rm
    4. Applied pow-to-exp37.3

      \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{e^{\log \left(1 + \frac{i}{n}\right) \cdot n}}, 100, -100\right)}{\frac{i}{n}}\]
    5. Simplified31.7

      \[\leadsto \frac{\mathsf{fma}\left(e^{\color{blue}{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}}, 100, -100\right)}{\frac{i}{n}}\]
    6. Using strategy rm
    7. Applied div-inv31.8

      \[\leadsto \color{blue}{\mathsf{fma}\left(e^{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}, 100, -100\right) \cdot \frac{1}{\frac{i}{n}}}\]
    8. Using strategy rm
    9. Applied insert-posit1628.6

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

    if -1.8734143464281e-311 < n < 1.512315662452518e-93

    1. Initial program 45.3

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left({\left(1 + \frac{i}{n}\right)}^{n}, 100, -100\right)}{\frac{i}{n}}}\]
    3. Using strategy rm
    4. Applied pow-to-exp45.3

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

      \[\leadsto \frac{\mathsf{fma}\left(e^{\color{blue}{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}}, 100, -100\right)}{\frac{i}{n}}\]
    6. Taylor expanded around 0 18.9

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

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

    if 1.512315662452518e-93 < n < 1.7502911309066923e+267

    1. Initial program 59.0

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left({\left(1 + \frac{i}{n}\right)}^{n}, 100, -100\right)}{\frac{i}{n}}}\]
    3. Taylor expanded around 0 29.4

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

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

    if 1.7502911309066923e+267 < n

    1. Initial program 58.3

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left({\left(1 + \frac{i}{n}\right)}^{n}, 100, -100\right)}{\frac{i}{n}}}\]
    3. Using strategy rm
    4. Applied pow-to-exp58.3

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

      \[\leadsto \frac{\mathsf{fma}\left(e^{\color{blue}{n \cdot \mathsf{log1p}\left(\frac{i}{n}\right)}}, 100, -100\right)}{\frac{i}{n}}\]
    6. Using strategy rm
    7. Applied insert-posit1638.3

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \le -1.8734143464281 \cdot 10^{-311}:\\ \;\;\;\;\mathsf{fma}\left(e^{\left(\left(\mathsf{log1p}\left(\frac{i}{n}\right)\right)\right) \cdot n}, 100, -100\right) \cdot \frac{1}{\frac{i}{n}}\\ \mathbf{elif}\;n \le 1.512315662452518 \cdot 10^{-93}:\\ \;\;\;\;\frac{\mathsf{fma}\left(50 \cdot \left(n \cdot n\right), \log n \cdot \log n, \mathsf{fma}\left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \frac{50}{3}, \left(\log i \cdot \log i\right) \cdot \log i, \mathsf{fma}\left(n \cdot \log i, 100, \mathsf{fma}\left(\frac{100}{3}, \left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \log i\right) \cdot \left(\log n \cdot \log n\right), \mathsf{fma}\left(\frac{50}{3}, \left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \log i\right) \cdot \left(\log n \cdot \log n\right), \left(50 \cdot \left(n \cdot n\right)\right) \cdot \left(\log i \cdot \log i\right)\right)\right)\right)\right)\right) - \mathsf{fma}\left(\frac{100}{3} \cdot \left(n \cdot \left(n \cdot n\right)\right), \left(\log i \cdot \log i\right) \cdot \log n, \log n \cdot \left(n \cdot 100\right) + \left(\left(\left(n \cdot \left(n \cdot n\right)\right) \cdot \frac{50}{3}\right) \cdot \left(\left(\log i \cdot \log i\right) \cdot \log n + \log n \cdot \left(\log n \cdot \log n\right)\right) + \left(\left(\log n \cdot \log i\right) \cdot \left(n \cdot n\right)\right) \cdot 100\right)\right)}{\frac{i}{n}}\\ \mathbf{elif}\;n \le 1.7502911309066923 \cdot 10^{+267}:\\ \;\;\;\;\frac{\mathsf{fma}\left(100, i, \left(i \cdot i\right) \cdot \left(50 + i \cdot \frac{50}{3}\right)\right)}{\frac{i}{n}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(e^{\left(\left(\mathsf{log1p}\left(\frac{i}{n}\right) \cdot n\right)\right)}, 100, -100\right)}{\frac{i}{n}}\\ \end{array}\]

Reproduce

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

  :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))))