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

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

\end{array}
double f(double x) {
        double r41960 = 1.0;
        double r41961 = x;
        double r41962 = cos(r41961);
        double r41963 = r41960 - r41962;
        double r41964 = sin(r41961);
        double r41965 = r41963 / r41964;
        return r41965;
}

double f(double x) {
        double r41966 = x;
        double r41967 = -0.02322762662824426;
        bool r41968 = r41966 <= r41967;
        double r41969 = 1.0;
        double r41970 = cos(r41966);
        double r41971 = r41969 - r41970;
        double r41972 = sin(r41966);
        double r41973 = r41971 / r41972;
        double r41974 = exp(r41973);
        double r41975 = sqrt(r41974);
        double r41976 = log(r41975);
        double r41977 = r41976 + r41976;
        double r41978 = 0.020904349932135746;
        bool r41979 = r41966 <= r41978;
        double r41980 = 0.041666666666666664;
        double r41981 = 3.0;
        double r41982 = pow(r41966, r41981);
        double r41983 = 0.004166666666666667;
        double r41984 = 5.0;
        double r41985 = pow(r41966, r41984);
        double r41986 = 0.5;
        double r41987 = r41986 * r41966;
        double r41988 = fma(r41983, r41985, r41987);
        double r41989 = fma(r41980, r41982, r41988);
        double r41990 = pow(r41969, r41981);
        double r41991 = pow(r41970, r41981);
        double r41992 = exp(r41991);
        double r41993 = log(r41992);
        double r41994 = r41990 - r41993;
        double r41995 = r41969 * r41970;
        double r41996 = fma(r41970, r41970, r41995);
        double r41997 = fma(r41969, r41969, r41996);
        double r41998 = r41997 * r41972;
        double r41999 = r41994 / r41998;
        double r42000 = r41979 ? r41989 : r41999;
        double r42001 = r41968 ? r41977 : r42000;
        return r42001;
}

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.02322762662824426

    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-sqr-sqrt1.2

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

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

    if -0.02322762662824426 < x < 0.020904349932135746

    1. Initial program 59.7

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

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

      \[\leadsto \frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\color{blue}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}}\]
    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)}}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.6

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

Reproduce

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