Average Error: 30.7 → 0.6
Time: 26.1s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.02707534624189289820805903730160935083404:\\ \;\;\;\;\frac{1}{\sin x} - \frac{\cos x}{\sin x}\\ \mathbf{elif}\;x \le 0.02105783752125852878456235828252829378471:\\ \;\;\;\;\mathsf{fma}\left(x, \mathsf{fma}\left(x, \frac{1}{24} \cdot x, \frac{1}{2}\right), \frac{1}{240} \cdot {x}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{{1}^{3} - \sqrt[3]{\left(\left(\left(\cos x \cdot \cos x\right) \cdot \left(\cos x \cdot \cos x\right)\right) \cdot \left(\cos x \cdot \cos x\right)\right) \cdot \left(\cos x \cdot \left(\cos x \cdot \cos x\right)\right)}}{\sin x \cdot \mathsf{fma}\left(\frac{\mathsf{fma}\left(1 \cdot 1, 1, \cos x \cdot \left(\cos x \cdot \cos x\right)\right)}{\mathsf{fma}\left(1, 1 - \cos x, \cos x \cdot \cos x\right)}, \cos x, 1 \cdot 1\right)}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.02707534624189289820805903730160935083404:\\
\;\;\;\;\frac{1}{\sin x} - \frac{\cos x}{\sin x}\\

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

\mathbf{else}:\\
\;\;\;\;\frac{{1}^{3} - \sqrt[3]{\left(\left(\left(\cos x \cdot \cos x\right) \cdot \left(\cos x \cdot \cos x\right)\right) \cdot \left(\cos x \cdot \cos x\right)\right) \cdot \left(\cos x \cdot \left(\cos x \cdot \cos x\right)\right)}}{\sin x \cdot \mathsf{fma}\left(\frac{\mathsf{fma}\left(1 \cdot 1, 1, \cos x \cdot \left(\cos x \cdot \cos x\right)\right)}{\mathsf{fma}\left(1, 1 - \cos x, \cos x \cdot \cos x\right)}, \cos x, 1 \cdot 1\right)}\\

\end{array}
double f(double x) {
        double r2818845 = 1.0;
        double r2818846 = x;
        double r2818847 = cos(r2818846);
        double r2818848 = r2818845 - r2818847;
        double r2818849 = sin(r2818846);
        double r2818850 = r2818848 / r2818849;
        return r2818850;
}

double f(double x) {
        double r2818851 = x;
        double r2818852 = -0.027075346241892898;
        bool r2818853 = r2818851 <= r2818852;
        double r2818854 = 1.0;
        double r2818855 = sin(r2818851);
        double r2818856 = r2818854 / r2818855;
        double r2818857 = cos(r2818851);
        double r2818858 = r2818857 / r2818855;
        double r2818859 = r2818856 - r2818858;
        double r2818860 = 0.02105783752125853;
        bool r2818861 = r2818851 <= r2818860;
        double r2818862 = 0.041666666666666664;
        double r2818863 = r2818862 * r2818851;
        double r2818864 = 0.5;
        double r2818865 = fma(r2818851, r2818863, r2818864);
        double r2818866 = 0.004166666666666667;
        double r2818867 = 5.0;
        double r2818868 = pow(r2818851, r2818867);
        double r2818869 = r2818866 * r2818868;
        double r2818870 = fma(r2818851, r2818865, r2818869);
        double r2818871 = 3.0;
        double r2818872 = pow(r2818854, r2818871);
        double r2818873 = r2818857 * r2818857;
        double r2818874 = r2818873 * r2818873;
        double r2818875 = r2818874 * r2818873;
        double r2818876 = r2818857 * r2818873;
        double r2818877 = r2818875 * r2818876;
        double r2818878 = cbrt(r2818877);
        double r2818879 = r2818872 - r2818878;
        double r2818880 = r2818854 * r2818854;
        double r2818881 = fma(r2818880, r2818854, r2818876);
        double r2818882 = r2818854 - r2818857;
        double r2818883 = fma(r2818854, r2818882, r2818873);
        double r2818884 = r2818881 / r2818883;
        double r2818885 = fma(r2818884, r2818857, r2818880);
        double r2818886 = r2818855 * r2818885;
        double r2818887 = r2818879 / r2818886;
        double r2818888 = r2818861 ? r2818870 : r2818887;
        double r2818889 = r2818853 ? r2818859 : r2818888;
        return r2818889;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.9

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

      \[\leadsto \color{blue}{\frac{1}{\sin x} - \frac{\cos x}{\sin x}}\]

    if -0.027075346241892898 < x < 0.02105783752125853

    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, \mathsf{fma}\left(x, \frac{1}{24} \cdot x, \frac{1}{2}\right), \frac{1}{240} \cdot {x}^{5}\right)}\]

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

      \[\leadsto \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}}}}{\sin x \cdot \mathsf{fma}\left(\cos x + 1, \cos x, 1 \cdot 1\right)}\]
    8. Simplified1.1

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.02707534624189289820805903730160935083404:\\ \;\;\;\;\frac{1}{\sin x} - \frac{\cos x}{\sin x}\\ \mathbf{elif}\;x \le 0.02105783752125852878456235828252829378471:\\ \;\;\;\;\mathsf{fma}\left(x, \mathsf{fma}\left(x, \frac{1}{24} \cdot x, \frac{1}{2}\right), \frac{1}{240} \cdot {x}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{{1}^{3} - \sqrt[3]{\left(\left(\left(\cos x \cdot \cos x\right) \cdot \left(\cos x \cdot \cos x\right)\right) \cdot \left(\cos x \cdot \cos x\right)\right) \cdot \left(\cos x \cdot \left(\cos x \cdot \cos x\right)\right)}}{\sin x \cdot \mathsf{fma}\left(\frac{\mathsf{fma}\left(1 \cdot 1, 1, \cos x \cdot \left(\cos x \cdot \cos x\right)\right)}{\mathsf{fma}\left(1, 1 - \cos x, \cos x \cdot \cos x\right)}, \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)))