Average Error: 30.0 → 0.8
Time: 18.4s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -7.531467756712890016922851144087758257228 \cdot 10^{-5}:\\ \;\;\;\;\log \left(\mathsf{expm1}\left(\mathsf{log1p}\left(e^{\frac{1 - \cos x}{\sin x}}\right)\right)\right)\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 2.546632212092768594597497200382463233836 \cdot 10^{-6}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left({x}^{5}, \frac{1}{240}, x \cdot \frac{1}{2}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(1 - \cos x\right) \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1}{\sin x}\right)\right)\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -7.531467756712890016922851144087758257228 \cdot 10^{-5}:\\
\;\;\;\;\log \left(\mathsf{expm1}\left(\mathsf{log1p}\left(e^{\frac{1 - \cos x}{\sin x}}\right)\right)\right)\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 2.546632212092768594597497200382463233836 \cdot 10^{-6}:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left({x}^{5}, \frac{1}{240}, x \cdot \frac{1}{2}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\left(1 - \cos x\right) \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1}{\sin x}\right)\right)\\

\end{array}
double f(double x) {
        double r40481 = 1.0;
        double r40482 = x;
        double r40483 = cos(r40482);
        double r40484 = r40481 - r40483;
        double r40485 = sin(r40482);
        double r40486 = r40484 / r40485;
        return r40486;
}

double f(double x) {
        double r40487 = 1.0;
        double r40488 = x;
        double r40489 = cos(r40488);
        double r40490 = r40487 - r40489;
        double r40491 = sin(r40488);
        double r40492 = r40490 / r40491;
        double r40493 = -7.53146775671289e-05;
        bool r40494 = r40492 <= r40493;
        double r40495 = exp(r40492);
        double r40496 = log1p(r40495);
        double r40497 = expm1(r40496);
        double r40498 = log(r40497);
        double r40499 = 2.5466322120927686e-06;
        bool r40500 = r40492 <= r40499;
        double r40501 = 0.041666666666666664;
        double r40502 = 3.0;
        double r40503 = pow(r40488, r40502);
        double r40504 = 5.0;
        double r40505 = pow(r40488, r40504);
        double r40506 = 0.004166666666666667;
        double r40507 = 0.5;
        double r40508 = r40488 * r40507;
        double r40509 = fma(r40505, r40506, r40508);
        double r40510 = fma(r40501, r40503, r40509);
        double r40511 = 1.0;
        double r40512 = r40511 / r40491;
        double r40513 = expm1(r40512);
        double r40514 = log1p(r40513);
        double r40515 = r40490 * r40514;
        double r40516 = r40500 ? r40510 : r40515;
        double r40517 = r40494 ? r40498 : r40516;
        return r40517;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 1.0

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

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

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

    if -7.53146775671289e-05 < (/ (- 1.0 (cos x)) (sin x)) < 2.5466322120927686e-06

    1. Initial program 60.3

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left({x}^{5}, \frac{1}{240}, \frac{1}{2} \cdot x\right)\right)}\]

    if 2.5466322120927686e-06 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 1.2

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

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

      \[\leadsto \log \left(e^{\color{blue}{\left(1 - \cos x\right) \cdot \frac{1}{\sin x}}}\right)\]
    6. Applied exp-prod1.7

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

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

      \[\leadsto \frac{1}{\sin x} \cdot \color{blue}{\left(1 - \cos x\right)}\]
    9. Using strategy rm
    10. Applied log1p-expm1-u1.8

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1}{\sin x}\right)\right)} \cdot \left(1 - \cos x\right)\]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.8

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -7.531467756712890016922851144087758257228 \cdot 10^{-5}:\\ \;\;\;\;\log \left(\mathsf{expm1}\left(\mathsf{log1p}\left(e^{\frac{1 - \cos x}{\sin x}}\right)\right)\right)\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 2.546632212092768594597497200382463233836 \cdot 10^{-6}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left({x}^{5}, \frac{1}{240}, x \cdot \frac{1}{2}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(1 - \cos x\right) \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1}{\sin x}\right)\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2019196 +o rules:numerics
(FPCore (x)
  :name "tanhf (example 3.4)"
  :herbie-expected 2

  :herbie-target
  (tan (/ x 2.0))

  (/ (- 1.0 (cos x)) (sin x)))