Average Error: 30.8 → 0.5
Time: 7.1s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.02490049914676041933891958990443527000025:\\ \;\;\;\;\frac{\frac{\log \left(e^{{1}^{3} - {\left(\cos x\right)}^{3}}\right)}{\sin x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}\\ \mathbf{elif}\;x \le 0.02315373815002300453280703607106261188164:\\ \;\;\;\;\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}}}{\sin x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.02490049914676041933891958990443527000025:\\
\;\;\;\;\frac{\frac{\log \left(e^{{1}^{3} - {\left(\cos x\right)}^{3}}\right)}{\sin x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}\\

\mathbf{elif}\;x \le 0.02315373815002300453280703607106261188164:\\
\;\;\;\;\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}}}{\sin x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}\\

\end{array}
double f(double x) {
        double r49457 = 1.0;
        double r49458 = x;
        double r49459 = cos(r49458);
        double r49460 = r49457 - r49459;
        double r49461 = sin(r49458);
        double r49462 = r49460 / r49461;
        return r49462;
}

double f(double x) {
        double r49463 = x;
        double r49464 = -0.02490049914676042;
        bool r49465 = r49463 <= r49464;
        double r49466 = 1.0;
        double r49467 = 3.0;
        double r49468 = pow(r49466, r49467);
        double r49469 = cos(r49463);
        double r49470 = pow(r49469, r49467);
        double r49471 = r49468 - r49470;
        double r49472 = exp(r49471);
        double r49473 = log(r49472);
        double r49474 = sin(r49463);
        double r49475 = r49473 / r49474;
        double r49476 = r49466 * r49466;
        double r49477 = r49469 * r49469;
        double r49478 = r49466 * r49469;
        double r49479 = r49477 + r49478;
        double r49480 = r49476 + r49479;
        double r49481 = r49475 / r49480;
        double r49482 = 0.023153738150023005;
        bool r49483 = r49463 <= r49482;
        double r49484 = 0.041666666666666664;
        double r49485 = pow(r49463, r49467);
        double r49486 = 0.004166666666666667;
        double r49487 = 5.0;
        double r49488 = pow(r49463, r49487);
        double r49489 = 0.5;
        double r49490 = r49489 * r49463;
        double r49491 = fma(r49486, r49488, r49490);
        double r49492 = fma(r49484, r49485, r49491);
        double r49493 = pow(r49470, r49467);
        double r49494 = cbrt(r49493);
        double r49495 = r49468 - r49494;
        double r49496 = r49495 / r49474;
        double r49497 = r49496 / r49480;
        double r49498 = r49483 ? r49492 : r49497;
        double r49499 = r49465 ? r49481 : r49498;
        return r49499;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied div-inv0.9

      \[\leadsto \color{blue}{\left(1 - \cos x\right) \cdot \frac{1}{\sin x}}\]
    4. Using strategy rm
    5. Applied flip3--1.0

      \[\leadsto \color{blue}{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}} \cdot \frac{1}{\sin x}\]
    6. Applied associate-*l/1.0

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

      \[\leadsto \frac{\color{blue}{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\sin x}}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}\]
    8. Using strategy rm
    9. Applied add-log-exp1.0

      \[\leadsto \frac{\frac{{1}^{3} - \color{blue}{\log \left(e^{{\left(\cos x\right)}^{3}}\right)}}{\sin x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}\]
    10. Applied add-log-exp1.0

      \[\leadsto \frac{\frac{\color{blue}{\log \left(e^{{1}^{3}}\right)} - \log \left(e^{{\left(\cos x\right)}^{3}}\right)}{\sin x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}\]
    11. Applied diff-log1.1

      \[\leadsto \frac{\frac{\color{blue}{\log \left(\frac{e^{{1}^{3}}}{e^{{\left(\cos x\right)}^{3}}}\right)}}{\sin x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}\]
    12. Simplified1.0

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

    if -0.02490049914676042 < x < 0.023153738150023005

    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)}\]

    if 0.023153738150023005 < x

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied div-inv0.9

      \[\leadsto \color{blue}{\left(1 - \cos x\right) \cdot \frac{1}{\sin x}}\]
    4. Using strategy rm
    5. Applied flip3--1.0

      \[\leadsto \color{blue}{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}} \cdot \frac{1}{\sin x}\]
    6. Applied associate-*l/1.0

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

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

      \[\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}}}}{\sin x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}\]
    10. Simplified1.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.02490049914676041933891958990443527000025:\\ \;\;\;\;\frac{\frac{\log \left(e^{{1}^{3} - {\left(\cos x\right)}^{3}}\right)}{\sin x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}\\ \mathbf{elif}\;x \le 0.02315373815002300453280703607106261188164:\\ \;\;\;\;\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}}}{\sin x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}\\ \end{array}\]

Reproduce

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