Average Error: 9.5 → 0.1
Time: 7.1s
Precision: 64
\[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}\]
\[\begin{array}{l} \mathbf{if}\;x \le -112.31575512255452 \lor \neg \left(x \le 107.919341256729908\right):\\ \;\;\;\;\mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, {x}^{\left(-2\right)} \cdot \frac{2}{x}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1}{x - 1}\right)\right)\\ \end{array}\]
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\begin{array}{l}
\mathbf{if}\;x \le -112.31575512255452 \lor \neg \left(x \le 107.919341256729908\right):\\
\;\;\;\;\mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, {x}^{\left(-2\right)} \cdot \frac{2}{x}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1}{x - 1}\right)\right)\\

\end{array}
double f(double x) {
        double r103366 = 1.0;
        double r103367 = x;
        double r103368 = r103367 + r103366;
        double r103369 = r103366 / r103368;
        double r103370 = 2.0;
        double r103371 = r103370 / r103367;
        double r103372 = r103369 - r103371;
        double r103373 = r103367 - r103366;
        double r103374 = r103366 / r103373;
        double r103375 = r103372 + r103374;
        return r103375;
}

double f(double x) {
        double r103376 = x;
        double r103377 = -112.31575512255452;
        bool r103378 = r103376 <= r103377;
        double r103379 = 107.91934125672991;
        bool r103380 = r103376 <= r103379;
        double r103381 = !r103380;
        bool r103382 = r103378 || r103381;
        double r103383 = 2.0;
        double r103384 = 1.0;
        double r103385 = 7.0;
        double r103386 = pow(r103376, r103385);
        double r103387 = r103384 / r103386;
        double r103388 = 5.0;
        double r103389 = pow(r103376, r103388);
        double r103390 = r103384 / r103389;
        double r103391 = 2.0;
        double r103392 = -r103391;
        double r103393 = pow(r103376, r103392);
        double r103394 = r103383 / r103376;
        double r103395 = r103393 * r103394;
        double r103396 = fma(r103383, r103390, r103395);
        double r103397 = fma(r103383, r103387, r103396);
        double r103398 = 1.0;
        double r103399 = r103376 + r103398;
        double r103400 = r103398 / r103399;
        double r103401 = r103400 - r103394;
        double r103402 = r103376 - r103398;
        double r103403 = r103398 / r103402;
        double r103404 = expm1(r103403);
        double r103405 = log1p(r103404);
        double r103406 = r103401 + r103405;
        double r103407 = r103382 ? r103397 : r103406;
        return r103407;
}

Error

Bits error versus x

Target

Original9.5
Target0.3
Herbie0.1
\[\frac{2}{x \cdot \left(x \cdot x - 1\right)}\]

Derivation

  1. Split input into 2 regimes
  2. if x < -112.31575512255452 or 107.91934125672991 < x

    1. Initial program 19.1

      \[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}\]
    2. Taylor expanded around inf 0.5

      \[\leadsto \color{blue}{2 \cdot \frac{1}{{x}^{7}} + \left(2 \cdot \frac{1}{{x}^{5}} + 2 \cdot \frac{1}{{x}^{3}}\right)}\]
    3. Simplified0.5

      \[\leadsto \color{blue}{\mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \frac{2}{{x}^{3}}\right)\right)}\]
    4. Using strategy rm
    5. Applied add-cube-cbrt1.1

      \[\leadsto \mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \frac{2}{{\color{blue}{\left(\left(\sqrt[3]{x} \cdot \sqrt[3]{x}\right) \cdot \sqrt[3]{x}\right)}}^{3}}\right)\right)\]
    6. Applied unpow-prod-down1.1

      \[\leadsto \mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \frac{2}{\color{blue}{{\left(\sqrt[3]{x} \cdot \sqrt[3]{x}\right)}^{3} \cdot {\left(\sqrt[3]{x}\right)}^{3}}}\right)\right)\]
    7. Applied *-un-lft-identity1.1

      \[\leadsto \mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \frac{\color{blue}{1 \cdot 2}}{{\left(\sqrt[3]{x} \cdot \sqrt[3]{x}\right)}^{3} \cdot {\left(\sqrt[3]{x}\right)}^{3}}\right)\right)\]
    8. Applied times-frac0.7

      \[\leadsto \mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \color{blue}{\frac{1}{{\left(\sqrt[3]{x} \cdot \sqrt[3]{x}\right)}^{3}} \cdot \frac{2}{{\left(\sqrt[3]{x}\right)}^{3}}}\right)\right)\]
    9. Simplified0.4

      \[\leadsto \mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \color{blue}{\frac{1}{x \cdot x}} \cdot \frac{2}{{\left(\sqrt[3]{x}\right)}^{3}}\right)\right)\]
    10. Simplified0.1

      \[\leadsto \mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \frac{1}{x \cdot x} \cdot \color{blue}{\frac{2}{x}}\right)\right)\]
    11. Using strategy rm
    12. Applied pow10.1

      \[\leadsto \mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \frac{1}{x \cdot \color{blue}{{x}^{1}}} \cdot \frac{2}{x}\right)\right)\]
    13. Applied pow10.1

      \[\leadsto \mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \frac{1}{\color{blue}{{x}^{1}} \cdot {x}^{1}} \cdot \frac{2}{x}\right)\right)\]
    14. Applied pow-prod-up0.1

      \[\leadsto \mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \frac{1}{\color{blue}{{x}^{\left(1 + 1\right)}}} \cdot \frac{2}{x}\right)\right)\]
    15. Applied pow-flip0.1

      \[\leadsto \mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \color{blue}{{x}^{\left(-\left(1 + 1\right)\right)}} \cdot \frac{2}{x}\right)\right)\]
    16. Simplified0.1

      \[\leadsto \mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, {x}^{\color{blue}{\left(-2\right)}} \cdot \frac{2}{x}\right)\right)\]

    if -112.31575512255452 < x < 107.91934125672991

    1. Initial program 0.1

      \[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}\]
    2. Using strategy rm
    3. Applied log1p-expm1-u0.1

      \[\leadsto \left(\frac{1}{x + 1} - \frac{2}{x}\right) + \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1}{x - 1}\right)\right)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -112.31575512255452 \lor \neg \left(x \le 107.919341256729908\right):\\ \;\;\;\;\mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, {x}^{\left(-2\right)} \cdot \frac{2}{x}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1}{x - 1}\right)\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2020021 +o rules:numerics
(FPCore (x)
  :name "3frac (problem 3.3.3)"
  :precision binary64

  :herbie-target
  (/ 2 (* x (- (* x x) 1)))

  (+ (- (/ 1 (+ x 1)) (/ 2 x)) (/ 1 (- x 1))))