Average Error: 30.7 → 0.5
Time: 26.7s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.02027792377591882896914476930305681889877:\\ \;\;\;\;\frac{\frac{\left(\left(1 \cdot 1\right) \cdot \left(\left(1 \cdot 1\right) \cdot \left(1 \cdot 1\right)\right)\right) \cdot \left(1 \cdot \left(1 \cdot 1\right)\right) - \left(\mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right) \cdot \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)\right) \cdot \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)}{\mathsf{fma}\left(\mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right), \mathsf{fma}\left(1 \cdot 1, 1, \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)\right), \left(1 \cdot 1\right) \cdot \left(\left(1 \cdot 1\right) \cdot \left(1 \cdot 1\right)\right)\right)}}{\sin x \cdot \mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}\\ \mathbf{elif}\;x \le 0.01868325523481086797294992152274062391371:\\ \;\;\;\;\mathsf{fma}\left({x}^{5}, \frac{1}{240}, \left(\frac{1}{2} + \left(x \cdot x\right) \cdot \frac{1}{24}\right) \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{\log \left(\mathsf{fma}\left(1, 1 \cdot 1, \mathsf{fma}\left(\cos \left(x + x\right), \cos x, \cos x\right) \cdot \frac{-1}{2}\right)\right)}}{\sin x \cdot \mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.02027792377591882896914476930305681889877:\\
\;\;\;\;\frac{\frac{\left(\left(1 \cdot 1\right) \cdot \left(\left(1 \cdot 1\right) \cdot \left(1 \cdot 1\right)\right)\right) \cdot \left(1 \cdot \left(1 \cdot 1\right)\right) - \left(\mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right) \cdot \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)\right) \cdot \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)}{\mathsf{fma}\left(\mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right), \mathsf{fma}\left(1 \cdot 1, 1, \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)\right), \left(1 \cdot 1\right) \cdot \left(\left(1 \cdot 1\right) \cdot \left(1 \cdot 1\right)\right)\right)}}{\sin x \cdot \mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}\\

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

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

\end{array}
double f(double x) {
        double r2740540 = 1.0;
        double r2740541 = x;
        double r2740542 = cos(r2740541);
        double r2740543 = r2740540 - r2740542;
        double r2740544 = sin(r2740541);
        double r2740545 = r2740543 / r2740544;
        return r2740545;
}

double f(double x) {
        double r2740546 = x;
        double r2740547 = -0.02027792377591883;
        bool r2740548 = r2740546 <= r2740547;
        double r2740549 = 1.0;
        double r2740550 = r2740549 * r2740549;
        double r2740551 = r2740550 * r2740550;
        double r2740552 = r2740550 * r2740551;
        double r2740553 = r2740549 * r2740550;
        double r2740554 = r2740552 * r2740553;
        double r2740555 = r2740546 + r2740546;
        double r2740556 = cos(r2740555);
        double r2740557 = cos(r2740546);
        double r2740558 = 0.5;
        double r2740559 = r2740557 * r2740558;
        double r2740560 = fma(r2740556, r2740559, r2740559);
        double r2740561 = r2740560 * r2740560;
        double r2740562 = r2740561 * r2740560;
        double r2740563 = r2740554 - r2740562;
        double r2740564 = fma(r2740550, r2740549, r2740560);
        double r2740565 = fma(r2740560, r2740564, r2740552);
        double r2740566 = r2740563 / r2740565;
        double r2740567 = sin(r2740546);
        double r2740568 = r2740549 + r2740557;
        double r2740569 = fma(r2740557, r2740568, r2740550);
        double r2740570 = r2740567 * r2740569;
        double r2740571 = r2740566 / r2740570;
        double r2740572 = 0.018683255234810868;
        bool r2740573 = r2740546 <= r2740572;
        double r2740574 = 5.0;
        double r2740575 = pow(r2740546, r2740574);
        double r2740576 = 0.004166666666666667;
        double r2740577 = r2740546 * r2740546;
        double r2740578 = 0.041666666666666664;
        double r2740579 = r2740577 * r2740578;
        double r2740580 = r2740558 + r2740579;
        double r2740581 = r2740580 * r2740546;
        double r2740582 = fma(r2740575, r2740576, r2740581);
        double r2740583 = fma(r2740556, r2740557, r2740557);
        double r2740584 = -0.5;
        double r2740585 = r2740583 * r2740584;
        double r2740586 = fma(r2740549, r2740550, r2740585);
        double r2740587 = log(r2740586);
        double r2740588 = exp(r2740587);
        double r2740589 = r2740588 / r2740570;
        double r2740590 = r2740573 ? r2740582 : r2740589;
        double r2740591 = r2740548 ? r2740571 : r2740590;
        return r2740591;
}

