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

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

\end{array}
double f(double x) {
        double r68514 = 1.0;
        double r68515 = x;
        double r68516 = cos(r68515);
        double r68517 = r68514 - r68516;
        double r68518 = sin(r68515);
        double r68519 = r68517 / r68518;
        return r68519;
}

double f(double x) {
        double r68520 = x;
        double r68521 = -0.021580538691811514;
        bool r68522 = r68520 <= r68521;
        double r68523 = cos(r68520);
        double r68524 = 3.0;
        double r68525 = pow(r68523, r68524);
        double r68526 = -r68525;
        double r68527 = 1.0;
        double r68528 = 6.0;
        double r68529 = pow(r68527, r68528);
        double r68530 = fma(r68526, r68525, r68529);
        double r68531 = pow(r68527, r68524);
        double r68532 = r68531 + r68525;
        double r68533 = r68530 / r68532;
        double r68534 = r68527 * r68523;
        double r68535 = fma(r68523, r68523, r68534);
        double r68536 = fma(r68527, r68527, r68535);
        double r68537 = sin(r68520);
        double r68538 = r68536 * r68537;
        double r68539 = r68533 / r68538;
        double r68540 = 0.021515064933806238;
        bool r68541 = r68520 <= r68540;
        double r68542 = 0.041666666666666664;
        double r68543 = pow(r68520, r68524);
        double r68544 = 0.004166666666666667;
        double r68545 = 5.0;
        double r68546 = pow(r68520, r68545);
        double r68547 = 0.5;
        double r68548 = r68547 * r68520;
        double r68549 = fma(r68544, r68546, r68548);
        double r68550 = fma(r68542, r68543, r68549);
        double r68551 = pow(r68525, r68524);
        double r68552 = expm1(r68551);
        double r68553 = log1p(r68552);
        double r68554 = cbrt(r68553);
        double r68555 = r68531 - r68554;
        double r68556 = r68555 / r68538;
        double r68557 = r68541 ? r68550 : r68556;
        double r68558 = r68522 ? r68539 : r68557;
        return r68558;
}

Error

Bits error versus x

Target

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

Derivation

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

    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. Applied associate-/l/0.9

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

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

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

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

    if -0.021580538691811514 < x < 0.021515064933806238

    1. Initial program 60.0

      \[\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.021515064933806238 < 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}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}}\]
    6. Using strategy rm
    7. Applied add-cbrt-cube1.0

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

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

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

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

Reproduce

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