Average Error: 29.4 → 1.0
Time: 14.4s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0444958089144920146:\\ \;\;\;\;\frac{\mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x + 1\right)\right) \cdot \left(1 - \cos x\right)}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 0.0033326395732868408:\\ \;\;\;\;\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{log1p}\left(\mathsf{expm1}\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.0444958089144920146:\\
\;\;\;\;\frac{\mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x + 1\right)\right) \cdot \left(1 - \cos x\right)}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 0.0033326395732868408:\\
\;\;\;\;\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{log1p}\left(\mathsf{expm1}\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 r257 = 1.0;
        double r258 = x;
        double r259 = cos(r258);
        double r260 = r257 - r259;
        double r261 = sin(r258);
        double r262 = r260 / r261;
        return r262;
}

double f(double x) {
        double r263 = 1.0;
        double r264 = x;
        double r265 = cos(r264);
        double r266 = r263 - r265;
        double r267 = sin(r264);
        double r268 = r266 / r267;
        double r269 = -0.044495808914492015;
        bool r270 = r268 <= r269;
        double r271 = r265 + r263;
        double r272 = r265 * r271;
        double r273 = fma(r263, r263, r272);
        double r274 = r273 * r266;
        double r275 = r263 * r265;
        double r276 = fma(r265, r265, r275);
        double r277 = fma(r263, r263, r276);
        double r278 = r277 * r267;
        double r279 = r274 / r278;
        double r280 = 0.003332639573286841;
        bool r281 = r268 <= r280;
        double r282 = 0.041666666666666664;
        double r283 = 3.0;
        double r284 = pow(r264, r283);
        double r285 = 0.004166666666666667;
        double r286 = 5.0;
        double r287 = pow(r264, r286);
        double r288 = 0.5;
        double r289 = r288 * r264;
        double r290 = fma(r285, r287, r289);
        double r291 = fma(r282, r284, r290);
        double r292 = pow(r263, r283);
        double r293 = pow(r265, r283);
        double r294 = expm1(r293);
        double r295 = log1p(r294);
        double r296 = r292 - r295;
        double r297 = r296 / r278;
        double r298 = r281 ? r291 : r297;
        double r299 = r270 ? r279 : r298;
        return r299;
}

Error

Bits error versus x

Target

Original29.4
Target0.0
Herbie1.0
\[\tan \left(\frac{x}{2}\right)\]

Derivation

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

    1. Initial program 0.7

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

      \[\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/0.8

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

      \[\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 difference-cubes0.8

      \[\leadsto \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)}}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}\]
    8. Simplified0.8

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

    if -0.044495808914492015 < (/ (- 1.0 (cos x)) (sin x)) < 0.003332639573286841

    1. Initial program 59.0

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

      \[\leadsto \color{blue}{\frac{1}{24} \cdot {x}^{3} + \left(\frac{1}{240} \cdot {x}^{5} + \frac{1}{2} \cdot x\right)}\]
    3. Simplified1.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.003332639573286841 < (/ (- 1.0 (cos x)) (sin 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 log1p-expm1-u1.0

      \[\leadsto \frac{{1}^{3} - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\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 simplification1.0

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0444958089144920146:\\ \;\;\;\;\frac{\mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x + 1\right)\right) \cdot \left(1 - \cos x\right)}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 0.0033326395732868408:\\ \;\;\;\;\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{log1p}\left(\mathsf{expm1}\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 2020025 +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)))