Average Error: 30.0 → 0.8
Time: 9.1s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0196280106623787261:\\ \;\;\;\;\frac{\frac{\mathsf{log1p}\left(\mathsf{expm1}\left({1}^{3} - {\left(\cos x\right)}^{3}\right)\right)}{\mathsf{fma}\left(\cos x, \frac{{1}^{3} + {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x - 1\right)\right)}, 1 \cdot 1\right)}}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le -0.0:\\ \;\;\;\;\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}:\\ \;\;\;\;\frac{\frac{{1}^{3} - \sqrt[3]{{\left({\left(\cos x\right)}^{3}\right)}^{3}}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0196280106623787261:\\
\;\;\;\;\frac{\frac{\mathsf{log1p}\left(\mathsf{expm1}\left({1}^{3} - {\left(\cos x\right)}^{3}\right)\right)}{\mathsf{fma}\left(\cos x, \frac{{1}^{3} + {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x - 1\right)\right)}, 1 \cdot 1\right)}}{\sin x}\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le -0.0:\\
\;\;\;\;\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}:\\
\;\;\;\;\frac{\frac{{1}^{3} - \sqrt[3]{{\left({\left(\cos x\right)}^{3}\right)}^{3}}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\

\end{array}
double f(double x) {
        double r57354 = 1.0;
        double r57355 = x;
        double r57356 = cos(r57355);
        double r57357 = r57354 - r57356;
        double r57358 = sin(r57355);
        double r57359 = r57357 / r57358;
        return r57359;
}

double f(double x) {
        double r57360 = 1.0;
        double r57361 = x;
        double r57362 = cos(r57361);
        double r57363 = r57360 - r57362;
        double r57364 = sin(r57361);
        double r57365 = r57363 / r57364;
        double r57366 = -0.019628010662378726;
        bool r57367 = r57365 <= r57366;
        double r57368 = 3.0;
        double r57369 = pow(r57360, r57368);
        double r57370 = pow(r57362, r57368);
        double r57371 = r57369 - r57370;
        double r57372 = expm1(r57371);
        double r57373 = log1p(r57372);
        double r57374 = r57369 + r57370;
        double r57375 = r57362 - r57360;
        double r57376 = r57362 * r57375;
        double r57377 = fma(r57360, r57360, r57376);
        double r57378 = r57374 / r57377;
        double r57379 = r57360 * r57360;
        double r57380 = fma(r57362, r57378, r57379);
        double r57381 = r57373 / r57380;
        double r57382 = r57381 / r57364;
        double r57383 = -0.0;
        bool r57384 = r57365 <= r57383;
        double r57385 = 0.041666666666666664;
        double r57386 = pow(r57361, r57368);
        double r57387 = 0.004166666666666667;
        double r57388 = 5.0;
        double r57389 = pow(r57361, r57388);
        double r57390 = 0.5;
        double r57391 = r57390 * r57361;
        double r57392 = fma(r57387, r57389, r57391);
        double r57393 = fma(r57385, r57386, r57392);
        double r57394 = pow(r57370, r57368);
        double r57395 = cbrt(r57394);
        double r57396 = r57369 - r57395;
        double r57397 = r57360 + r57362;
        double r57398 = fma(r57362, r57397, r57379);
        double r57399 = r57396 / r57398;
        double r57400 = r57399 / r57364;
        double r57401 = r57384 ? r57393 : r57400;
        double r57402 = r57367 ? r57382 : r57401;
        return r57402;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.8

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

      \[\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. Simplified0.9

      \[\leadsto \frac{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\color{blue}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}}{\sin x}\]
    5. Using strategy rm
    6. Applied log1p-expm1-u0.9

      \[\leadsto \frac{\frac{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left({1}^{3} - {\left(\cos x\right)}^{3}\right)\right)}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\]
    7. Using strategy rm
    8. Applied flip3-+0.9

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

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

    if -0.019628010662378726 < (/ (- 1.0 (cos x)) (sin x)) < -0.0

    1. Initial program 59.9

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

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

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

    1. Initial program 1.4

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

      \[\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.5

      \[\leadsto \frac{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\color{blue}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}}{\sin x}\]
    5. Using strategy rm
    6. Applied add-cbrt-cube1.5

      \[\leadsto \frac{\frac{{1}^{3} - \color{blue}{\sqrt[3]{\left({\left(\cos x\right)}^{3} \cdot {\left(\cos x\right)}^{3}\right) \cdot {\left(\cos x\right)}^{3}}}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\]
    7. Simplified1.6

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0196280106623787261:\\ \;\;\;\;\frac{\frac{\mathsf{log1p}\left(\mathsf{expm1}\left({1}^{3} - {\left(\cos x\right)}^{3}\right)\right)}{\mathsf{fma}\left(\cos x, \frac{{1}^{3} + {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x - 1\right)\right)}, 1 \cdot 1\right)}}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le -0.0:\\ \;\;\;\;\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}:\\ \;\;\;\;\frac{\frac{{1}^{3} - \sqrt[3]{{\left({\left(\cos x\right)}^{3}\right)}^{3}}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\ \end{array}\]

Reproduce

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