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

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

\end{array}
double f(double x) {
        double r66918 = 1.0;
        double r66919 = x;
        double r66920 = cos(r66919);
        double r66921 = r66918 - r66920;
        double r66922 = sin(r66919);
        double r66923 = r66921 / r66922;
        return r66923;
}

double f(double x) {
        double r66924 = x;
        double r66925 = -0.024087893247376972;
        bool r66926 = r66924 <= r66925;
        double r66927 = 1.0;
        double r66928 = sin(r66924);
        double r66929 = 1.0;
        double r66930 = cos(r66924);
        double r66931 = r66929 - r66930;
        double r66932 = log1p(r66931);
        double r66933 = expm1(r66932);
        double r66934 = r66928 / r66933;
        double r66935 = r66927 / r66934;
        double r66936 = 0.020706483323998073;
        bool r66937 = r66924 <= r66936;
        double r66938 = 0.041666666666666664;
        double r66939 = 3.0;
        double r66940 = pow(r66924, r66939);
        double r66941 = 0.004166666666666667;
        double r66942 = 5.0;
        double r66943 = pow(r66924, r66942);
        double r66944 = 0.5;
        double r66945 = r66944 * r66924;
        double r66946 = fma(r66941, r66943, r66945);
        double r66947 = fma(r66938, r66940, r66946);
        double r66948 = pow(r66929, r66939);
        double r66949 = pow(r66930, r66939);
        double r66950 = pow(r66949, r66939);
        double r66951 = cbrt(r66950);
        double r66952 = r66948 - r66951;
        double r66953 = r66929 + r66930;
        double r66954 = r66930 * r66953;
        double r66955 = fma(r66929, r66929, r66954);
        double r66956 = r66928 * r66955;
        double r66957 = r66952 / r66956;
        double r66958 = r66937 ? r66947 : r66957;
        double r66959 = r66926 ? r66935 : r66958;
        return r66959;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied clear-num1.0

      \[\leadsto \color{blue}{\frac{1}{\frac{\sin x}{1 - \cos x}}}\]
    4. Using strategy rm
    5. Applied expm1-log1p-u1.1

      \[\leadsto \frac{1}{\frac{\sin x}{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 - \cos x\right)\right)}}}\]

    if -0.024087893247376972 < x < 0.020706483323998073

    1. Initial program 59.8

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

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

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

Reproduce

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