Average Error: 9.7 → 0.5
Time: 5.4s
Precision: 64
\[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}\]
\[\begin{array}{l} \mathbf{if}\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \le -33511.8397027548854:\\ \;\;\;\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}\\ \mathbf{elif}\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \le 1.65436 \cdot 10^{-24}:\\ \;\;\;\;\mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \frac{\frac{2}{x \cdot x}}{x}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(1 \cdot x - \left(x + 1\right) \cdot 2, x - 1, \left(\left(x + 1\right) \cdot x\right) \cdot 1\right)}{\left(\left(x + 1\right) \cdot x\right) \cdot \left(x - 1\right)}\\ \end{array}\]
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\begin{array}{l}
\mathbf{if}\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \le -33511.8397027548854:\\
\;\;\;\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}\\

\mathbf{elif}\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \le 1.65436 \cdot 10^{-24}:\\
\;\;\;\;\mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \frac{\frac{2}{x \cdot x}}{x}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(1 \cdot x - \left(x + 1\right) \cdot 2, x - 1, \left(\left(x + 1\right) \cdot x\right) \cdot 1\right)}{\left(\left(x + 1\right) \cdot x\right) \cdot \left(x - 1\right)}\\

\end{array}
double f(double x) {
        double r132532 = 1.0;
        double r132533 = x;
        double r132534 = r132533 + r132532;
        double r132535 = r132532 / r132534;
        double r132536 = 2.0;
        double r132537 = r132536 / r132533;
        double r132538 = r132535 - r132537;
        double r132539 = r132533 - r132532;
        double r132540 = r132532 / r132539;
        double r132541 = r132538 + r132540;
        return r132541;
}

double f(double x) {
        double r132542 = 1.0;
        double r132543 = x;
        double r132544 = r132543 + r132542;
        double r132545 = r132542 / r132544;
        double r132546 = 2.0;
        double r132547 = r132546 / r132543;
        double r132548 = r132545 - r132547;
        double r132549 = r132543 - r132542;
        double r132550 = r132542 / r132549;
        double r132551 = r132548 + r132550;
        double r132552 = -33511.839702754885;
        bool r132553 = r132551 <= r132552;
        double r132554 = 1.6543612251060553e-24;
        bool r132555 = r132551 <= r132554;
        double r132556 = 1.0;
        double r132557 = 7.0;
        double r132558 = pow(r132543, r132557);
        double r132559 = r132556 / r132558;
        double r132560 = 5.0;
        double r132561 = pow(r132543, r132560);
        double r132562 = r132556 / r132561;
        double r132563 = r132543 * r132543;
        double r132564 = r132546 / r132563;
        double r132565 = r132564 / r132543;
        double r132566 = fma(r132546, r132562, r132565);
        double r132567 = fma(r132546, r132559, r132566);
        double r132568 = r132542 * r132543;
        double r132569 = r132544 * r132546;
        double r132570 = r132568 - r132569;
        double r132571 = r132544 * r132543;
        double r132572 = r132571 * r132542;
        double r132573 = fma(r132570, r132549, r132572);
        double r132574 = r132571 * r132549;
        double r132575 = r132573 / r132574;
        double r132576 = r132555 ? r132567 : r132575;
        double r132577 = r132553 ? r132551 : r132576;
        return r132577;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 3 regimes
  2. if (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))) < -33511.839702754885

    1. Initial program 0.0

      \[\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}\]

    if -33511.839702754885 < (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))) < 1.6543612251060553e-24

    1. Initial program 19.4

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

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

      \[\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 unpow31.4

      \[\leadsto \mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \frac{2}{\color{blue}{\left(x \cdot x\right) \cdot x}}\right)\right)\]
    6. Applied associate-/r*0.8

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

    if 1.6543612251060553e-24 < (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0)))

    1. Initial program 0.5

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

      \[\leadsto \color{blue}{\frac{1 \cdot x - \left(x + 1\right) \cdot 2}{\left(x + 1\right) \cdot x}} + \frac{1}{x - 1}\]
    4. Applied frac-add0.0

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \le -33511.8397027548854:\\ \;\;\;\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}\\ \mathbf{elif}\;\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1} \le 1.65436 \cdot 10^{-24}:\\ \;\;\;\;\mathsf{fma}\left(2, \frac{1}{{x}^{7}}, \mathsf{fma}\left(2, \frac{1}{{x}^{5}}, \frac{\frac{2}{x \cdot x}}{x}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(1 \cdot x - \left(x + 1\right) \cdot 2, x - 1, \left(\left(x + 1\right) \cdot x\right) \cdot 1\right)}{\left(\left(x + 1\right) \cdot x\right) \cdot \left(x - 1\right)}\\ \end{array}\]

Reproduce

herbie shell --seed 2020046 +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))))