Average Error: 30.1 → 0.5
Time: 14.2s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.020168669770568547:\\ \;\;\;\;\frac{{1}^{3} - \mathsf{log1p}\left(\mathsf{expm1}\left({\left(\cos x\right)}^{3}\right)\right)}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}\\ \mathbf{elif}\;x \le 0.0257297152168081099:\\ \;\;\;\;\mathsf{fma}\left(0.04166666666666663, {x}^{3}, \mathsf{fma}\left(0.004166666666666624, {x}^{5}, 0.5 \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sin x} - \frac{\cos x}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.020168669770568547:\\
\;\;\;\;\frac{{1}^{3} - \mathsf{log1p}\left(\mathsf{expm1}\left({\left(\cos x\right)}^{3}\right)\right)}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}\\

\mathbf{elif}\;x \le 0.0257297152168081099:\\
\;\;\;\;\mathsf{fma}\left(0.04166666666666663, {x}^{3}, \mathsf{fma}\left(0.004166666666666624, {x}^{5}, 0.5 \cdot x\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\sin x} - \frac{\cos x}{\sin x}\\

\end{array}
double f(double x) {
        double r108662 = 1.0;
        double r108663 = x;
        double r108664 = cos(r108663);
        double r108665 = r108662 - r108664;
        double r108666 = sin(r108663);
        double r108667 = r108665 / r108666;
        return r108667;
}

double f(double x) {
        double r108668 = x;
        double r108669 = -0.020168669770568547;
        bool r108670 = r108668 <= r108669;
        double r108671 = 1.0;
        double r108672 = 3.0;
        double r108673 = pow(r108671, r108672);
        double r108674 = cos(r108668);
        double r108675 = pow(r108674, r108672);
        double r108676 = expm1(r108675);
        double r108677 = log1p(r108676);
        double r108678 = r108673 - r108677;
        double r108679 = sin(r108668);
        double r108680 = r108671 + r108674;
        double r108681 = r108674 * r108680;
        double r108682 = fma(r108671, r108671, r108681);
        double r108683 = r108679 * r108682;
        double r108684 = r108678 / r108683;
        double r108685 = 0.02572971521680811;
        bool r108686 = r108668 <= r108685;
        double r108687 = 0.04166666666666663;
        double r108688 = pow(r108668, r108672);
        double r108689 = 0.004166666666666624;
        double r108690 = 5.0;
        double r108691 = pow(r108668, r108690);
        double r108692 = 0.5;
        double r108693 = r108692 * r108668;
        double r108694 = fma(r108689, r108691, r108693);
        double r108695 = fma(r108687, r108688, r108694);
        double r108696 = r108671 / r108679;
        double r108697 = r108674 / r108679;
        double r108698 = r108696 - r108697;
        double r108699 = r108686 ? r108695 : r108698;
        double r108700 = r108670 ? r108684 : r108699;
        return r108700;
}

Error

Bits error versus x

Target

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

Derivation

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

    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 log1p-expm1-u1.0

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

    if -0.020168669770568547 < x < 0.02572971521680811

    1. Initial program 59.8

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

      \[\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/59.8

      \[\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. Simplified59.8

      \[\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. Taylor expanded around 0 0.0

      \[\leadsto \color{blue}{0.04166666666666663 \cdot {x}^{3} + \left(0.004166666666666624 \cdot {x}^{5} + 0.5 \cdot x\right)}\]
    7. Simplified0.0

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.04166666666666663, {x}^{3}, \mathsf{fma}\left(0.004166666666666624, {x}^{5}, 0.5 \cdot x\right)\right)}\]

    if 0.02572971521680811 < x

    1. Initial program 0.9

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

      \[\leadsto \color{blue}{\frac{1}{\sin x} - \frac{\cos x}{\sin x}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.020168669770568547:\\ \;\;\;\;\frac{{1}^{3} - \mathsf{log1p}\left(\mathsf{expm1}\left({\left(\cos x\right)}^{3}\right)\right)}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}\\ \mathbf{elif}\;x \le 0.0257297152168081099:\\ \;\;\;\;\mathsf{fma}\left(0.04166666666666663, {x}^{3}, \mathsf{fma}\left(0.004166666666666624, {x}^{5}, 0.5 \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sin x} - \frac{\cos x}{\sin x}\\ \end{array}\]

Reproduce

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