Average Error: 30.4 → 0.4
Time: 8.0s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.0198749278940998884 \lor \neg \left(x \le 0.022259987926796919\right):\\ \;\;\;\;1 \cdot \frac{1 - \cos x}{\sin x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(0.04166666666666663, {x}^{3}, \mathsf{fma}\left(0.004166666666666624, {x}^{5}, 0.5 \cdot x\right)\right)\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.0198749278940998884 \lor \neg \left(x \le 0.022259987926796919\right):\\
\;\;\;\;1 \cdot \frac{1 - \cos x}{\sin x}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(0.04166666666666663, {x}^{3}, \mathsf{fma}\left(0.004166666666666624, {x}^{5}, 0.5 \cdot x\right)\right)\\

\end{array}
double f(double x) {
        double r81491 = 1.0;
        double r81492 = x;
        double r81493 = cos(r81492);
        double r81494 = r81491 - r81493;
        double r81495 = sin(r81492);
        double r81496 = r81494 / r81495;
        return r81496;
}

double f(double x) {
        double r81497 = x;
        double r81498 = -0.01987492789409989;
        bool r81499 = r81497 <= r81498;
        double r81500 = 0.02225998792679692;
        bool r81501 = r81497 <= r81500;
        double r81502 = !r81501;
        bool r81503 = r81499 || r81502;
        double r81504 = 1.0;
        double r81505 = 1.0;
        double r81506 = cos(r81497);
        double r81507 = r81505 - r81506;
        double r81508 = sin(r81497);
        double r81509 = r81507 / r81508;
        double r81510 = r81504 * r81509;
        double r81511 = 0.04166666666666663;
        double r81512 = 3.0;
        double r81513 = pow(r81497, r81512);
        double r81514 = 0.004166666666666624;
        double r81515 = 5.0;
        double r81516 = pow(r81497, r81515);
        double r81517 = 0.5;
        double r81518 = r81517 * r81497;
        double r81519 = fma(r81514, r81516, r81518);
        double r81520 = fma(r81511, r81513, r81519);
        double r81521 = r81503 ? r81510 : r81520;
        return r81521;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 2 regimes
  2. if x < -0.01987492789409989 or 0.02225998792679692 < x

    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/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 difference-cubes1.0

      \[\leadsto \frac{\color{blue}{\left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right) \cdot \left(1 - \cos x\right)}}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}\]
    8. Applied times-frac0.9

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

      \[\leadsto \color{blue}{1} \cdot \frac{1 - \cos x}{\sin x}\]

    if -0.01987492789409989 < x < 0.02225998792679692

    1. Initial program 59.8

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied flip3--59.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/59.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. Simplified59.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. Taylor expanded around 0 0.0

      \[\leadsto \color{blue}{0.04166666666666663 \cdot {x}^{3} + \left(0.004166666666666624 \cdot {x}^{5} + 0.5 \cdot x\right)}\]
    7. Simplified0.0

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.04166666666666663, {x}^{3}, \mathsf{fma}\left(0.004166666666666624, {x}^{5}, 0.5 \cdot x\right)\right)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.0198749278940998884 \lor \neg \left(x \le 0.022259987926796919\right):\\ \;\;\;\;1 \cdot \frac{1 - \cos x}{\sin x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(0.04166666666666663, {x}^{3}, \mathsf{fma}\left(0.004166666666666624, {x}^{5}, 0.5 \cdot x\right)\right)\\ \end{array}\]

Reproduce

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