Average Error: 30.2 → 0.5
Time: 25.9s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.02066634150666165486986969312965811695904:\\ \;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \left(\cos x \cdot \cos x\right) \cdot \cos x}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\ \mathbf{elif}\;x \le 0.02306599559720158912345944202115788357332:\\ \;\;\;\;\mathsf{fma}\left({x}^{5}, \frac{1}{240}, x \cdot \left(\frac{1}{2} + \left(x \cdot x\right) \cdot \frac{1}{24}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{\log \left(\frac{1 \cdot \left(1 \cdot 1\right) - \log \left(e^{\cos x \cdot \cos x}\right) \cdot \cos x}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}\right)}}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.02066634150666165486986969312965811695904:\\
\;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \left(\cos x \cdot \cos x\right) \cdot \cos x}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\

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

\mathbf{else}:\\
\;\;\;\;\frac{e^{\log \left(\frac{1 \cdot \left(1 \cdot 1\right) - \log \left(e^{\cos x \cdot \cos x}\right) \cdot \cos x}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}\right)}}{\sin x}\\

\end{array}
double f(double x) {
        double r2876430 = 1.0;
        double r2876431 = x;
        double r2876432 = cos(r2876431);
        double r2876433 = r2876430 - r2876432;
        double r2876434 = sin(r2876431);
        double r2876435 = r2876433 / r2876434;
        return r2876435;
}

double f(double x) {
        double r2876436 = x;
        double r2876437 = -0.020666341506661655;
        bool r2876438 = r2876436 <= r2876437;
        double r2876439 = 1.0;
        double r2876440 = r2876439 * r2876439;
        double r2876441 = r2876439 * r2876440;
        double r2876442 = cos(r2876436);
        double r2876443 = r2876442 * r2876442;
        double r2876444 = r2876443 * r2876442;
        double r2876445 = r2876441 - r2876444;
        double r2876446 = r2876439 + r2876442;
        double r2876447 = fma(r2876442, r2876446, r2876440);
        double r2876448 = r2876445 / r2876447;
        double r2876449 = sin(r2876436);
        double r2876450 = r2876448 / r2876449;
        double r2876451 = 0.02306599559720159;
        bool r2876452 = r2876436 <= r2876451;
        double r2876453 = 5.0;
        double r2876454 = pow(r2876436, r2876453);
        double r2876455 = 0.004166666666666667;
        double r2876456 = 0.5;
        double r2876457 = r2876436 * r2876436;
        double r2876458 = 0.041666666666666664;
        double r2876459 = r2876457 * r2876458;
        double r2876460 = r2876456 + r2876459;
        double r2876461 = r2876436 * r2876460;
        double r2876462 = fma(r2876454, r2876455, r2876461);
        double r2876463 = exp(r2876443);
        double r2876464 = log(r2876463);
        double r2876465 = r2876464 * r2876442;
        double r2876466 = r2876441 - r2876465;
        double r2876467 = r2876466 / r2876447;
        double r2876468 = log(r2876467);
        double r2876469 = exp(r2876468);
        double r2876470 = r2876469 / r2876449;
        double r2876471 = r2876452 ? r2876462 : r2876470;
        double r2876472 = r2876438 ? r2876450 : r2876471;
        return r2876472;
}

Error

Bits error versus x

Target

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

Derivation

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

    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. Simplified1.0

      \[\leadsto \frac{\frac{\color{blue}{1 \cdot \left(1 \cdot 1\right) - \left(\cos x \cdot \cos x\right) \cdot \cos x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\]
    5. Simplified1.0

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

    if -0.020666341506661655 < x < 0.02306599559720159

    1. Initial program 59.9

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

    if 0.02306599559720159 < 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. Simplified1.0

      \[\leadsto \frac{\frac{\color{blue}{1 \cdot \left(1 \cdot 1\right) - \left(\cos x \cdot \cos x\right) \cdot \cos x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\]
    5. Simplified1.0

      \[\leadsto \frac{\frac{1 \cdot \left(1 \cdot 1\right) - \left(\cos x \cdot \cos x\right) \cdot \cos x}{\color{blue}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right)}}}{\sin x}\]
    6. Using strategy rm
    7. Applied add-exp-log1.0

      \[\leadsto \frac{\frac{1 \cdot \left(1 \cdot 1\right) - \left(\cos x \cdot \cos x\right) \cdot \cos x}{\color{blue}{e^{\log \left(\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right)\right)}}}}{\sin x}\]
    8. Applied add-exp-log1.0

      \[\leadsto \frac{\frac{\color{blue}{e^{\log \left(1 \cdot \left(1 \cdot 1\right) - \left(\cos x \cdot \cos x\right) \cdot \cos x\right)}}}{e^{\log \left(\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right)\right)}}}{\sin x}\]
    9. Applied div-exp1.0

      \[\leadsto \frac{\color{blue}{e^{\log \left(1 \cdot \left(1 \cdot 1\right) - \left(\cos x \cdot \cos x\right) \cdot \cos x\right) - \log \left(\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right)\right)}}}{\sin x}\]
    10. Simplified1.0

      \[\leadsto \frac{e^{\color{blue}{\log \left(\frac{1 \cdot \left(1 \cdot 1\right) - \left(\cos x \cdot \cos x\right) \cdot \cos x}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}\right)}}}{\sin x}\]
    11. Using strategy rm
    12. Applied add-log-exp1.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.02066634150666165486986969312965811695904:\\ \;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \left(\cos x \cdot \cos x\right) \cdot \cos x}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\ \mathbf{elif}\;x \le 0.02306599559720158912345944202115788357332:\\ \;\;\;\;\mathsf{fma}\left({x}^{5}, \frac{1}{240}, x \cdot \left(\frac{1}{2} + \left(x \cdot x\right) \cdot \frac{1}{24}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{\log \left(\frac{1 \cdot \left(1 \cdot 1\right) - \log \left(e^{\cos x \cdot \cos x}\right) \cdot \cos x}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}\right)}}{\sin x}\\ \end{array}\]

Reproduce

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