Average Error: 29.9 → 0.6
Time: 27.6s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.02354046714369385437382042880471999524161:\\ \;\;\;\;\frac{\frac{{1}^{3} - \sqrt[3]{{\left({\left(\cos x\right)}^{3}\right)}^{3}}}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}{\sin x}\\ \mathbf{elif}\;x \le 0.02128798787360045713379363974127045366913:\\ \;\;\;\;\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(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)} - \frac{\mathsf{log1p}\left(\mathsf{expm1}\left({\left(\cos x\right)}^{3}\right)\right)}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.02354046714369385437382042880471999524161:\\
\;\;\;\;\frac{\frac{{1}^{3} - \sqrt[3]{{\left({\left(\cos x\right)}^{3}\right)}^{3}}}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}{\sin x}\\

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

\end{array}
double f(double x) {
        double r73748 = 1.0;
        double r73749 = x;
        double r73750 = cos(r73749);
        double r73751 = r73748 - r73750;
        double r73752 = sin(r73749);
        double r73753 = r73751 / r73752;
        return r73753;
}

double f(double x) {
        double r73754 = x;
        double r73755 = -0.023540467143693854;
        bool r73756 = r73754 <= r73755;
        double r73757 = 1.0;
        double r73758 = 3.0;
        double r73759 = pow(r73757, r73758);
        double r73760 = cos(r73754);
        double r73761 = pow(r73760, r73758);
        double r73762 = pow(r73761, r73758);
        double r73763 = cbrt(r73762);
        double r73764 = r73759 - r73763;
        double r73765 = r73757 + r73760;
        double r73766 = r73760 * r73765;
        double r73767 = fma(r73757, r73757, r73766);
        double r73768 = r73764 / r73767;
        double r73769 = sin(r73754);
        double r73770 = r73768 / r73769;
        double r73771 = 0.021287987873600457;
        bool r73772 = r73754 <= r73771;
        double r73773 = 0.041666666666666664;
        double r73774 = pow(r73754, r73758);
        double r73775 = 0.004166666666666667;
        double r73776 = 5.0;
        double r73777 = pow(r73754, r73776);
        double r73778 = 0.5;
        double r73779 = r73778 * r73754;
        double r73780 = fma(r73775, r73777, r73779);
        double r73781 = fma(r73773, r73774, r73780);
        double r73782 = r73759 / r73767;
        double r73783 = expm1(r73761);
        double r73784 = log1p(r73783);
        double r73785 = r73784 / r73767;
        double r73786 = r73782 - r73785;
        double r73787 = r73786 / r73769;
        double r73788 = r73772 ? r73781 : r73787;
        double r73789 = r73756 ? r73770 : r73788;
        return r73789;
}

Error

Bits error versus x

Target

Original29.9
Target0.0
Herbie0.6
\[\tan \left(\frac{x}{2}\right)\]

Derivation

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

    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(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}}{\sin x}\]
    5. Using strategy rm
    6. Applied add-cbrt-cube1.1

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

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

    if -0.023540467143693854 < x < 0.021287987873600457

    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.021287987873600457 < x

    1. Initial program 1.0

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. 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}\]
    4. Simplified1.1

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

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

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

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

Reproduce

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