Average Error: 30.2 → 0.7
Time: 7.4s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0190087476176216581:\\ \;\;\;\;\log \left(\mathsf{expm1}\left(\mathsf{log1p}\left(e^{\frac{1 - \cos x}{\sin x}}\right)\right)\right)\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 4.47527012274922384 \cdot 10^{-5}:\\ \;\;\;\;\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{\log \left(e^{1 - \cos x}\right)}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0190087476176216581:\\
\;\;\;\;\log \left(\mathsf{expm1}\left(\mathsf{log1p}\left(e^{\frac{1 - \cos x}{\sin x}}\right)\right)\right)\\

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

\end{array}
double f(double x) {
        double r41513 = 1.0;
        double r41514 = x;
        double r41515 = cos(r41514);
        double r41516 = r41513 - r41515;
        double r41517 = sin(r41514);
        double r41518 = r41516 / r41517;
        return r41518;
}

double f(double x) {
        double r41519 = 1.0;
        double r41520 = x;
        double r41521 = cos(r41520);
        double r41522 = r41519 - r41521;
        double r41523 = sin(r41520);
        double r41524 = r41522 / r41523;
        double r41525 = -0.019008747617621658;
        bool r41526 = r41524 <= r41525;
        double r41527 = exp(r41524);
        double r41528 = log1p(r41527);
        double r41529 = expm1(r41528);
        double r41530 = log(r41529);
        double r41531 = 4.475270122749224e-05;
        bool r41532 = r41524 <= r41531;
        double r41533 = 0.041666666666666664;
        double r41534 = 3.0;
        double r41535 = pow(r41520, r41534);
        double r41536 = 0.004166666666666667;
        double r41537 = 5.0;
        double r41538 = pow(r41520, r41537);
        double r41539 = 0.5;
        double r41540 = r41539 * r41520;
        double r41541 = fma(r41536, r41538, r41540);
        double r41542 = fma(r41533, r41535, r41541);
        double r41543 = exp(r41522);
        double r41544 = log(r41543);
        double r41545 = r41544 / r41523;
        double r41546 = r41532 ? r41542 : r41545;
        double r41547 = r41526 ? r41530 : r41546;
        return r41547;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 3 regimes
  2. if (/ (- 1.0 (cos x)) (sin x)) < -0.019008747617621658

    1. Initial program 0.8

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

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

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

    if -0.019008747617621658 < (/ (- 1.0 (cos x)) (sin x)) < 4.475270122749224e-05

    1. Initial program 59.6

      \[\frac{1 - \cos x}{\sin x}\]
    2. Taylor expanded around 0 0.4

      \[\leadsto \color{blue}{\frac{1}{24} \cdot {x}^{3} + \left(\frac{1}{240} \cdot {x}^{5} + \frac{1}{2} \cdot x\right)}\]
    3. Simplified0.4

      \[\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 4.475270122749224e-05 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 1.0

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

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

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

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

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

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

Reproduce

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