Average Error: 30.0 → 0.5
Time: 8.5s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.02373580573239164950849477975225454429165:\\ \;\;\;\;\frac{\mathsf{log1p}\left(\log \left(e^{\mathsf{expm1}\left({1}^{3} - {\left(\cos x\right)}^{3}\right)}\right)\right)}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}\\ \mathbf{elif}\;x \le 0.02029306521166552093604629192213906208053:\\ \;\;\;\;\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{\frac{{1}^{3}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)} - \frac{{\left(\cos x\right)}^{3}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.02373580573239164950849477975225454429165:\\
\;\;\;\;\frac{\mathsf{log1p}\left(\log \left(e^{\mathsf{expm1}\left({1}^{3} - {\left(\cos x\right)}^{3}\right)}\right)\right)}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}\\

\mathbf{elif}\;x \le 0.02029306521166552093604629192213906208053:\\
\;\;\;\;\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{\frac{{1}^{3}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)} - \frac{{\left(\cos x\right)}^{3}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\

\end{array}
double f(double x) {
        double r65562 = 1.0;
        double r65563 = x;
        double r65564 = cos(r65563);
        double r65565 = r65562 - r65564;
        double r65566 = sin(r65563);
        double r65567 = r65565 / r65566;
        return r65567;
}

double f(double x) {
        double r65568 = x;
        double r65569 = -0.02373580573239165;
        bool r65570 = r65568 <= r65569;
        double r65571 = 1.0;
        double r65572 = 3.0;
        double r65573 = pow(r65571, r65572);
        double r65574 = cos(r65568);
        double r65575 = pow(r65574, r65572);
        double r65576 = r65573 - r65575;
        double r65577 = expm1(r65576);
        double r65578 = exp(r65577);
        double r65579 = log(r65578);
        double r65580 = log1p(r65579);
        double r65581 = r65571 * r65574;
        double r65582 = fma(r65574, r65574, r65581);
        double r65583 = fma(r65571, r65571, r65582);
        double r65584 = sin(r65568);
        double r65585 = r65583 * r65584;
        double r65586 = r65580 / r65585;
        double r65587 = 0.02029306521166552;
        bool r65588 = r65568 <= r65587;
        double r65589 = 0.041666666666666664;
        double r65590 = pow(r65568, r65572);
        double r65591 = 0.004166666666666667;
        double r65592 = 5.0;
        double r65593 = pow(r65568, r65592);
        double r65594 = 0.5;
        double r65595 = r65594 * r65568;
        double r65596 = fma(r65591, r65593, r65595);
        double r65597 = fma(r65589, r65590, r65596);
        double r65598 = r65571 + r65574;
        double r65599 = r65571 * r65571;
        double r65600 = fma(r65574, r65598, r65599);
        double r65601 = r65573 / r65600;
        double r65602 = r65575 / r65600;
        double r65603 = r65601 - r65602;
        double r65604 = r65603 / r65584;
        double r65605 = r65588 ? r65597 : r65604;
        double r65606 = r65570 ? r65586 : r65605;
        return r65606;
}

Error

Bits error versus x

Target

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

Derivation

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

    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(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}}\]
    6. Using strategy rm
    7. Applied log1p-expm1-u1.0

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

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

    if -0.02373580573239165 < x < 0.02029306521166552

    1. Initial program 60.0

      \[\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.02029306521166552 < 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. Simplified1.0

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.02373580573239164950849477975225454429165:\\ \;\;\;\;\frac{\mathsf{log1p}\left(\log \left(e^{\mathsf{expm1}\left({1}^{3} - {\left(\cos x\right)}^{3}\right)}\right)\right)}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}\\ \mathbf{elif}\;x \le 0.02029306521166552093604629192213906208053:\\ \;\;\;\;\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{\frac{{1}^{3}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)} - \frac{{\left(\cos x\right)}^{3}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\ \end{array}\]

Reproduce

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