Average Error: 29.7 → 0.6
Time: 8.1s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.008960255255002240112882638811697688652202:\\ \;\;\;\;\frac{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right) \cdot \frac{1 - \cos x}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 1.05393807315382309185841647192205527972 \cdot 10^{-4}:\\ \;\;\;\;\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}{\sin x} - \frac{\cos x}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.008960255255002240112882638811697688652202:\\
\;\;\;\;\frac{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right) \cdot \frac{1 - \cos x}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 1.05393807315382309185841647192205527972 \cdot 10^{-4}:\\
\;\;\;\;\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}{\sin x} - \frac{\cos x}{\sin x}\\

\end{array}
double f(double x) {
        double r59910 = 1.0;
        double r59911 = x;
        double r59912 = cos(r59911);
        double r59913 = r59910 - r59912;
        double r59914 = sin(r59911);
        double r59915 = r59913 / r59914;
        return r59915;
}

double f(double x) {
        double r59916 = 1.0;
        double r59917 = x;
        double r59918 = cos(r59917);
        double r59919 = r59916 - r59918;
        double r59920 = sin(r59917);
        double r59921 = r59919 / r59920;
        double r59922 = -0.00896025525500224;
        bool r59923 = r59921 <= r59922;
        double r59924 = r59916 + r59918;
        double r59925 = r59916 * r59916;
        double r59926 = fma(r59918, r59924, r59925);
        double r59927 = r59919 / r59926;
        double r59928 = r59926 * r59927;
        double r59929 = r59928 / r59920;
        double r59930 = 0.00010539380731538231;
        bool r59931 = r59921 <= r59930;
        double r59932 = 0.041666666666666664;
        double r59933 = 3.0;
        double r59934 = pow(r59917, r59933);
        double r59935 = 0.004166666666666667;
        double r59936 = 5.0;
        double r59937 = pow(r59917, r59936);
        double r59938 = 0.5;
        double r59939 = r59938 * r59917;
        double r59940 = fma(r59935, r59937, r59939);
        double r59941 = fma(r59932, r59934, r59940);
        double r59942 = r59916 / r59920;
        double r59943 = r59918 / r59920;
        double r59944 = r59942 - r59943;
        double r59945 = r59931 ? r59941 : r59944;
        double r59946 = r59923 ? r59929 : r59945;
        return r59946;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 3 regimes
  2. if (/ (- 1.0 (cos x)) (sin x)) < -0.00896025525500224

    1. Initial program 0.8

      \[\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 *-un-lft-identity1.0

      \[\leadsto \frac{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\color{blue}{1 \cdot \mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}}{\sin x}\]
    7. Applied difference-cubes0.9

      \[\leadsto \frac{\frac{\color{blue}{\left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right) \cdot \left(1 - \cos x\right)}}{1 \cdot \mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\]
    8. Applied times-frac1.0

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

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

    if -0.00896025525500224 < (/ (- 1.0 (cos x)) (sin x)) < 0.00010539380731538231

    1. Initial program 59.8

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

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

      \[\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.00010539380731538231 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 1.0

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

      \[\leadsto \color{blue}{\frac{1}{\sin x} - \frac{\cos x}{\sin x}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.008960255255002240112882638811697688652202:\\ \;\;\;\;\frac{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right) \cdot \frac{1 - \cos x}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 1.05393807315382309185841647192205527972 \cdot 10^{-4}:\\ \;\;\;\;\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}{\sin x} - \frac{\cos x}{\sin x}\\ \end{array}\]

Reproduce

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