Average Error: 30.1 → 0.6
Time: 18.9s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.02270927157581989139223388463051378494129:\\ \;\;\;\;\log \left(e^{\frac{\log \left(e^{1 - \cos x}\right)}{\sin x}}\right)\\ \mathbf{elif}\;x \le 0.01743449574326636983268024039261945290491:\\ \;\;\;\;\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}\;x \le -0.02270927157581989139223388463051378494129:\\
\;\;\;\;\log \left(e^{\frac{\log \left(e^{1 - \cos x}\right)}{\sin x}}\right)\\

\mathbf{elif}\;x \le 0.01743449574326636983268024039261945290491:\\
\;\;\;\;\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 r59982 = 1.0;
        double r59983 = x;
        double r59984 = cos(r59983);
        double r59985 = r59982 - r59984;
        double r59986 = sin(r59983);
        double r59987 = r59985 / r59986;
        return r59987;
}

double f(double x) {
        double r59988 = x;
        double r59989 = -0.02270927157581989;
        bool r59990 = r59988 <= r59989;
        double r59991 = 1.0;
        double r59992 = cos(r59988);
        double r59993 = r59991 - r59992;
        double r59994 = exp(r59993);
        double r59995 = log(r59994);
        double r59996 = sin(r59988);
        double r59997 = r59995 / r59996;
        double r59998 = exp(r59997);
        double r59999 = log(r59998);
        double r60000 = 0.01743449574326637;
        bool r60001 = r59988 <= r60000;
        double r60002 = 0.041666666666666664;
        double r60003 = 3.0;
        double r60004 = pow(r59988, r60003);
        double r60005 = 0.004166666666666667;
        double r60006 = 5.0;
        double r60007 = pow(r59988, r60006);
        double r60008 = 0.5;
        double r60009 = r60008 * r59988;
        double r60010 = fma(r60005, r60007, r60009);
        double r60011 = fma(r60002, r60004, r60010);
        double r60012 = r59991 / r59996;
        double r60013 = r59992 / r59996;
        double r60014 = r60012 - r60013;
        double r60015 = r60001 ? r60011 : r60014;
        double r60016 = r59990 ? r59999 : r60015;
        return r60016;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied add-exp-log0.9

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

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

      \[\leadsto \log \color{blue}{\left(e^{\frac{1 - \cos x}{\sin x}}\right)}\]
    7. Using strategy rm
    8. Applied add-log-exp1.3

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

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

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

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

    if -0.02270927157581989 < x < 0.01743449574326637

    1. Initial program 59.9

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

    1. Initial program 1.0

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

      \[\leadsto \color{blue}{\frac{1}{\sin x} - \frac{\cos x}{\sin x}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.02270927157581989139223388463051378494129:\\ \;\;\;\;\log \left(e^{\frac{\log \left(e^{1 - \cos x}\right)}{\sin x}}\right)\\ \mathbf{elif}\;x \le 0.01743449574326636983268024039261945290491:\\ \;\;\;\;\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 2019212 +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)))