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

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

\end{array}
double f(double x) {
        double r45426 = 1.0;
        double r45427 = x;
        double r45428 = cos(r45427);
        double r45429 = r45426 - r45428;
        double r45430 = sin(r45427);
        double r45431 = r45429 / r45430;
        return r45431;
}

double f(double x) {
        double r45432 = x;
        double r45433 = -0.022988205514711212;
        bool r45434 = r45432 <= r45433;
        double r45435 = 0.018056437645311372;
        bool r45436 = r45432 <= r45435;
        double r45437 = !r45436;
        bool r45438 = r45434 || r45437;
        double r45439 = 1.0;
        double r45440 = cos(r45432);
        double r45441 = r45439 - r45440;
        double r45442 = sin(r45432);
        double r45443 = r45441 / r45442;
        double r45444 = 0.041666666666666664;
        double r45445 = 3.0;
        double r45446 = pow(r45432, r45445);
        double r45447 = 5.0;
        double r45448 = pow(r45432, r45447);
        double r45449 = 0.004166666666666667;
        double r45450 = 0.5;
        double r45451 = r45432 * r45450;
        double r45452 = fma(r45448, r45449, r45451);
        double r45453 = fma(r45444, r45446, r45452);
        double r45454 = r45438 ? r45443 : r45453;
        return r45454;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 2 regimes
  2. if x < -0.022988205514711212 or 0.018056437645311372 < x

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied *-un-lft-identity0.9

      \[\leadsto \frac{1 - \cos x}{\color{blue}{1 \cdot \sin x}}\]
    4. Applied *-un-lft-identity0.9

      \[\leadsto \frac{\color{blue}{1 \cdot \left(1 - \cos x\right)}}{1 \cdot \sin x}\]
    5. Applied times-frac0.9

      \[\leadsto \color{blue}{\frac{1}{1} \cdot \frac{1 - \cos x}{\sin x}}\]
    6. Simplified0.9

      \[\leadsto \color{blue}{1} \cdot \frac{1 - \cos x}{\sin x}\]

    if -0.022988205514711212 < x < 0.018056437645311372

    1. Initial program 59.8

      \[\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(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left({x}^{5}, \frac{1}{240}, \frac{1}{2} \cdot x\right)\right)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.4

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

Reproduce

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