Average Error: 30.0 → 0.7
Time: 26.1s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.01135426828358780323990906424569402588531:\\ \;\;\;\;\frac{\frac{e^{\log \left(\left(1 \cdot 1\right) \cdot 1 - \mathsf{fma}\left(\frac{1}{2}, \cos \left(x + x\right), \frac{1}{2}\right) \cdot \cos x\right)}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 2.716774483654111734418070084415980369741 \cdot 10^{-8}:\\ \;\;\;\;\mathsf{fma}\left(x, \mathsf{fma}\left(x \cdot x, \frac{1}{24}, \frac{1}{2}\right), \frac{1}{240} \cdot {x}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\frac{-1}{2}, \mathsf{fma}\left(\cos \left(x + x\right), \cos x, \cos x\right), 1\right) + \mathsf{fma}\left(-\cos x, \frac{1}{2} + \cos \left(x + x\right) \cdot \frac{1}{2}, \cos x \cdot \left(\frac{1}{2} + \cos \left(x + x\right) \cdot \frac{1}{2}\right)\right)}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \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.01135426828358780323990906424569402588531:\\
\;\;\;\;\frac{\frac{e^{\log \left(\left(1 \cdot 1\right) \cdot 1 - \mathsf{fma}\left(\frac{1}{2}, \cos \left(x + x\right), \frac{1}{2}\right) \cdot \cos x\right)}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 2.716774483654111734418070084415980369741 \cdot 10^{-8}:\\
\;\;\;\;\mathsf{fma}\left(x, \mathsf{fma}\left(x \cdot x, \frac{1}{24}, \frac{1}{2}\right), \frac{1}{240} \cdot {x}^{5}\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(\frac{-1}{2}, \mathsf{fma}\left(\cos \left(x + x\right), \cos x, \cos x\right), 1\right) + \mathsf{fma}\left(-\cos x, \frac{1}{2} + \cos \left(x + x\right) \cdot \frac{1}{2}, \cos x \cdot \left(\frac{1}{2} + \cos \left(x + x\right) \cdot \frac{1}{2}\right)\right)}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\\

\end{array}
double f(double x) {
        double r8540573 = 1.0;
        double r8540574 = x;
        double r8540575 = cos(r8540574);
        double r8540576 = r8540573 - r8540575;
        double r8540577 = sin(r8540574);
        double r8540578 = r8540576 / r8540577;
        return r8540578;
}

double f(double x) {
        double r8540579 = 1.0;
        double r8540580 = x;
        double r8540581 = cos(r8540580);
        double r8540582 = r8540579 - r8540581;
        double r8540583 = sin(r8540580);
        double r8540584 = r8540582 / r8540583;
        double r8540585 = -0.011354268283587803;
        bool r8540586 = r8540584 <= r8540585;
        double r8540587 = r8540579 * r8540579;
        double r8540588 = r8540587 * r8540579;
        double r8540589 = 0.5;
        double r8540590 = r8540580 + r8540580;
        double r8540591 = cos(r8540590);
        double r8540592 = fma(r8540589, r8540591, r8540589);
        double r8540593 = r8540592 * r8540581;
        double r8540594 = r8540588 - r8540593;
        double r8540595 = log(r8540594);
        double r8540596 = exp(r8540595);
        double r8540597 = r8540581 * r8540581;
        double r8540598 = r8540579 * r8540581;
        double r8540599 = r8540597 + r8540598;
        double r8540600 = r8540587 + r8540599;
        double r8540601 = r8540596 / r8540600;
        double r8540602 = r8540601 / r8540583;
        double r8540603 = 2.7167744836541117e-08;
        bool r8540604 = r8540584 <= r8540603;
        double r8540605 = r8540580 * r8540580;
        double r8540606 = 0.041666666666666664;
        double r8540607 = fma(r8540605, r8540606, r8540589);
        double r8540608 = 0.004166666666666667;
        double r8540609 = 5.0;
        double r8540610 = pow(r8540580, r8540609);
        double r8540611 = r8540608 * r8540610;
        double r8540612 = fma(r8540580, r8540607, r8540611);
        double r8540613 = -0.5;
        double r8540614 = fma(r8540591, r8540581, r8540581);
        double r8540615 = fma(r8540613, r8540614, r8540579);
        double r8540616 = -r8540581;
        double r8540617 = r8540591 * r8540589;
        double r8540618 = r8540589 + r8540617;
        double r8540619 = r8540581 * r8540618;
        double r8540620 = fma(r8540616, r8540618, r8540619);
        double r8540621 = r8540615 + r8540620;
        double r8540622 = r8540621 / r8540600;
        double r8540623 = r8540622 / r8540583;
        double r8540624 = r8540604 ? r8540612 : r8540623;
        double r8540625 = r8540586 ? r8540602 : r8540624;
        return r8540625;
}

Error

Bits error versus x

Target

Original30.0
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.011354268283587803

    1. Initial program 0.9

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

      \[\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.0

      \[\leadsto \frac{\frac{\color{blue}{\left(1 \cdot 1\right) \cdot 1 - \left(\cos x \cdot \cos x\right) \cdot \cos x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\]
    5. Using strategy rm
    6. Applied sqr-cos0.9

      \[\leadsto \frac{\frac{\left(1 \cdot 1\right) \cdot 1 - \color{blue}{\left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(2 \cdot x\right)\right)} \cdot \cos x}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\]
    7. Simplified0.9

      \[\leadsto \frac{\frac{\left(1 \cdot 1\right) \cdot 1 - \left(\frac{1}{2} + \color{blue}{\cos \left(x + x\right) \cdot \frac{1}{2}}\right) \cdot \cos x}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\]
    8. Using strategy rm
    9. Applied add-exp-log0.9

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

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

    if -0.011354268283587803 < (/ (- 1.0 (cos x)) (sin x)) < 2.7167744836541117e-08

    1. Initial program 59.9

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x \cdot x, \frac{1}{24}, \frac{1}{2}\right), \frac{1}{240} \cdot {x}^{5}\right)}\]

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

    1. Initial program 1.4

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

      \[\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.4

      \[\leadsto \frac{\frac{\color{blue}{\left(1 \cdot 1\right) \cdot 1 - \left(\cos x \cdot \cos x\right) \cdot \cos x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\]
    5. Using strategy rm
    6. Applied sqr-cos1.4

      \[\leadsto \frac{\frac{\left(1 \cdot 1\right) \cdot 1 - \color{blue}{\left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(2 \cdot x\right)\right)} \cdot \cos x}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\]
    7. Simplified1.4

      \[\leadsto \frac{\frac{\left(1 \cdot 1\right) \cdot 1 - \left(\frac{1}{2} + \color{blue}{\cos \left(x + x\right) \cdot \frac{1}{2}}\right) \cdot \cos x}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\]
    8. Using strategy rm
    9. Applied prod-diff1.4

      \[\leadsto \frac{\frac{\color{blue}{\mathsf{fma}\left(1 \cdot 1, 1, -\cos x \cdot \left(\frac{1}{2} + \cos \left(x + x\right) \cdot \frac{1}{2}\right)\right) + \mathsf{fma}\left(-\cos x, \frac{1}{2} + \cos \left(x + x\right) \cdot \frac{1}{2}, \cos x \cdot \left(\frac{1}{2} + \cos \left(x + x\right) \cdot \frac{1}{2}\right)\right)}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\]
    10. Taylor expanded around inf 1.4

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

      \[\leadsto \frac{\frac{\color{blue}{\mathsf{fma}\left(\frac{-1}{2}, \mathsf{fma}\left(\cos \left(x + x\right), \cos x, \cos x\right), 1\right)} + \mathsf{fma}\left(-\cos x, \frac{1}{2} + \cos \left(x + x\right) \cdot \frac{1}{2}, \cos x \cdot \left(\frac{1}{2} + \cos \left(x + x\right) \cdot \frac{1}{2}\right)\right)}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \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.01135426828358780323990906424569402588531:\\ \;\;\;\;\frac{\frac{e^{\log \left(\left(1 \cdot 1\right) \cdot 1 - \mathsf{fma}\left(\frac{1}{2}, \cos \left(x + x\right), \frac{1}{2}\right) \cdot \cos x\right)}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 2.716774483654111734418070084415980369741 \cdot 10^{-8}:\\ \;\;\;\;\mathsf{fma}\left(x, \mathsf{fma}\left(x \cdot x, \frac{1}{24}, \frac{1}{2}\right), \frac{1}{240} \cdot {x}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\frac{-1}{2}, \mathsf{fma}\left(\cos \left(x + x\right), \cos x, \cos x\right), 1\right) + \mathsf{fma}\left(-\cos x, \frac{1}{2} + \cos \left(x + x\right) \cdot \frac{1}{2}, \cos x \cdot \left(\frac{1}{2} + \cos \left(x + x\right) \cdot \frac{1}{2}\right)\right)}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\\ \end{array}\]

Reproduce

herbie shell --seed 2019173 +o rules:numerics
(FPCore (x)
  :name "tanhf (example 3.4)"
  :herbie-expected 2

  :herbie-target
  (tan (/ x 2.0))

  (/ (- 1.0 (cos x)) (sin x)))