Average Error: 30.4 → 0.5
Time: 13.7s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.0227880187083014697224658817731324234046 \lor \neg \left(x \le 0.02338074252416355927608471176881721476093\right):\\ \;\;\;\;\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(0.04166666666666662965923251249478198587894, {x}^{3}, \mathsf{fma}\left(0.004166666666666624108117389368999283760786, {x}^{5}, 0.5 \cdot x\right)\right)\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.0227880187083014697224658817731324234046 \lor \neg \left(x \le 0.02338074252416355927608471176881721476093\right):\\
\;\;\;\;\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(0.04166666666666662965923251249478198587894, {x}^{3}, \mathsf{fma}\left(0.004166666666666624108117389368999283760786, {x}^{5}, 0.5 \cdot x\right)\right)\\

\end{array}
double f(double x) {
        double r50382 = 1.0;
        double r50383 = x;
        double r50384 = cos(r50383);
        double r50385 = r50382 - r50384;
        double r50386 = sin(r50383);
        double r50387 = r50385 / r50386;
        return r50387;
}

double f(double x) {
        double r50388 = x;
        double r50389 = -0.02278801870830147;
        bool r50390 = r50388 <= r50389;
        double r50391 = 0.02338074252416356;
        bool r50392 = r50388 <= r50391;
        double r50393 = !r50392;
        bool r50394 = r50390 || r50393;
        double r50395 = 1.0;
        double r50396 = cos(r50388);
        double r50397 = r50395 - r50396;
        double r50398 = log(r50397);
        double r50399 = exp(r50398);
        double r50400 = sin(r50388);
        double r50401 = r50399 / r50400;
        double r50402 = 0.04166666666666663;
        double r50403 = 3.0;
        double r50404 = pow(r50388, r50403);
        double r50405 = 0.004166666666666624;
        double r50406 = 5.0;
        double r50407 = pow(r50388, r50406);
        double r50408 = 0.5;
        double r50409 = r50408 * r50388;
        double r50410 = fma(r50405, r50407, r50409);
        double r50411 = fma(r50402, r50404, r50410);
        double r50412 = r50394 ? r50401 : r50411;
        return r50412;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 2 regimes
  2. if x < -0.02278801870830147 or 0.02338074252416356 < 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}\]
    7. Using strategy rm
    8. Applied add-exp-log1.1

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

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

    if -0.02278801870830147 < x < 0.02338074252416356

    1. Initial program 59.8

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

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

      \[\leadsto \log \left(e^{\frac{\color{blue}{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}}{\sin x}}\right)\]
    6. Simplified59.8

      \[\leadsto \log \left(e^{\frac{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\color{blue}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}}{\sin x}}\right)\]
    7. Taylor expanded around 0 0.0

      \[\leadsto \color{blue}{0.04166666666666662965923251249478198587894 \cdot {x}^{3} + \left(0.004166666666666624108117389368999283760786 \cdot {x}^{5} + 0.5 \cdot x\right)}\]
    8. Simplified0.0

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.04166666666666662965923251249478198587894, {x}^{3}, \mathsf{fma}\left(0.004166666666666624108117389368999283760786, {x}^{5}, 0.5 \cdot x\right)\right)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.0227880187083014697224658817731324234046 \lor \neg \left(x \le 0.02338074252416355927608471176881721476093\right):\\ \;\;\;\;\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(0.04166666666666662965923251249478198587894, {x}^{3}, \mathsf{fma}\left(0.004166666666666624108117389368999283760786, {x}^{5}, 0.5 \cdot x\right)\right)\\ \end{array}\]

Reproduce

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