Average Error: 30.3 → 0.8
Time: 7.4s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.00408598860077935295:\\ \;\;\;\;\log \left(e^{\frac{1}{\sin x} - \frac{\cos x}{\sin x}}\right)\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le -0.0:\\ \;\;\;\;\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} - \mathsf{expm1}\left(\mathsf{log1p}\left({\left(\cos x\right)}^{3}\right)\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}\;\frac{1 - \cos x}{\sin x} \le -0.00408598860077935295:\\
\;\;\;\;\log \left(e^{\frac{1}{\sin x} - \frac{\cos x}{\sin x}}\right)\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le -0.0:\\
\;\;\;\;\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} - \mathsf{expm1}\left(\mathsf{log1p}\left({\left(\cos x\right)}^{3}\right)\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 r63934 = 1.0;
        double r63935 = x;
        double r63936 = cos(r63935);
        double r63937 = r63934 - r63936;
        double r63938 = sin(r63935);
        double r63939 = r63937 / r63938;
        return r63939;
}

double f(double x) {
        double r63940 = 1.0;
        double r63941 = x;
        double r63942 = cos(r63941);
        double r63943 = r63940 - r63942;
        double r63944 = sin(r63941);
        double r63945 = r63943 / r63944;
        double r63946 = -0.004085988600779353;
        bool r63947 = r63945 <= r63946;
        double r63948 = r63940 / r63944;
        double r63949 = r63942 / r63944;
        double r63950 = r63948 - r63949;
        double r63951 = exp(r63950);
        double r63952 = log(r63951);
        double r63953 = -0.0;
        bool r63954 = r63945 <= r63953;
        double r63955 = 0.041666666666666664;
        double r63956 = 3.0;
        double r63957 = pow(r63941, r63956);
        double r63958 = 0.004166666666666667;
        double r63959 = 5.0;
        double r63960 = pow(r63941, r63959);
        double r63961 = 0.5;
        double r63962 = r63961 * r63941;
        double r63963 = fma(r63958, r63960, r63962);
        double r63964 = fma(r63955, r63957, r63963);
        double r63965 = pow(r63940, r63956);
        double r63966 = pow(r63942, r63956);
        double r63967 = log1p(r63966);
        double r63968 = expm1(r63967);
        double r63969 = r63965 - r63968;
        double r63970 = r63940 * r63942;
        double r63971 = fma(r63942, r63942, r63970);
        double r63972 = fma(r63940, r63940, r63971);
        double r63973 = r63972 * r63944;
        double r63974 = r63969 / r63973;
        double r63975 = r63954 ? r63964 : r63974;
        double r63976 = r63947 ? r63952 : r63975;
        return r63976;
}

Error

Bits error versus x

Target

Original30.3
Target0
Herbie0.8
\[\tan \left(\frac{x}{2}\right)\]

Derivation

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

    1. Initial program 0.9

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

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

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

    if -0.004085988600779353 < (/ (- 1.0 (cos x)) (sin x)) < -0.0

    1. Initial program 60.1

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

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

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

    1. Initial program 1.5

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

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

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

      \[\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 expm1-log1p-u1.7

      \[\leadsto \frac{{1}^{3} - \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left({\left(\cos x\right)}^{3}\right)\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.8

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