Average Error: 29.7 → 0.7
Time: 14.6s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0161525551537130101:\\ \;\;\;\;\frac{\frac{\log \left(e^{{1}^{3} - {\left(\cos x\right)}^{3}}\right)}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 8.0836609335251401 \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{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(1, 1, \cos x \cdot \frac{1 \cdot 1 - {\left(\cos x\right)}^{2}}{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.0161525551537130101:\\
\;\;\;\;\frac{\frac{\log \left(e^{{1}^{3} - {\left(\cos x\right)}^{3}}\right)}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}{\sin x}\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 8.0836609335251401 \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{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(1, 1, \cos x \cdot \frac{1 \cdot 1 - {\left(\cos x\right)}^{2}}{1 - \cos x}\right)}}{\sin x}\\

\end{array}
double f(double x) {
        double r66561 = 1.0;
        double r66562 = x;
        double r66563 = cos(r66562);
        double r66564 = r66561 - r66563;
        double r66565 = sin(r66562);
        double r66566 = r66564 / r66565;
        return r66566;
}

double f(double x) {
        double r66567 = 1.0;
        double r66568 = x;
        double r66569 = cos(r66568);
        double r66570 = r66567 - r66569;
        double r66571 = sin(r66568);
        double r66572 = r66570 / r66571;
        double r66573 = -0.01615255515371301;
        bool r66574 = r66572 <= r66573;
        double r66575 = 3.0;
        double r66576 = pow(r66567, r66575);
        double r66577 = pow(r66569, r66575);
        double r66578 = r66576 - r66577;
        double r66579 = exp(r66578);
        double r66580 = log(r66579);
        double r66581 = r66567 + r66569;
        double r66582 = r66569 * r66581;
        double r66583 = fma(r66567, r66567, r66582);
        double r66584 = r66580 / r66583;
        double r66585 = r66584 / r66571;
        double r66586 = 8.08366093352514e-05;
        bool r66587 = r66572 <= r66586;
        double r66588 = 0.041666666666666664;
        double r66589 = pow(r66568, r66575);
        double r66590 = 0.004166666666666667;
        double r66591 = 5.0;
        double r66592 = pow(r66568, r66591);
        double r66593 = 0.5;
        double r66594 = r66593 * r66568;
        double r66595 = fma(r66590, r66592, r66594);
        double r66596 = fma(r66588, r66589, r66595);
        double r66597 = r66567 * r66567;
        double r66598 = 2.0;
        double r66599 = pow(r66569, r66598);
        double r66600 = r66597 - r66599;
        double r66601 = r66600 / r66570;
        double r66602 = r66569 * r66601;
        double r66603 = fma(r66567, r66567, r66602);
        double r66604 = r66578 / r66603;
        double r66605 = r66604 / r66571;
        double r66606 = r66587 ? r66596 : r66605;
        double r66607 = r66574 ? r66585 : r66606;
        return r66607;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.8

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

      \[\leadsto \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}\]
    4. Simplified0.9

      \[\leadsto \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}\]
    5. Using strategy rm
    6. Applied add-log-exp1.0

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

      \[\leadsto \frac{\frac{\color{blue}{\log \left(e^{{1}^{3}}\right)} - \log \left(e^{{\left(\cos x\right)}^{3}}\right)}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}{\sin x}\]
    8. Applied diff-log1.0

      \[\leadsto \frac{\frac{\color{blue}{\log \left(\frac{e^{{1}^{3}}}{e^{{\left(\cos x\right)}^{3}}}\right)}}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}{\sin x}\]
    9. Simplified0.9

      \[\leadsto \frac{\frac{\log \color{blue}{\left(e^{{1}^{3} - {\left(\cos x\right)}^{3}}\right)}}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}{\sin x}\]

    if -0.01615255515371301 < (/ (- 1.0 (cos x)) (sin x)) < 8.08366093352514e-05

    1. Initial program 59.7

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

    1. Initial program 1.0

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

      \[\leadsto \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}\]
    4. Simplified1.1

      \[\leadsto \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}\]
    5. Using strategy rm
    6. Applied flip-+1.1

      \[\leadsto \frac{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(1, 1, \cos x \cdot \color{blue}{\frac{1 \cdot 1 - \cos x \cdot \cos x}{1 - \cos x}}\right)}}{\sin x}\]
    7. Simplified1.1

      \[\leadsto \frac{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(1, 1, \cos x \cdot \frac{\color{blue}{1 \cdot 1 - {\left(\cos x\right)}^{2}}}{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.0161525551537130101:\\ \;\;\;\;\frac{\frac{\log \left(e^{{1}^{3} - {\left(\cos x\right)}^{3}}\right)}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 8.0836609335251401 \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{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(1, 1, \cos x \cdot \frac{1 \cdot 1 - {\left(\cos x\right)}^{2}}{1 - \cos x}\right)}}{\sin x}\\ \end{array}\]

Reproduce

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