Average Error: 30.5 → 0.5
Time: 20.8s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.02120801236249347798690223498851992189884 \lor \neg \left(x \le 0.02196010544527774235867845220582239562646\right):\\ \;\;\;\;\frac{\frac{\left(1 - \cos x\right) \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}{\sin x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.02120801236249347798690223498851992189884 \lor \neg \left(x \le 0.02196010544527774235867845220582239562646\right):\\
\;\;\;\;\frac{\frac{\left(1 - \cos x\right) \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}{\sin x}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\

\end{array}
double f(double x) {
        double r37381 = 1.0;
        double r37382 = x;
        double r37383 = cos(r37382);
        double r37384 = r37381 - r37383;
        double r37385 = sin(r37382);
        double r37386 = r37384 / r37385;
        return r37386;
}

double f(double x) {
        double r37387 = x;
        double r37388 = -0.021208012362493478;
        bool r37389 = r37387 <= r37388;
        double r37390 = 0.021960105445277742;
        bool r37391 = r37387 <= r37390;
        double r37392 = !r37391;
        bool r37393 = r37389 || r37392;
        double r37394 = 1.0;
        double r37395 = cos(r37387);
        double r37396 = r37394 - r37395;
        double r37397 = r37394 + r37395;
        double r37398 = r37395 * r37397;
        double r37399 = fma(r37394, r37394, r37398);
        double r37400 = r37396 * r37399;
        double r37401 = r37400 / r37399;
        double r37402 = sin(r37387);
        double r37403 = r37401 / r37402;
        double r37404 = 0.041666666666666664;
        double r37405 = 3.0;
        double r37406 = pow(r37387, r37405);
        double r37407 = 0.004166666666666667;
        double r37408 = 5.0;
        double r37409 = pow(r37387, r37408);
        double r37410 = 0.5;
        double r37411 = r37410 * r37387;
        double r37412 = fma(r37407, r37409, r37411);
        double r37413 = fma(r37404, r37406, r37412);
        double r37414 = r37393 ? r37403 : r37413;
        return r37414;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 2 regimes
  2. if x < -0.021208012362493478 or 0.021960105445277742 < 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. Simplified1.0

      \[\leadsto \frac{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\color{blue}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}}{\sin x}\]
    5. Using strategy rm
    6. Applied *-un-lft-identity1.0

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

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

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

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(1 + \cos x, \cos x, 1 \cdot 1\right)} \cdot \frac{1 - \cos x}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}{\sin x}\]
    10. Using strategy rm
    11. Applied associate-*r/1.0

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

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

    if -0.021208012362493478 < x < 0.021960105445277742

    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)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.02120801236249347798690223498851992189884 \lor \neg \left(x \le 0.02196010544527774235867845220582239562646\right):\\ \;\;\;\;\frac{\frac{\left(1 - \cos x\right) \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}{\sin x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\ \end{array}\]

Reproduce

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