Average Error: 30.3 → 0.7
Time: 7.3s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -2.76308119989467809 \cdot 10^{-4}:\\ \;\;\;\;\frac{{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}\;\frac{1 - \cos x}{\sin x} \le -0.0:\\ \;\;\;\;\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}{\sin x} - \frac{\cos x}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -2.76308119989467809 \cdot 10^{-4}:\\
\;\;\;\;\frac{{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}\;\frac{1 - \cos x}{\sin x} \le -0.0:\\
\;\;\;\;\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}{\sin x} - \frac{\cos x}{\sin x}\\

\end{array}
double f(double x) {
        double r76572 = 1.0;
        double r76573 = x;
        double r76574 = cos(r76573);
        double r76575 = r76572 - r76574;
        double r76576 = sin(r76573);
        double r76577 = r76575 / r76576;
        return r76577;
}

double f(double x) {
        double r76578 = 1.0;
        double r76579 = x;
        double r76580 = cos(r76579);
        double r76581 = r76578 - r76580;
        double r76582 = sin(r76579);
        double r76583 = r76581 / r76582;
        double r76584 = -0.0002763081199894678;
        bool r76585 = r76583 <= r76584;
        double r76586 = 3.0;
        double r76587 = pow(r76578, r76586);
        double r76588 = pow(r76580, r76586);
        double r76589 = r76587 - r76588;
        double r76590 = r76578 * r76580;
        double r76591 = fma(r76580, r76580, r76590);
        double r76592 = fma(r76578, r76578, r76591);
        double r76593 = r76592 * r76582;
        double r76594 = r76589 / r76593;
        double r76595 = -0.0;
        bool r76596 = r76583 <= r76595;
        double r76597 = 0.041666666666666664;
        double r76598 = pow(r76579, r76586);
        double r76599 = 0.004166666666666667;
        double r76600 = 5.0;
        double r76601 = pow(r76579, r76600);
        double r76602 = 0.5;
        double r76603 = r76602 * r76579;
        double r76604 = fma(r76599, r76601, r76603);
        double r76605 = fma(r76597, r76598, r76604);
        double r76606 = r76578 / r76582;
        double r76607 = r76580 / r76582;
        double r76608 = r76606 - r76607;
        double r76609 = r76596 ? r76605 : r76608;
        double r76610 = r76585 ? r76594 : r76609;
        return r76610;
}

Error

Bits error versus x

Target

Original30.3
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.0002763081199894678

    1. Initial program 1.0

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

      \[\leadsto \color{blue}{\log \left(e^{\frac{1 - \cos x}{\sin x}}\right)}\]
    4. Using strategy rm
    5. Applied clear-num1.2

      \[\leadsto \log \left(e^{\color{blue}{\frac{1}{\frac{\sin x}{1 - \cos x}}}}\right)\]
    6. Taylor expanded around inf 1.0

      \[\leadsto \color{blue}{\frac{1 - \cos x}{\sin x}}\]
    7. Using strategy rm
    8. Applied flip3--1.1

      \[\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}\]
    9. Applied associate-/l/1.1

      \[\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)}}\]
    10. Simplified1.1

      \[\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}}\]

    if -0.0002763081199894678 < (/ (- 1.0 (cos x)) (sin x)) < -0.0

    1. Initial program 60.3

      \[\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.0 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 1.3

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied div-sub1.5

      \[\leadsto \color{blue}{\frac{1}{\sin x} - \frac{\cos x}{\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 -2.76308119989467809 \cdot 10^{-4}:\\ \;\;\;\;\frac{{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}\;\frac{1 - \cos x}{\sin x} \le -0.0:\\ \;\;\;\;\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}{\sin x} - \frac{\cos x}{\sin x}\\ \end{array}\]

Reproduce

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