Average Error: 30.1 → 0.5
Time: 14.3s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.0195414650842071873:\\ \;\;\;\;\frac{\mathsf{log1p}\left(\mathsf{expm1}\left(1 - \cos x\right)\right)}{\sin x}\\ \mathbf{elif}\;x \le 0.023267676544555443:\\ \;\;\;\;\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{{e}^{\left(\log \left(1 - \cos x\right)\right)}}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.0195414650842071873:\\
\;\;\;\;\frac{\mathsf{log1p}\left(\mathsf{expm1}\left(1 - \cos x\right)\right)}{\sin x}\\

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

\end{array}
double f(double x) {
        double r83934 = 1.0;
        double r83935 = x;
        double r83936 = cos(r83935);
        double r83937 = r83934 - r83936;
        double r83938 = sin(r83935);
        double r83939 = r83937 / r83938;
        return r83939;
}

double f(double x) {
        double r83940 = x;
        double r83941 = -0.019541465084207187;
        bool r83942 = r83940 <= r83941;
        double r83943 = 1.0;
        double r83944 = cos(r83940);
        double r83945 = r83943 - r83944;
        double r83946 = expm1(r83945);
        double r83947 = log1p(r83946);
        double r83948 = sin(r83940);
        double r83949 = r83947 / r83948;
        double r83950 = 0.023267676544555443;
        bool r83951 = r83940 <= r83950;
        double r83952 = 0.041666666666666664;
        double r83953 = 3.0;
        double r83954 = pow(r83940, r83953);
        double r83955 = 0.004166666666666667;
        double r83956 = 5.0;
        double r83957 = pow(r83940, r83956);
        double r83958 = 0.5;
        double r83959 = r83958 * r83940;
        double r83960 = fma(r83955, r83957, r83959);
        double r83961 = fma(r83952, r83954, r83960);
        double r83962 = exp(1.0);
        double r83963 = log(r83945);
        double r83964 = pow(r83962, r83963);
        double r83965 = r83964 / r83948;
        double r83966 = r83951 ? r83961 : r83965;
        double r83967 = r83942 ? r83949 : r83966;
        return r83967;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied add-exp-log0.9

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

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

      \[\leadsto \frac{e^{\color{blue}{1 \cdot \log \left(1 - \cos x\right)}}}{\sin x}\]
    7. Applied exp-prod0.9

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

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

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

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

    if -0.019541465084207187 < x < 0.023267676544555443

    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.023267676544555443 < x

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied add-exp-log0.9

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

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

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

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

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

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

Reproduce

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