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

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

\end{array}
double f(double x) {
        double r60497 = 1.0;
        double r60498 = x;
        double r60499 = cos(r60498);
        double r60500 = r60497 - r60499;
        double r60501 = sin(r60498);
        double r60502 = r60500 / r60501;
        return r60502;
}

double f(double x) {
        double r60503 = x;
        double r60504 = -0.024087893247376972;
        bool r60505 = r60503 <= r60504;
        double r60506 = 1.0;
        double r60507 = sin(r60503);
        double r60508 = 1.0;
        double r60509 = cos(r60503);
        double r60510 = r60508 - r60509;
        double r60511 = log1p(r60510);
        double r60512 = expm1(r60511);
        double r60513 = r60507 / r60512;
        double r60514 = r60506 / r60513;
        double r60515 = 0.020706483323998073;
        bool r60516 = r60503 <= r60515;
        double r60517 = 0.041666666666666664;
        double r60518 = 3.0;
        double r60519 = pow(r60503, r60518);
        double r60520 = 0.004166666666666667;
        double r60521 = 5.0;
        double r60522 = pow(r60503, r60521);
        double r60523 = 0.5;
        double r60524 = r60523 * r60503;
        double r60525 = fma(r60520, r60522, r60524);
        double r60526 = fma(r60517, r60519, r60525);
        double r60527 = pow(r60508, r60518);
        double r60528 = pow(r60509, r60518);
        double r60529 = pow(r60528, r60518);
        double r60530 = cbrt(r60529);
        double r60531 = r60527 - r60530;
        double r60532 = r60508 + r60509;
        double r60533 = r60509 * r60532;
        double r60534 = fma(r60508, r60508, r60533);
        double r60535 = r60507 * r60534;
        double r60536 = r60531 / r60535;
        double r60537 = r60516 ? r60526 : r60536;
        double r60538 = r60505 ? r60514 : r60537;
        return r60538;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied clear-num1.0

      \[\leadsto \color{blue}{\frac{1}{\frac{\sin x}{1 - \cos x}}}\]
    4. Using strategy rm
    5. Applied expm1-log1p-u1.1

      \[\leadsto \frac{1}{\frac{\sin x}{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 - \cos x\right)\right)}}}\]

    if -0.024087893247376972 < x < 0.020706483323998073

    1. Initial program 59.8

      \[\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.020706483323998073 < 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. Applied associate-/l/1.0

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

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

      \[\leadsto \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 \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}\]
    8. Simplified1.1

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

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

Reproduce

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