Average Error: 30.6 → 0.5
Time: 7.7s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.023342343569720127:\\ \;\;\;\;\frac{1}{\frac{\sin x}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 - \cos x\right)\right)}}\\ \mathbf{elif}\;x \le 0.02343065570717855:\\ \;\;\;\;\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}:\\ \;\;\;\;\log \left(e^{\frac{1}{\frac{\sin x}{1 - \cos x}}}\right)\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.023342343569720127:\\
\;\;\;\;\frac{1}{\frac{\sin x}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 - \cos x\right)\right)}}\\

\mathbf{elif}\;x \le 0.02343065570717855:\\
\;\;\;\;\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}:\\
\;\;\;\;\log \left(e^{\frac{1}{\frac{\sin x}{1 - \cos x}}}\right)\\

\end{array}
double f(double x) {
        double r49109 = 1.0;
        double r49110 = x;
        double r49111 = cos(r49110);
        double r49112 = r49109 - r49111;
        double r49113 = sin(r49110);
        double r49114 = r49112 / r49113;
        return r49114;
}

double f(double x) {
        double r49115 = x;
        double r49116 = -0.023342343569720127;
        bool r49117 = r49115 <= r49116;
        double r49118 = 1.0;
        double r49119 = sin(r49115);
        double r49120 = 1.0;
        double r49121 = cos(r49115);
        double r49122 = r49120 - r49121;
        double r49123 = log1p(r49122);
        double r49124 = expm1(r49123);
        double r49125 = r49119 / r49124;
        double r49126 = r49118 / r49125;
        double r49127 = 0.02343065570717855;
        bool r49128 = r49115 <= r49127;
        double r49129 = 0.041666666666666664;
        double r49130 = 3.0;
        double r49131 = pow(r49115, r49130);
        double r49132 = 0.004166666666666667;
        double r49133 = 5.0;
        double r49134 = pow(r49115, r49133);
        double r49135 = 0.5;
        double r49136 = r49135 * r49115;
        double r49137 = fma(r49132, r49134, r49136);
        double r49138 = fma(r49129, r49131, r49137);
        double r49139 = r49119 / r49122;
        double r49140 = r49118 / r49139;
        double r49141 = exp(r49140);
        double r49142 = log(r49141);
        double r49143 = r49128 ? r49138 : r49142;
        double r49144 = r49117 ? r49126 : r49143;
        return r49144;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 3 regimes
  2. if x < -0.023342343569720127

    1. Initial program 0.8

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied clear-num0.9

      \[\leadsto \color{blue}{\frac{1}{\frac{\sin x}{1 - \cos x}}}\]
    4. Using strategy rm
    5. Applied expm1-log1p-u1.0

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

    if -0.023342343569720127 < x < 0.02343065570717855

    1. Initial program 60.0

      \[\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.02343065570717855 < x

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied clear-num1.0

      \[\leadsto \color{blue}{\frac{1}{\frac{\sin x}{1 - \cos x}}}\]
    4. Using strategy rm
    5. Applied add-log-exp1.1

      \[\leadsto \color{blue}{\log \left(e^{\frac{1}{\frac{\sin x}{1 - \cos x}}}\right)}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.023342343569720127:\\ \;\;\;\;\frac{1}{\frac{\sin x}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 - \cos x\right)\right)}}\\ \mathbf{elif}\;x \le 0.02343065570717855:\\ \;\;\;\;\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}:\\ \;\;\;\;\log \left(e^{\frac{1}{\frac{\sin x}{1 - \cos x}}}\right)\\ \end{array}\]

Reproduce

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