Average Error: 30.7 → 0.5
Time: 22.6s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.01854364127689415303601450091264268849045 \lor \neg \left(x \le 0.02331429387269752864786376278516399906948\right):\\ \;\;\;\;\frac{{e}^{\left(\log \left(1 - \cos x\right)\right)}}{\sin x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.01854364127689415303601450091264268849045 \lor \neg \left(x \le 0.02331429387269752864786376278516399906948\right):\\
\;\;\;\;\frac{{e}^{\left(\log \left(1 - \cos x\right)\right)}}{\sin x}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\

\end{array}
double f(double x) {
        double r64435 = 1.0;
        double r64436 = x;
        double r64437 = cos(r64436);
        double r64438 = r64435 - r64437;
        double r64439 = sin(r64436);
        double r64440 = r64438 / r64439;
        return r64440;
}

double f(double x) {
        double r64441 = x;
        double r64442 = -0.018543641276894153;
        bool r64443 = r64441 <= r64442;
        double r64444 = 0.02331429387269753;
        bool r64445 = r64441 <= r64444;
        double r64446 = !r64445;
        bool r64447 = r64443 || r64446;
        double r64448 = exp(1.0);
        double r64449 = 1.0;
        double r64450 = cos(r64441);
        double r64451 = r64449 - r64450;
        double r64452 = log(r64451);
        double r64453 = pow(r64448, r64452);
        double r64454 = sin(r64441);
        double r64455 = r64453 / r64454;
        double r64456 = 0.041666666666666664;
        double r64457 = 3.0;
        double r64458 = pow(r64441, r64457);
        double r64459 = 0.004166666666666667;
        double r64460 = 5.0;
        double r64461 = pow(r64441, r64460);
        double r64462 = 0.5;
        double r64463 = r64462 * r64441;
        double r64464 = fma(r64459, r64461, r64463);
        double r64465 = fma(r64456, r64458, r64464);
        double r64466 = r64447 ? r64455 : r64465;
        return r64466;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 2 regimes
  2. if x < -0.018543641276894153 or 0.02331429387269753 < x

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied add-exp-log0.9

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

      \[\leadsto \frac{e^{\log \color{blue}{\left({\left(1 - \cos x\right)}^{1}\right)}}}{\sin x}\]
    6. Applied log-pow0.9

      \[\leadsto \frac{e^{\color{blue}{1 \cdot \log \left(1 - \cos x\right)}}}{\sin x}\]
    7. Applied exp-prod1.0

      \[\leadsto \frac{\color{blue}{{\left(e^{1}\right)}^{\left(\log \left(1 - \cos x\right)\right)}}}{\sin x}\]
    8. Simplified1.0

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

    if -0.018543641276894153 < x < 0.02331429387269753

    1. Initial program 59.9

      \[\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)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.5

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

Reproduce

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