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

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

\end{array}
double f(double x) {
        double r59950 = 1.0;
        double r59951 = x;
        double r59952 = cos(r59951);
        double r59953 = r59950 - r59952;
        double r59954 = sin(r59951);
        double r59955 = r59953 / r59954;
        return r59955;
}

double f(double x) {
        double r59956 = x;
        double r59957 = -0.020520125187230325;
        bool r59958 = r59956 <= r59957;
        double r59959 = 1.0;
        double r59960 = 3.0;
        double r59961 = pow(r59959, r59960);
        double r59962 = cos(r59956);
        double r59963 = pow(r59962, r59960);
        double r59964 = r59961 - r59963;
        double r59965 = log(r59964);
        double r59966 = r59959 + r59962;
        double r59967 = r59959 * r59959;
        double r59968 = fma(r59962, r59966, r59967);
        double r59969 = log(r59968);
        double r59970 = r59965 - r59969;
        double r59971 = exp(r59970);
        double r59972 = sin(r59956);
        double r59973 = r59971 / r59972;
        double r59974 = 0.015121154527709535;
        bool r59975 = r59956 <= r59974;
        double r59976 = 0.041666666666666664;
        double r59977 = pow(r59956, r59960);
        double r59978 = 0.004166666666666667;
        double r59979 = 5.0;
        double r59980 = pow(r59956, r59979);
        double r59981 = 0.5;
        double r59982 = r59981 * r59956;
        double r59983 = fma(r59978, r59980, r59982);
        double r59984 = fma(r59976, r59977, r59983);
        double r59985 = 2.0;
        double r59986 = pow(r59962, r59985);
        double r59987 = r59967 - r59986;
        double r59988 = r59959 - r59962;
        double r59989 = r59987 / r59988;
        double r59990 = fma(r59962, r59989, r59967);
        double r59991 = r59964 / r59990;
        double r59992 = log(r59991);
        double r59993 = exp(r59992);
        double r59994 = r59993 / r59972;
        double r59995 = r59975 ? r59984 : r59994;
        double r59996 = r59958 ? r59973 : r59995;
        return r59996;
}

Error

Bits error versus x

Target

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

Derivation

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

    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(\cos x, 1 + \cos x, 1 \cdot 1\right)}}}{\sin x}\]
    5. Using strategy rm
    6. Applied add-exp-log1.1

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

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

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

      \[\leadsto \frac{e^{\color{blue}{\log \left(\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}\right)}}}{\sin x}\]
    10. Using strategy rm
    11. Applied log-div1.1

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

    if -0.020520125187230325 < x < 0.015121154527709535

    1. Initial program 59.9

      \[\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.015121154527709535 < x

    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{{1}^{3} - {\left(\cos x\right)}^{3}}{\color{blue}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}}{\sin x}\]
    5. Using strategy rm
    6. Applied add-exp-log1.0

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

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

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

      \[\leadsto \frac{e^{\color{blue}{\log \left(\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}\right)}}}{\sin x}\]
    10. Using strategy rm
    11. Applied flip-+1.0

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

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

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

Reproduce

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