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

\mathbf{elif}\;x \le 0.01896428056389416749727416799942147918046:\\
\;\;\;\;\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{\mathsf{fma}\left(\mathsf{fma}\left(\frac{-1}{2}, \cos \left(2 \cdot x\right), \frac{-1}{2}\right), \cos x, 1\right)}{\sin x}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1\right)}\\

\end{array}
double f(double x) {
        double r58405 = 1.0;
        double r58406 = x;
        double r58407 = cos(r58406);
        double r58408 = r58405 - r58407;
        double r58409 = sin(r58406);
        double r58410 = r58408 / r58409;
        return r58410;
}

double f(double x) {
        double r58411 = x;
        double r58412 = -0.01938722760852505;
        bool r58413 = r58411 <= r58412;
        double r58414 = 1.0;
        double r58415 = sin(r58411);
        double r58416 = r58414 / r58415;
        double r58417 = -0.5;
        double r58418 = 2.0;
        double r58419 = r58418 * r58411;
        double r58420 = cos(r58419);
        double r58421 = fma(r58417, r58420, r58417);
        double r58422 = cos(r58411);
        double r58423 = 1.0;
        double r58424 = 3.0;
        double r58425 = pow(r58423, r58424);
        double r58426 = fma(r58421, r58422, r58425);
        double r58427 = r58423 + r58422;
        double r58428 = r58422 * r58427;
        double r58429 = fma(r58423, r58423, r58428);
        double r58430 = r58426 / r58429;
        double r58431 = r58416 * r58430;
        double r58432 = 0.018964280563894167;
        bool r58433 = r58411 <= r58432;
        double r58434 = 0.041666666666666664;
        double r58435 = pow(r58411, r58424);
        double r58436 = 0.004166666666666667;
        double r58437 = 5.0;
        double r58438 = pow(r58411, r58437);
        double r58439 = 0.5;
        double r58440 = r58439 * r58411;
        double r58441 = fma(r58436, r58438, r58440);
        double r58442 = fma(r58434, r58435, r58441);
        double r58443 = fma(r58421, r58422, r58423);
        double r58444 = r58443 / r58415;
        double r58445 = fma(r58422, r58427, r58423);
        double r58446 = r58444 / r58445;
        double r58447 = r58433 ? r58442 : r58446;
        double r58448 = r58413 ? r58431 : r58447;
        return r58448;
}

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.01938722760852505

    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.4

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

      \[\leadsto \frac{{1}^{3} - \color{blue}{\left(\cos x \cdot \cos x\right) \cdot \cos x}}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}\]
    9. Using strategy rm
    10. Applied sqr-cos0.9

      \[\leadsto \frac{{1}^{3} - \color{blue}{\left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(2 \cdot x\right)\right)} \cdot \cos x}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}\]
    11. Using strategy rm
    12. Applied *-un-lft-identity0.9

      \[\leadsto \frac{\color{blue}{1 \cdot \left({1}^{3} - \left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(2 \cdot x\right)\right) \cdot \cos x\right)}}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}\]
    13. Applied times-frac1.0

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

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

    if -0.01938722760852505 < x < 0.018964280563894167

    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.018964280563894167 < 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.4

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

      \[\leadsto \frac{{1}^{3} - \color{blue}{\left(\cos x \cdot \cos x\right) \cdot \cos x}}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}\]
    9. Using strategy rm
    10. Applied sqr-cos0.9

      \[\leadsto \frac{{1}^{3} - \color{blue}{\left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(2 \cdot x\right)\right)} \cdot \cos x}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}\]
    11. Taylor expanded around inf 0.9

      \[\leadsto \color{blue}{\frac{1 - \left(\frac{1}{2} \cdot \left(\cos \left(2 \cdot x\right) \cdot \cos x\right) + \frac{1}{2} \cdot \cos x\right)}{\sin x \cdot \left(1 + \left({\left(\cos x\right)}^{2} + 1 \cdot \cos x\right)\right)}}\]
    12. Simplified0.9

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

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

Reproduce

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