Average Error: 30.4 → 0.6
Time: 11.9s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.004893422048095925:\\ \;\;\;\;\log \left(\mathsf{expm1}\left(\mathsf{log1p}\left(e^{\frac{1 - \cos x}{\sin x}}\right)\right)\right)\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 2.98402005876572314 \cdot 10^{-5}:\\ \;\;\;\;\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}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1 - \cos x}{\sin x}\right)\right)\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.004893422048095925:\\
\;\;\;\;\log \left(\mathsf{expm1}\left(\mathsf{log1p}\left(e^{\frac{1 - \cos x}{\sin x}}\right)\right)\right)\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 2.98402005876572314 \cdot 10^{-5}:\\
\;\;\;\;\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}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1 - \cos x}{\sin x}\right)\right)\\

\end{array}
double f(double x) {
        double r106597 = 1.0;
        double r106598 = x;
        double r106599 = cos(r106598);
        double r106600 = r106597 - r106599;
        double r106601 = sin(r106598);
        double r106602 = r106600 / r106601;
        return r106602;
}

double f(double x) {
        double r106603 = 1.0;
        double r106604 = x;
        double r106605 = cos(r106604);
        double r106606 = r106603 - r106605;
        double r106607 = sin(r106604);
        double r106608 = r106606 / r106607;
        double r106609 = -0.0048934220480959245;
        bool r106610 = r106608 <= r106609;
        double r106611 = exp(r106608);
        double r106612 = log1p(r106611);
        double r106613 = expm1(r106612);
        double r106614 = log(r106613);
        double r106615 = 2.984020058765723e-05;
        bool r106616 = r106608 <= r106615;
        double r106617 = 0.041666666666666664;
        double r106618 = 3.0;
        double r106619 = pow(r106604, r106618);
        double r106620 = 0.004166666666666667;
        double r106621 = 5.0;
        double r106622 = pow(r106604, r106621);
        double r106623 = 0.5;
        double r106624 = r106623 * r106604;
        double r106625 = fma(r106620, r106622, r106624);
        double r106626 = fma(r106617, r106619, r106625);
        double r106627 = expm1(r106608);
        double r106628 = log1p(r106627);
        double r106629 = r106616 ? r106626 : r106628;
        double r106630 = r106610 ? r106614 : r106629;
        return r106630;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 3 regimes
  2. if (/ (- 1.0 (cos x)) (sin x)) < -0.0048934220480959245

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied add-log-exp1.0

      \[\leadsto \color{blue}{\log \left(e^{\frac{1 - \cos x}{\sin x}}\right)}\]
    4. Using strategy rm
    5. Applied expm1-log1p-u1.1

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

    if -0.0048934220480959245 < (/ (- 1.0 (cos x)) (sin x)) < 2.984020058765723e-05

    1. Initial program 59.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Taylor expanded around 0 0.1

      \[\leadsto \color{blue}{\frac{1}{24} \cdot {x}^{3} + \left(\frac{1}{240} \cdot {x}^{5} + \frac{1}{2} \cdot x\right)}\]
    3. Simplified0.1

      \[\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 2.984020058765723e-05 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 1.1

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied log1p-expm1-u1.1

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

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

Reproduce

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