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

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

\end{array}
double f(double x) {
        double r68593 = 1.0;
        double r68594 = x;
        double r68595 = cos(r68594);
        double r68596 = r68593 - r68595;
        double r68597 = sin(r68594);
        double r68598 = r68596 / r68597;
        return r68598;
}

double f(double x) {
        double r68599 = x;
        double r68600 = -0.02503568567500278;
        bool r68601 = r68599 <= r68600;
        double r68602 = 1.0;
        double r68603 = sin(r68599);
        double r68604 = 1.0;
        double r68605 = 3.0;
        double r68606 = pow(r68604, r68605);
        double r68607 = cos(r68599);
        double r68608 = pow(r68607, r68605);
        double r68609 = pow(r68608, r68605);
        double r68610 = cbrt(r68609);
        double r68611 = log1p(r68610);
        double r68612 = expm1(r68611);
        double r68613 = r68606 - r68612;
        double r68614 = r68603 / r68613;
        double r68615 = r68604 * r68604;
        double r68616 = r68607 * r68607;
        double r68617 = r68604 * r68607;
        double r68618 = r68616 + r68617;
        double r68619 = r68615 + r68618;
        double r68620 = r68614 * r68619;
        double r68621 = r68602 / r68620;
        double r68622 = 0.02321496743503862;
        bool r68623 = r68599 <= r68622;
        double r68624 = 0.041666666666666664;
        double r68625 = pow(r68599, r68605);
        double r68626 = 0.004166666666666667;
        double r68627 = 5.0;
        double r68628 = pow(r68599, r68627);
        double r68629 = 0.5;
        double r68630 = r68629 * r68599;
        double r68631 = fma(r68626, r68628, r68630);
        double r68632 = fma(r68624, r68625, r68631);
        double r68633 = r68606 - r68608;
        double r68634 = exp(r68633);
        double r68635 = log(r68634);
        double r68636 = r68603 / r68635;
        double r68637 = r68636 * r68619;
        double r68638 = r68602 / r68637;
        double r68639 = r68623 ? r68632 : r68638;
        double r68640 = r68601 ? r68621 : r68639;
        return r68640;
}

Error

Bits error versus x

Target

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

Derivation

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

    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 flip3--1.0

      \[\leadsto \frac{1}{\frac{\sin x}{\color{blue}{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}}}\]
    6. Applied associate-/r/1.0

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

      \[\leadsto \frac{1}{\frac{\sin x}{{1}^{3} - \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left({\left(\cos x\right)}^{3}\right)\right)}} \cdot \left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right)}\]
    9. Using strategy rm
    10. Applied add-cbrt-cube1.1

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

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

    if -0.02503568567500278 < x < 0.02321496743503862

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

    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 flip3--1.1

      \[\leadsto \frac{1}{\frac{\sin x}{\color{blue}{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}}}\]
    6. Applied associate-/r/1.0

      \[\leadsto \frac{1}{\color{blue}{\frac{\sin x}{{1}^{3} - {\left(\cos x\right)}^{3}} \cdot \left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right)}}\]
    7. Using strategy rm
    8. Applied add-log-exp1.1

      \[\leadsto \frac{1}{\frac{\sin x}{{1}^{3} - \color{blue}{\log \left(e^{{\left(\cos x\right)}^{3}}\right)}} \cdot \left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right)}\]
    9. Applied add-log-exp1.1

      \[\leadsto \frac{1}{\frac{\sin x}{\color{blue}{\log \left(e^{{1}^{3}}\right)} - \log \left(e^{{\left(\cos x\right)}^{3}}\right)} \cdot \left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right)}\]
    10. Applied diff-log1.2

      \[\leadsto \frac{1}{\frac{\sin x}{\color{blue}{\log \left(\frac{e^{{1}^{3}}}{e^{{\left(\cos x\right)}^{3}}}\right)}} \cdot \left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right)}\]
    11. Simplified1.1

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

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

Reproduce

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