Average Error: 29.7 → 1.0
Time: 7.4s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0443112716287218308:\\ \;\;\;\;\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, \log \left({\left(e^{\cos x}\right)}^{\left(1 + \cos x\right)}\right)\right) \cdot \sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 0.002190375129707059:\\ \;\;\;\;\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{\sqrt{1 - \cos x}}{\sin x} \cdot \sqrt{1 - \cos x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0443112716287218308:\\
\;\;\;\;\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, \log \left({\left(e^{\cos x}\right)}^{\left(1 + \cos x\right)}\right)\right) \cdot \sin x}\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 0.002190375129707059:\\
\;\;\;\;\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{\sqrt{1 - \cos x}}{\sin x} \cdot \sqrt{1 - \cos x}\\

\end{array}
double f(double x) {
        double r65241 = 1.0;
        double r65242 = x;
        double r65243 = cos(r65242);
        double r65244 = r65241 - r65243;
        double r65245 = sin(r65242);
        double r65246 = r65244 / r65245;
        return r65246;
}

double f(double x) {
        double r65247 = 1.0;
        double r65248 = x;
        double r65249 = cos(r65248);
        double r65250 = r65247 - r65249;
        double r65251 = sin(r65248);
        double r65252 = r65250 / r65251;
        double r65253 = -0.04431127162872183;
        bool r65254 = r65252 <= r65253;
        double r65255 = r65249 + r65247;
        double r65256 = r65249 * r65255;
        double r65257 = fma(r65247, r65247, r65256);
        double r65258 = r65257 * r65250;
        double r65259 = exp(r65249);
        double r65260 = r65247 + r65249;
        double r65261 = pow(r65259, r65260);
        double r65262 = log(r65261);
        double r65263 = fma(r65247, r65247, r65262);
        double r65264 = r65263 * r65251;
        double r65265 = r65258 / r65264;
        double r65266 = 0.002190375129707059;
        bool r65267 = r65252 <= r65266;
        double r65268 = 0.041666666666666664;
        double r65269 = 3.0;
        double r65270 = pow(r65248, r65269);
        double r65271 = 0.004166666666666667;
        double r65272 = 5.0;
        double r65273 = pow(r65248, r65272);
        double r65274 = 0.5;
        double r65275 = r65274 * r65248;
        double r65276 = fma(r65271, r65273, r65275);
        double r65277 = fma(r65268, r65270, r65276);
        double r65278 = sqrt(r65250);
        double r65279 = r65278 / r65251;
        double r65280 = r65279 * r65278;
        double r65281 = r65267 ? r65277 : r65280;
        double r65282 = r65254 ? r65265 : r65281;
        return r65282;
}

Error

Bits error versus x

Target

Original29.7
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.04431127162872183

    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}\]
    9. Using strategy rm
    10. Applied add-log-exp0.9

      \[\leadsto \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, \color{blue}{\log \left(e^{\mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)}\right)}\right) \cdot \sin x}\]
    11. Simplified0.9

      \[\leadsto \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, \log \color{blue}{\left({\left(e^{\cos x}\right)}^{\left(1 + \cos x\right)}\right)}\right) \cdot \sin x}\]

    if -0.04431127162872183 < (/ (- 1.0 (cos x)) (sin x)) < 0.002190375129707059

    1. Initial program 59.1

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

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

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

    1. Initial program 0.9

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

      \[\leadsto \color{blue}{\log \left(e^{\frac{1 - \cos x}{\sin x}}\right)}\]
    4. Using strategy rm
    5. Applied *-un-lft-identity1.0

      \[\leadsto \log \left(e^{\frac{1 - \cos x}{\color{blue}{1 \cdot \sin x}}}\right)\]
    6. Applied add-sqr-sqrt1.2

      \[\leadsto \log \left(e^{\frac{\color{blue}{\sqrt{1 - \cos x} \cdot \sqrt{1 - \cos x}}}{1 \cdot \sin x}}\right)\]
    7. Applied times-frac1.2

      \[\leadsto \log \left(e^{\color{blue}{\frac{\sqrt{1 - \cos x}}{1} \cdot \frac{\sqrt{1 - \cos x}}{\sin x}}}\right)\]
    8. Applied exp-prod1.2

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

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

      \[\leadsto \frac{\sqrt{1 - \cos x}}{\sin x} \cdot \color{blue}{\sqrt{1 - \cos 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.0443112716287218308:\\ \;\;\;\;\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, \log \left({\left(e^{\cos x}\right)}^{\left(1 + \cos x\right)}\right)\right) \cdot \sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 0.002190375129707059:\\ \;\;\;\;\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{\sqrt{1 - \cos x}}{\sin x} \cdot \sqrt{1 - \cos x}\\ \end{array}\]

Reproduce

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