Average Error: 30.4 → 0.6
Time: 23.2s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.02364773704659709624431584984449727926403:\\ \;\;\;\;\frac{\sqrt{e^{\log \left(1 - \cos x\right)}}}{\sin x} \cdot \sqrt{1 - \cos x}\\ \mathbf{elif}\;x \le 0.02077111946200701705911306760299339657649:\\ \;\;\;\;\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}^{3} - {\left(\cos x\right)}^{3}}\right)}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.02364773704659709624431584984449727926403:\\
\;\;\;\;\frac{\sqrt{e^{\log \left(1 - \cos x\right)}}}{\sin x} \cdot \sqrt{1 - \cos x}\\

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

\end{array}
double f(double x) {
        double r50630 = 1.0;
        double r50631 = x;
        double r50632 = cos(r50631);
        double r50633 = r50630 - r50632;
        double r50634 = sin(r50631);
        double r50635 = r50633 / r50634;
        return r50635;
}

double f(double x) {
        double r50636 = x;
        double r50637 = -0.023647737046597096;
        bool r50638 = r50636 <= r50637;
        double r50639 = 1.0;
        double r50640 = cos(r50636);
        double r50641 = r50639 - r50640;
        double r50642 = log(r50641);
        double r50643 = exp(r50642);
        double r50644 = sqrt(r50643);
        double r50645 = sin(r50636);
        double r50646 = r50644 / r50645;
        double r50647 = sqrt(r50641);
        double r50648 = r50646 * r50647;
        double r50649 = 0.020771119462007017;
        bool r50650 = r50636 <= r50649;
        double r50651 = 0.041666666666666664;
        double r50652 = 3.0;
        double r50653 = pow(r50636, r50652);
        double r50654 = 0.004166666666666667;
        double r50655 = 5.0;
        double r50656 = pow(r50636, r50655);
        double r50657 = 0.5;
        double r50658 = r50657 * r50636;
        double r50659 = fma(r50654, r50656, r50658);
        double r50660 = fma(r50651, r50653, r50659);
        double r50661 = pow(r50639, r50652);
        double r50662 = pow(r50640, r50652);
        double r50663 = r50661 - r50662;
        double r50664 = exp(r50663);
        double r50665 = log(r50664);
        double r50666 = r50639 + r50640;
        double r50667 = r50640 * r50666;
        double r50668 = fma(r50639, r50639, r50667);
        double r50669 = r50645 * r50668;
        double r50670 = r50665 / r50669;
        double r50671 = r50650 ? r50660 : r50670;
        double r50672 = r50638 ? r50648 : r50671;
        return r50672;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 3 regimes
  2. if x < -0.023647737046597096

    1. Initial program 0.9

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

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

      \[\leadsto \log \left(e^{\frac{\color{blue}{e^{\log \left(1 - \cos x\right)}}}{\sin x}}\right)\]
    6. Using strategy rm
    7. Applied *-un-lft-identity1.1

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

      \[\leadsto \log \left(e^{\frac{\color{blue}{\sqrt{e^{\log \left(1 - \cos x\right)}} \cdot \sqrt{e^{\log \left(1 - \cos x\right)}}}}{1 \cdot \sin x}}\right)\]
    9. Applied times-frac1.3

      \[\leadsto \log \left(e^{\color{blue}{\frac{\sqrt{e^{\log \left(1 - \cos x\right)}}}{1} \cdot \frac{\sqrt{e^{\log \left(1 - \cos x\right)}}}{\sin x}}}\right)\]
    10. Applied exp-prod1.3

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

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

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

    if -0.023647737046597096 < x < 0.020771119462007017

    1. Initial program 60.0

      \[\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)}\]

    if 0.020771119462007017 < x

    1. Initial program 0.9

      \[\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. Applied associate-/l/1.1

      \[\leadsto \color{blue}{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\sin x \cdot \left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right)}}\]
    5. Simplified1.1

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.02364773704659709624431584984449727926403:\\ \;\;\;\;\frac{\sqrt{e^{\log \left(1 - \cos x\right)}}}{\sin x} \cdot \sqrt{1 - \cos x}\\ \mathbf{elif}\;x \le 0.02077111946200701705911306760299339657649:\\ \;\;\;\;\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}^{3} - {\left(\cos x\right)}^{3}}\right)}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(1 + \cos x\right)\right)}\\ \end{array}\]

Reproduce

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