Average Error: 29.8 → 0.7
Time: 9.1s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0118953516307934663:\\ \;\;\;\;\log \left(e^{\frac{1 - \cos x}{\sin x}}\right)\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 2.2776287241343477 \cdot 10^{-5}:\\ \;\;\;\;\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} - {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(\cos x, \frac{1 \cdot 1 - {\left(\cos x\right)}^{2}}{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.0118953516307934663:\\
\;\;\;\;\log \left(e^{\frac{1 - \cos x}{\sin x}}\right)\\

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

\end{array}
double f(double x) {
        double r79494 = 1.0;
        double r79495 = x;
        double r79496 = cos(r79495);
        double r79497 = r79494 - r79496;
        double r79498 = sin(r79495);
        double r79499 = r79497 / r79498;
        return r79499;
}

double f(double x) {
        double r79500 = 1.0;
        double r79501 = x;
        double r79502 = cos(r79501);
        double r79503 = r79500 - r79502;
        double r79504 = sin(r79501);
        double r79505 = r79503 / r79504;
        double r79506 = -0.011895351630793466;
        bool r79507 = r79505 <= r79506;
        double r79508 = exp(r79505);
        double r79509 = log(r79508);
        double r79510 = 2.2776287241343477e-05;
        bool r79511 = r79505 <= r79510;
        double r79512 = 0.041666666666666664;
        double r79513 = 3.0;
        double r79514 = pow(r79501, r79513);
        double r79515 = 0.004166666666666667;
        double r79516 = 5.0;
        double r79517 = pow(r79501, r79516);
        double r79518 = 0.5;
        double r79519 = r79518 * r79501;
        double r79520 = fma(r79515, r79517, r79519);
        double r79521 = fma(r79512, r79514, r79520);
        double r79522 = pow(r79500, r79513);
        double r79523 = pow(r79502, r79513);
        double r79524 = r79522 - r79523;
        double r79525 = r79500 * r79500;
        double r79526 = 2.0;
        double r79527 = pow(r79502, r79526);
        double r79528 = r79525 - r79527;
        double r79529 = r79528 / r79503;
        double r79530 = fma(r79502, r79529, r79525);
        double r79531 = r79524 / r79530;
        double r79532 = r79531 / r79504;
        double r79533 = r79511 ? r79521 : r79532;
        double r79534 = r79507 ? r79509 : r79533;
        return r79534;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.8

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied add-log-exp1.0

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

    if -0.011895351630793466 < (/ (- 1.0 (cos x)) (sin x)) < 2.2776287241343477e-05

    1. Initial program 60.0

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

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

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

    1. Initial program 1.1

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

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

      \[\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 flip-+1.2

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

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

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

Reproduce

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