Average Error: 30.4 → 0.6
Time: 20.4s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.001308568773005096318520767972870544326724 \lor \neg \left(\frac{1 - \cos x}{\sin x} \le 4.957178312169810962668929968898134941213 \cdot 10^{-8}\right):\\ \;\;\;\;\log \left(e^{\frac{1 - \cos x}{\sin x}}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.001308568773005096318520767972870544326724 \lor \neg \left(\frac{1 - \cos x}{\sin x} \le 4.957178312169810962668929968898134941213 \cdot 10^{-8}\right):\\
\;\;\;\;\log \left(e^{\frac{1 - \cos x}{\sin x}}\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\

\end{array}
double f(double x) {
        double r48235 = 1.0;
        double r48236 = x;
        double r48237 = cos(r48236);
        double r48238 = r48235 - r48237;
        double r48239 = sin(r48236);
        double r48240 = r48238 / r48239;
        return r48240;
}

double f(double x) {
        double r48241 = 1.0;
        double r48242 = x;
        double r48243 = cos(r48242);
        double r48244 = r48241 - r48243;
        double r48245 = sin(r48242);
        double r48246 = r48244 / r48245;
        double r48247 = -0.0013085687730050963;
        bool r48248 = r48246 <= r48247;
        double r48249 = 4.957178312169811e-08;
        bool r48250 = r48246 <= r48249;
        double r48251 = !r48250;
        bool r48252 = r48248 || r48251;
        double r48253 = exp(r48246);
        double r48254 = log(r48253);
        double r48255 = 0.041666666666666664;
        double r48256 = 3.0;
        double r48257 = pow(r48242, r48256);
        double r48258 = 0.004166666666666667;
        double r48259 = 5.0;
        double r48260 = pow(r48242, r48259);
        double r48261 = 0.5;
        double r48262 = r48261 * r48242;
        double r48263 = fma(r48258, r48260, r48262);
        double r48264 = fma(r48255, r48257, r48263);
        double r48265 = r48252 ? r48254 : r48264;
        return r48265;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 2 regimes
  2. if (/ (- 1.0 (cos x)) (sin x)) < -0.0013085687730050963 or 4.957178312169811e-08 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 1.1

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

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

    if -0.0013085687730050963 < (/ (- 1.0 (cos x)) (sin x)) < 4.957178312169811e-08

    1. Initial program 60.2

      \[\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)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.001308568773005096318520767972870544326724 \lor \neg \left(\frac{1 - \cos x}{\sin x} \le 4.957178312169810962668929968898134941213 \cdot 10^{-8}\right):\\ \;\;\;\;\log \left(e^{\frac{1 - \cos x}{\sin x}}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\ \end{array}\]

Reproduce

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