Average Error: 29.9 → 0.6
Time: 40.4s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.004972647923616153355086400011941805132665:\\ \;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \cos x \cdot \left(\cos x \cdot \cos x\right)}{\mathsf{fma}\left(\cos x, \frac{\cos x \cdot \cos x - 1 \cdot 1}{\cos x - 1}, 1 \cdot 1\right)}}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 1.24535172833726550875588223732393089449 \cdot 10^{-5}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{240}, {x}^{5}, x \cdot \mathsf{fma}\left(x, x \cdot \frac{1}{24}, \frac{1}{2}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \cos x \cdot \left(\cos x \cdot \cos x\right)}{\mathsf{fma}\left(\cos x, \frac{\cos x \cdot \cos x - 1 \cdot 1}{\cos x - 1}, 1 \cdot 1\right)}}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.004972647923616153355086400011941805132665:\\
\;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \cos x \cdot \left(\cos x \cdot \cos x\right)}{\mathsf{fma}\left(\cos x, \frac{\cos x \cdot \cos x - 1 \cdot 1}{\cos x - 1}, 1 \cdot 1\right)}}{\sin x}\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 1.24535172833726550875588223732393089449 \cdot 10^{-5}:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{240}, {x}^{5}, x \cdot \mathsf{fma}\left(x, x \cdot \frac{1}{24}, \frac{1}{2}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \cos x \cdot \left(\cos x \cdot \cos x\right)}{\mathsf{fma}\left(\cos x, \frac{\cos x \cdot \cos x - 1 \cdot 1}{\cos x - 1}, 1 \cdot 1\right)}}{\sin x}\\

\end{array}
double f(double x) {
        double r2847373 = 1.0;
        double r2847374 = x;
        double r2847375 = cos(r2847374);
        double r2847376 = r2847373 - r2847375;
        double r2847377 = sin(r2847374);
        double r2847378 = r2847376 / r2847377;
        return r2847378;
}

double f(double x) {
        double r2847379 = 1.0;
        double r2847380 = x;
        double r2847381 = cos(r2847380);
        double r2847382 = r2847379 - r2847381;
        double r2847383 = sin(r2847380);
        double r2847384 = r2847382 / r2847383;
        double r2847385 = -0.004972647923616153;
        bool r2847386 = r2847384 <= r2847385;
        double r2847387 = r2847379 * r2847379;
        double r2847388 = r2847379 * r2847387;
        double r2847389 = r2847381 * r2847381;
        double r2847390 = r2847381 * r2847389;
        double r2847391 = r2847388 - r2847390;
        double r2847392 = r2847389 - r2847387;
        double r2847393 = r2847381 - r2847379;
        double r2847394 = r2847392 / r2847393;
        double r2847395 = fma(r2847381, r2847394, r2847387);
        double r2847396 = r2847391 / r2847395;
        double r2847397 = r2847396 / r2847383;
        double r2847398 = 1.2453517283372655e-05;
        bool r2847399 = r2847384 <= r2847398;
        double r2847400 = 0.004166666666666667;
        double r2847401 = 5.0;
        double r2847402 = pow(r2847380, r2847401);
        double r2847403 = 0.041666666666666664;
        double r2847404 = r2847380 * r2847403;
        double r2847405 = 0.5;
        double r2847406 = fma(r2847380, r2847404, r2847405);
        double r2847407 = r2847380 * r2847406;
        double r2847408 = fma(r2847400, r2847402, r2847407);
        double r2847409 = r2847399 ? r2847408 : r2847397;
        double r2847410 = r2847386 ? r2847397 : r2847409;
        return r2847410;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 2 regimes
  2. if (/ (- 1.0 (cos x)) (sin x)) < -0.004972647923616153 or 1.2453517283372655e-05 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 1.0

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

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

      \[\leadsto \frac{\frac{\color{blue}{1 \cdot \left(1 \cdot 1\right) - \left(\cos x \cdot \cos x\right) \cdot \cos x}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}}{\sin x}\]
    5. Simplified1.1

      \[\leadsto \frac{\frac{1 \cdot \left(1 \cdot 1\right) - \left(\cos x \cdot \cos x\right) \cdot \cos x}{\color{blue}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right)}}}{\sin x}\]
    6. Using strategy rm
    7. Applied flip-+1.1

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

    if -0.004972647923616153 < (/ (- 1.0 (cos x)) (sin x)) < 1.2453517283372655e-05

    1. Initial program 60.0

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

      \[\leadsto \color{blue}{\log \left(e^{\frac{1 - \cos x}{\sin x}}\right)}\]
    4. 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)}\]
    5. Simplified0.1

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{240}, {x}^{5}, x \cdot \mathsf{fma}\left(x, x \cdot \frac{1}{24}, \frac{1}{2}\right)\right)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.004972647923616153355086400011941805132665:\\ \;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \cos x \cdot \left(\cos x \cdot \cos x\right)}{\mathsf{fma}\left(\cos x, \frac{\cos x \cdot \cos x - 1 \cdot 1}{\cos x - 1}, 1 \cdot 1\right)}}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 1.24535172833726550875588223732393089449 \cdot 10^{-5}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{240}, {x}^{5}, x \cdot \mathsf{fma}\left(x, x \cdot \frac{1}{24}, \frac{1}{2}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \cos x \cdot \left(\cos x \cdot \cos x\right)}{\mathsf{fma}\left(\cos x, \frac{\cos x \cdot \cos x - 1 \cdot 1}{\cos x - 1}, 1 \cdot 1\right)}}{\sin x}\\ \end{array}\]

Reproduce

herbie shell --seed 2019200 +o rules:numerics
(FPCore (x)
  :name "tanhf (example 3.4)"
  :herbie-expected 2

  :herbie-target
  (tan (/ x 2.0))

  (/ (- 1.0 (cos x)) (sin x)))