Average Error: 29.9 → 0.5
Time: 24.8s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.02184956452552735267635952709497360046953:\\ \;\;\;\;\frac{\mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x + 1\right)\right) \cdot \left(1 - \cos x\right)}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x + 1\right)\right)}\\ \mathbf{elif}\;x \le 0.02174222265971261988659612995888892328367:\\ \;\;\;\;\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} - \left(\cos x \cdot \cos x\right) \cdot \cos x}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x + 1\right)\right)}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.02184956452552735267635952709497360046953:\\
\;\;\;\;\frac{\mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x + 1\right)\right) \cdot \left(1 - \cos x\right)}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x + 1\right)\right)}\\

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

\end{array}
double f(double x) {
        double r74327 = 1.0;
        double r74328 = x;
        double r74329 = cos(r74328);
        double r74330 = r74327 - r74329;
        double r74331 = sin(r74328);
        double r74332 = r74330 / r74331;
        return r74332;
}

double f(double x) {
        double r74333 = x;
        double r74334 = -0.021849564525527353;
        bool r74335 = r74333 <= r74334;
        double r74336 = 1.0;
        double r74337 = cos(r74333);
        double r74338 = r74337 + r74336;
        double r74339 = r74337 * r74338;
        double r74340 = fma(r74336, r74336, r74339);
        double r74341 = r74336 - r74337;
        double r74342 = r74340 * r74341;
        double r74343 = sin(r74333);
        double r74344 = r74343 * r74340;
        double r74345 = r74342 / r74344;
        double r74346 = 0.02174222265971262;
        bool r74347 = r74333 <= r74346;
        double r74348 = 0.041666666666666664;
        double r74349 = 3.0;
        double r74350 = pow(r74333, r74349);
        double r74351 = 0.004166666666666667;
        double r74352 = 5.0;
        double r74353 = pow(r74333, r74352);
        double r74354 = 0.5;
        double r74355 = r74354 * r74333;
        double r74356 = fma(r74351, r74353, r74355);
        double r74357 = fma(r74348, r74350, r74356);
        double r74358 = pow(r74336, r74349);
        double r74359 = r74337 * r74337;
        double r74360 = r74359 * r74337;
        double r74361 = r74358 - r74360;
        double r74362 = r74361 / r74344;
        double r74363 = r74347 ? r74357 : r74362;
        double r74364 = r74335 ? r74345 : r74363;
        return r74364;
}

Error

Bits error versus x

Target

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

Derivation

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

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

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

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

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

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

    if -0.021849564525527353 < x < 0.02174222265971262

    1. Initial program 59.9

      \[\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.02174222265971262 < 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}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x + 1\right)\right)}}\]
    6. Using strategy rm
    7. Applied add-cbrt-cube1.4

      \[\leadsto \frac{{1}^{3} - {\color{blue}{\left(\sqrt[3]{\left(\cos x \cdot \cos x\right) \cdot \cos x}\right)}}^{3}}{\sin x \cdot \mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x + 1\right)\right)}\]
    8. Applied rem-cube-cbrt1.0

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

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

Reproduce

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