Error

Bits error versus x

Target

Original30.7
Target0.0
Herbie0.5
\[\tan \left(\frac{x}{2}\right)\]

Derivation

  1. Split input into 3 regimes
  2. if x < -0.02027792377591883

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied flip3--1.0

      \[\leadsto \frac{\color{blue}{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}}{\sin x}\]
    4. Applied associate-/l/1.0

      \[\leadsto \color{blue}{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\sin x \cdot \left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right)}}\]
    5. Simplified1.0

      \[\leadsto \frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\color{blue}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}}\]
    6. Using strategy rm
    7. Applied log1p-expm1-u1.1

      \[\leadsto \frac{{1}^{3} - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left({\left(\cos x\right)}^{3}\right)\right)}}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}\]
    8. Simplified1.1

      \[\leadsto \frac{{1}^{3} - \mathsf{log1p}\left(\color{blue}{\mathsf{expm1}\left(\cos x \cdot \left(\cos x \cdot \cos x\right)\right)}\right)}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}\]
    9. Using strategy rm
    10. Applied sqr-cos1.1

      \[\leadsto \frac{{1}^{3} - \mathsf{log1p}\left(\mathsf{expm1}\left(\cos x \cdot \color{blue}{\left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(2 \cdot x\right)\right)}\right)\right)}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}\]
    11. Simplified1.1

      \[\leadsto \frac{{1}^{3} - \mathsf{log1p}\left(\mathsf{expm1}\left(\cos x \cdot \left(\frac{1}{2} + \color{blue}{\frac{1}{2} \cdot \cos \left(x + x\right)}\right)\right)\right)}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}\]
    12. Using strategy rm
    13. Applied flip3--1.1

      \[\leadsto \frac{\color{blue}{\frac{{\left({1}^{3}\right)}^{3} - {\left(\mathsf{log1p}\left(\mathsf{expm1}\left(\cos x \cdot \left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(x + x\right)\right)\right)\right)\right)}^{3}}{{1}^{3} \cdot {1}^{3} + \left(\mathsf{log1p}\left(\mathsf{expm1}\left(\cos x \cdot \left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(x + x\right)\right)\right)\right) \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\cos x \cdot \left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(x + x\right)\right)\right)\right) + {1}^{3} \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\cos x \cdot \left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(x + x\right)\right)\right)\right)\right)}}}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}\]
    14. Simplified1.1

      \[\leadsto \frac{\frac{\color{blue}{\left(\left(\left(1 \cdot 1\right) \cdot \left(1 \cdot 1\right)\right) \cdot \left(1 \cdot 1\right)\right) \cdot \left(\left(1 \cdot 1\right) \cdot 1\right) - \left(\mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right) \cdot \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)\right) \cdot \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)}}{{1}^{3} \cdot {1}^{3} + \left(\mathsf{log1p}\left(\mathsf{expm1}\left(\cos x \cdot \left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(x + x\right)\right)\right)\right) \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\cos x \cdot \left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(x + x\right)\right)\right)\right) + {1}^{3} \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\cos x \cdot \left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(x + x\right)\right)\right)\right)\right)}}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}\]
    15. Simplified1.0

      \[\leadsto \frac{\frac{\left(\left(\left(1 \cdot 1\right) \cdot \left(1 \cdot 1\right)\right) \cdot \left(1 \cdot 1\right)\right) \cdot \left(\left(1 \cdot 1\right) \cdot 1\right) - \left(\mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right) \cdot \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)\right) \cdot \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)}{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right), \mathsf{fma}\left(1 \cdot 1, 1, \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)\right), \left(1 \cdot 1\right) \cdot \left(\left(1 \cdot 1\right) \cdot \left(1 \cdot 1\right)\right)\right)}}}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}\]

    if -0.02027792377591883 < x < 0.018683255234810868

    1. Initial program 60.0

      \[\frac{1 - \cos x}{\sin x}\]
    2. Taylor expanded around 0 0.0

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

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

    if 0.018683255234810868 < x

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied flip3--1.0

      \[\leadsto \frac{\color{blue}{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}}{\sin x}\]
    4. Applied associate-/l/1.0

      \[\leadsto \color{blue}{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\sin x \cdot \left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right)}}\]
    5. Simplified1.0

      \[\leadsto \frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\color{blue}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}}\]
    6. Using strategy rm
    7. Applied log1p-expm1-u1.0

      \[\leadsto \frac{{1}^{3} - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left({\left(\cos x\right)}^{3}\right)\right)}}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}\]
    8. Simplified1.0

      \[\leadsto \frac{{1}^{3} - \mathsf{log1p}\left(\color{blue}{\mathsf{expm1}\left(\cos x \cdot \left(\cos x \cdot \cos x\right)\right)}\right)}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}\]
    9. Using strategy rm
    10. Applied sqr-cos1.0

      \[\leadsto \frac{{1}^{3} - \mathsf{log1p}\left(\mathsf{expm1}\left(\cos x \cdot \color{blue}{\left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(2 \cdot x\right)\right)}\right)\right)}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}\]
    11. Simplified1.0

      \[\leadsto \frac{{1}^{3} - \mathsf{log1p}\left(\mathsf{expm1}\left(\cos x \cdot \left(\frac{1}{2} + \color{blue}{\frac{1}{2} \cdot \cos \left(x + x\right)}\right)\right)\right)}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}\]
    12. Using strategy rm
    13. Applied add-exp-log1.0

      \[\leadsto \frac{\color{blue}{e^{\log \left({1}^{3} - \mathsf{log1p}\left(\mathsf{expm1}\left(\cos x \cdot \left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(x + x\right)\right)\right)\right)\right)}}}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right) \cdot \sin x}\]
    14. Simplified0.9

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.02027792377591882896914476930305681889877:\\ \;\;\;\;\frac{\frac{\left(\left(1 \cdot 1\right) \cdot \left(\left(1 \cdot 1\right) \cdot \left(1 \cdot 1\right)\right)\right) \cdot \left(1 \cdot \left(1 \cdot 1\right)\right) - \left(\mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right) \cdot \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)\right) \cdot \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)}{\mathsf{fma}\left(\mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right), \mathsf{fma}\left(1 \cdot 1, 1, \mathsf{fma}\left(\cos \left(x + x\right), \cos x \cdot \frac{1}{2}, \cos x \cdot \frac{1}{2}\right)\right), \left(1 \cdot 1\right) \cdot \left(\left(1 \cdot 1\right) \cdot \left(1 \cdot 1\right)\right)\right)}}{\sin x \cdot \mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}\\ \mathbf{elif}\;x \le 0.01868325523481086797294992152274062391371:\\ \;\;\;\;\mathsf{fma}\left({x}^{5}, \frac{1}{240}, \left(\frac{1}{2} + \left(x \cdot x\right) \cdot \frac{1}{24}\right) \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{\log \left(\mathsf{fma}\left(1, 1 \cdot 1, \mathsf{fma}\left(\cos \left(x + x\right), \cos x, \cos x\right) \cdot \frac{-1}{2}\right)\right)}}{\sin x \cdot \mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}\\ \end{array}\]

Reproduce

herbie shell --seed 2019172 +o rules:numerics
(FPCore (x)
  :name "tanhf (example 3.4)"
  :herbie-expected 2

  :herbie-target
  (tan (/ x 2.0))

  (/ (- 1.0 (cos x)) (sin x)))