Average Error: 30.2 → 0.6
Time: 7.1s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.0242809220681906925976001332401210675016:\\ \;\;\;\;\frac{1}{\sin x} - \frac{\cos x}{\sin x}\\ \mathbf{elif}\;x \le 0.02165047000055947065688677355410618474707:\\ \;\;\;\;\frac{1}{24} \cdot {x}^{3} + \left(\frac{1}{240} \cdot {x}^{5} + \frac{1}{2} \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\log \left(e^{1 - \cos x}\right)}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.0242809220681906925976001332401210675016:\\
\;\;\;\;\frac{1}{\sin x} - \frac{\cos x}{\sin x}\\

\mathbf{elif}\;x \le 0.02165047000055947065688677355410618474707:\\
\;\;\;\;\frac{1}{24} \cdot {x}^{3} + \left(\frac{1}{240} \cdot {x}^{5} + \frac{1}{2} \cdot x\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{\log \left(e^{1 - \cos x}\right)}{\sin x}\\

\end{array}
double f(double x) {
        double r50986 = 1.0;
        double r50987 = x;
        double r50988 = cos(r50987);
        double r50989 = r50986 - r50988;
        double r50990 = sin(r50987);
        double r50991 = r50989 / r50990;
        return r50991;
}

double f(double x) {
        double r50992 = x;
        double r50993 = -0.024280922068190693;
        bool r50994 = r50992 <= r50993;
        double r50995 = 1.0;
        double r50996 = sin(r50992);
        double r50997 = r50995 / r50996;
        double r50998 = cos(r50992);
        double r50999 = r50998 / r50996;
        double r51000 = r50997 - r50999;
        double r51001 = 0.02165047000055947;
        bool r51002 = r50992 <= r51001;
        double r51003 = 0.041666666666666664;
        double r51004 = 3.0;
        double r51005 = pow(r50992, r51004);
        double r51006 = r51003 * r51005;
        double r51007 = 0.004166666666666667;
        double r51008 = 5.0;
        double r51009 = pow(r50992, r51008);
        double r51010 = r51007 * r51009;
        double r51011 = 0.5;
        double r51012 = r51011 * r50992;
        double r51013 = r51010 + r51012;
        double r51014 = r51006 + r51013;
        double r51015 = r50995 - r50998;
        double r51016 = exp(r51015);
        double r51017 = log(r51016);
        double r51018 = r51017 / r50996;
        double r51019 = r51002 ? r51014 : r51018;
        double r51020 = r50994 ? r51000 : r51019;
        return r51020;
}

Error

Bits error versus x

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

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

Derivation

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

    1. Initial program 0.9

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

      \[\leadsto \color{blue}{\frac{1}{\sin x} - \frac{\cos x}{\sin x}}\]

    if -0.024280922068190693 < x < 0.02165047000055947

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

    if 0.02165047000055947 < x

    1. Initial program 0.9

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

      \[\leadsto \frac{1 - \color{blue}{\log \left(e^{\cos x}\right)}}{\sin x}\]
    4. Applied add-log-exp1.1

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.0242809220681906925976001332401210675016:\\ \;\;\;\;\frac{1}{\sin x} - \frac{\cos x}{\sin x}\\ \mathbf{elif}\;x \le 0.02165047000055947065688677355410618474707:\\ \;\;\;\;\frac{1}{24} \cdot {x}^{3} + \left(\frac{1}{240} \cdot {x}^{5} + \frac{1}{2} \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\log \left(e^{1 - \cos x}\right)}{\sin x}\\ \end{array}\]

Reproduce

herbie shell --seed 2020001 
(FPCore (x)
  :name "tanhf (example 3.4)"
  :precision binary64
  :herbie-expected 2

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

  (/ (- 1 (cos x)) (sin x)))