Average Error: 30.3 → 0.7
Time: 7.4s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -2.76308119989467809 \cdot 10^{-4}:\\ \;\;\;\;\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le -0.0:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sin x} - \frac{\cos x}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -2.76308119989467809 \cdot 10^{-4}:\\
\;\;\;\;\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le -0.0:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\sin x} - \frac{\cos x}{\sin x}\\

\end{array}
double f(double x) {
        double r61439 = 1.0;
        double r61440 = x;
        double r61441 = cos(r61440);
        double r61442 = r61439 - r61441;
        double r61443 = sin(r61440);
        double r61444 = r61442 / r61443;
        return r61444;
}

double f(double x) {
        double r61445 = 1.0;
        double r61446 = x;
        double r61447 = cos(r61446);
        double r61448 = r61445 - r61447;
        double r61449 = sin(r61446);
        double r61450 = r61448 / r61449;
        double r61451 = -0.0002763081199894678;
        bool r61452 = r61450 <= r61451;
        double r61453 = 3.0;
        double r61454 = pow(r61445, r61453);
        double r61455 = pow(r61447, r61453);
        double r61456 = r61454 - r61455;
        double r61457 = r61445 * r61447;
        double r61458 = fma(r61447, r61447, r61457);
        double r61459 = fma(r61445, r61445, r61458);
        double r61460 = r61459 * r61449;
        double r61461 = r61456 / r61460;
        double r61462 = -0.0;
        bool r61463 = r61450 <= r61462;
        double r61464 = 0.041666666666666664;
        double r61465 = pow(r61446, r61453);
        double r61466 = 0.004166666666666667;
        double r61467 = 5.0;
        double r61468 = pow(r61446, r61467);
        double r61469 = 0.5;
        double r61470 = r61469 * r61446;
        double r61471 = fma(r61466, r61468, r61470);
        double r61472 = fma(r61464, r61465, r61471);
        double r61473 = r61445 / r61449;
        double r61474 = r61447 / r61449;
        double r61475 = r61473 - r61474;
        double r61476 = r61463 ? r61472 : r61475;
        double r61477 = r61452 ? r61461 : r61476;
        return r61477;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 3 regimes
  2. if (/ (- 1.0 (cos x)) (sin x)) < -0.0002763081199894678

    1. Initial program 1.0

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied add-log-exp1.2

      \[\leadsto \color{blue}{\log \left(e^{\frac{1 - \cos x}{\sin x}}\right)}\]
    4. Using strategy rm
    5. Applied clear-num1.2

      \[\leadsto \log \left(e^{\color{blue}{\frac{1}{\frac{\sin x}{1 - \cos x}}}}\right)\]
    6. Taylor expanded around inf 1.0

      \[\leadsto \color{blue}{\frac{1 - \cos x}{\sin x}}\]
    7. Using strategy rm
    8. Applied flip3--1.1

      \[\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}\]
    9. Applied associate-/l/1.1

      \[\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)}}\]
    10. Simplified1.1

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

    if -0.0002763081199894678 < (/ (- 1.0 (cos x)) (sin x)) < -0.0

    1. Initial program 60.3

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

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

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

    if -0.0 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 1.3

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied div-sub1.5

      \[\leadsto \color{blue}{\frac{1}{\sin x} - \frac{\cos x}{\sin x}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -2.76308119989467809 \cdot 10^{-4}:\\ \;\;\;\;\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le -0.0:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sin x} - \frac{\cos x}{\sin x}\\ \end{array}\]

Reproduce

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

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

  (/ (- 1 (cos x)) (sin x)))