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

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

\end{array}
double f(double x) {
        double r58008 = 1.0;
        double r58009 = x;
        double r58010 = cos(r58009);
        double r58011 = r58008 - r58010;
        double r58012 = sin(r58009);
        double r58013 = r58011 / r58012;
        return r58013;
}

double f(double x) {
        double r58014 = x;
        double r58015 = -0.02373580573239165;
        bool r58016 = r58014 <= r58015;
        double r58017 = 1.0;
        double r58018 = 3.0;
        double r58019 = pow(r58017, r58018);
        double r58020 = cos(r58014);
        double r58021 = pow(r58020, r58018);
        double r58022 = r58019 - r58021;
        double r58023 = expm1(r58022);
        double r58024 = exp(r58023);
        double r58025 = log(r58024);
        double r58026 = log1p(r58025);
        double r58027 = r58017 * r58020;
        double r58028 = fma(r58020, r58020, r58027);
        double r58029 = fma(r58017, r58017, r58028);
        double r58030 = sin(r58014);
        double r58031 = r58029 * r58030;
        double r58032 = r58026 / r58031;
        double r58033 = 0.02029306521166552;
        bool r58034 = r58014 <= r58033;
        double r58035 = 0.041666666666666664;
        double r58036 = pow(r58014, r58018);
        double r58037 = 0.004166666666666667;
        double r58038 = 5.0;
        double r58039 = pow(r58014, r58038);
        double r58040 = 0.5;
        double r58041 = r58040 * r58014;
        double r58042 = fma(r58037, r58039, r58041);
        double r58043 = fma(r58035, r58036, r58042);
        double r58044 = r58017 + r58020;
        double r58045 = r58017 * r58017;
        double r58046 = fma(r58020, r58044, r58045);
        double r58047 = r58019 / r58046;
        double r58048 = r58021 / r58046;
        double r58049 = r58047 - r58048;
        double r58050 = r58049 / r58030;
        double r58051 = r58034 ? r58043 : r58050;
        double r58052 = r58016 ? r58032 : r58051;
        return r58052;
}

Error

Bits error versus x

Target

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

Derivation

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

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

      \[\leadsto \frac{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left({1}^{3} - {\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}\]
    8. Using strategy rm
    9. Applied add-log-exp1.0

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

    if -0.02373580573239165 < x < 0.02029306521166552

    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.02029306521166552 < 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 div-sub1.0

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

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

Reproduce

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