Average Error: 30.4 → 0.6
Time: 15.1s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.004893422048095925:\\ \;\;\;\;\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.98402005876572314 \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}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1 - \cos x}{\sin x}\right)\right)\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.004893422048095925:\\
\;\;\;\;\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.98402005876572314 \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}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1 - \cos x}{\sin x}\right)\right)\\

\end{array}
double f(double x) {
        double r51307 = 1.0;
        double r51308 = x;
        double r51309 = cos(r51308);
        double r51310 = r51307 - r51309;
        double r51311 = sin(r51308);
        double r51312 = r51310 / r51311;
        return r51312;
}

double f(double x) {
        double r51313 = 1.0;
        double r51314 = x;
        double r51315 = cos(r51314);
        double r51316 = r51313 - r51315;
        double r51317 = sin(r51314);
        double r51318 = r51316 / r51317;
        double r51319 = -0.0048934220480959245;
        bool r51320 = r51318 <= r51319;
        double r51321 = exp(r51318);
        double r51322 = log1p(r51321);
        double r51323 = expm1(r51322);
        double r51324 = log(r51323);
        double r51325 = 2.984020058765723e-05;
        bool r51326 = r51318 <= r51325;
        double r51327 = 0.041666666666666664;
        double r51328 = 3.0;
        double r51329 = pow(r51314, r51328);
        double r51330 = 0.004166666666666667;
        double r51331 = 5.0;
        double r51332 = pow(r51314, r51331);
        double r51333 = 0.5;
        double r51334 = r51333 * r51314;
        double r51335 = fma(r51330, r51332, r51334);
        double r51336 = fma(r51327, r51329, r51335);
        double r51337 = expm1(r51318);
        double r51338 = log1p(r51337);
        double r51339 = r51326 ? r51336 : r51338;
        double r51340 = r51320 ? r51324 : r51339;
        return r51340;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.9

      \[\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)}\]
    4. Using strategy rm
    5. Applied expm1-log1p-u1.1

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

    if -0.0048934220480959245 < (/ (- 1.0 (cos x)) (sin x)) < 2.984020058765723e-05

    1. Initial program 59.9

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

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

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

    1. Initial program 1.1

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied log1p-expm1-u1.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.004893422048095925:\\ \;\;\;\;\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.98402005876572314 \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}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{1 - \cos x}{\sin x}\right)\right)\\ \end{array}\]

Reproduce

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