Average Error: 30.3 → 1.0
Time: 20.5s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0391915361306189591328319465901586227119:\\ \;\;\;\;\frac{1}{1 + \cos x} \cdot \left(\left(\left(1 + \cos x\right) \cdot \left(1 - \cos x\right)\right) \cdot \frac{1}{\sin x}\right)\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 5.238158504786694352056697354669267951977 \cdot 10^{-4}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \mathsf{fma}\left({x}^{3}, \frac{1}{24}, x \cdot \frac{1}{2}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left({\left(e^{\frac{1}{\sin x}}\right)}^{\left(1 - \cos x\right)}\right)\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0391915361306189591328319465901586227119:\\
\;\;\;\;\frac{1}{1 + \cos x} \cdot \left(\left(\left(1 + \cos x\right) \cdot \left(1 - \cos x\right)\right) \cdot \frac{1}{\sin x}\right)\\

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

\mathbf{else}:\\
\;\;\;\;\log \left({\left(e^{\frac{1}{\sin x}}\right)}^{\left(1 - \cos x\right)}\right)\\

\end{array}
double f(double x) {
        double r70430 = 1.0;
        double r70431 = x;
        double r70432 = cos(r70431);
        double r70433 = r70430 - r70432;
        double r70434 = sin(r70431);
        double r70435 = r70433 / r70434;
        return r70435;
}

double f(double x) {
        double r70436 = 1.0;
        double r70437 = x;
        double r70438 = cos(r70437);
        double r70439 = r70436 - r70438;
        double r70440 = sin(r70437);
        double r70441 = r70439 / r70440;
        double r70442 = -0.03919153613061896;
        bool r70443 = r70441 <= r70442;
        double r70444 = 1.0;
        double r70445 = r70436 + r70438;
        double r70446 = r70444 / r70445;
        double r70447 = r70445 * r70439;
        double r70448 = r70444 / r70440;
        double r70449 = r70447 * r70448;
        double r70450 = r70446 * r70449;
        double r70451 = 0.0005238158504786694;
        bool r70452 = r70441 <= r70451;
        double r70453 = 0.004166666666666667;
        double r70454 = 5.0;
        double r70455 = pow(r70437, r70454);
        double r70456 = 3.0;
        double r70457 = pow(r70437, r70456);
        double r70458 = 0.041666666666666664;
        double r70459 = 0.5;
        double r70460 = r70437 * r70459;
        double r70461 = fma(r70457, r70458, r70460);
        double r70462 = fma(r70453, r70455, r70461);
        double r70463 = exp(r70448);
        double r70464 = pow(r70463, r70439);
        double r70465 = log(r70464);
        double r70466 = r70452 ? r70462 : r70465;
        double r70467 = r70443 ? r70450 : r70466;
        return r70467;
}

Error

Bits error versus x

Target

Original30.3
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.03919153613061896

    1. Initial program 0.7

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied clear-num0.8

      \[\leadsto \color{blue}{\frac{1}{\frac{\sin x}{1 - \cos x}}}\]
    4. Using strategy rm
    5. Applied flip--1.2

      \[\leadsto \frac{1}{\frac{\sin x}{\color{blue}{\frac{1 \cdot 1 - \cos x \cdot \cos x}{1 + \cos x}}}}\]
    6. Applied associate-/r/1.3

      \[\leadsto \frac{1}{\color{blue}{\frac{\sin x}{1 \cdot 1 - \cos x \cdot \cos x} \cdot \left(1 + \cos x\right)}}\]
    7. Applied add-cube-cbrt1.3

      \[\leadsto \frac{\color{blue}{\left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right) \cdot \sqrt[3]{1}}}{\frac{\sin x}{1 \cdot 1 - \cos x \cdot \cos x} \cdot \left(1 + \cos x\right)}\]
    8. Applied times-frac1.3

      \[\leadsto \color{blue}{\frac{\sqrt[3]{1} \cdot \sqrt[3]{1}}{\frac{\sin x}{1 \cdot 1 - \cos x \cdot \cos x}} \cdot \frac{\sqrt[3]{1}}{1 + \cos x}}\]
    9. Simplified0.9

      \[\leadsto \color{blue}{\left(\frac{1}{\sin x} \cdot \left(\left(1 + \cos x\right) \cdot \left(1 - \cos x\right)\right)\right)} \cdot \frac{\sqrt[3]{1}}{1 + \cos x}\]
    10. Simplified0.9

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

    if -0.03919153613061896 < (/ (- 1.0 (cos x)) (sin x)) < 0.0005238158504786694

    1. Initial program 59.2

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

      \[\leadsto \color{blue}{\frac{1}{\frac{\sin x}{1 - \cos x}}}\]
    4. Taylor expanded around 0 0.9

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \mathsf{fma}\left({x}^{3}, \frac{1}{24}, x \cdot \frac{1}{2}\right)\right)}\]

    if 0.0005238158504786694 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied clear-num1.0

      \[\leadsto \color{blue}{\frac{1}{\frac{\sin x}{1 - \cos x}}}\]
    4. Using strategy rm
    5. Applied add-log-exp1.0

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

      \[\leadsto \log \color{blue}{\left({\left(e^{\frac{1}{\sin x}}\right)}^{\left(1 - \cos x\right)}\right)}\]
  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.0391915361306189591328319465901586227119:\\ \;\;\;\;\frac{1}{1 + \cos x} \cdot \left(\left(\left(1 + \cos x\right) \cdot \left(1 - \cos x\right)\right) \cdot \frac{1}{\sin x}\right)\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 5.238158504786694352056697354669267951977 \cdot 10^{-4}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \mathsf{fma}\left({x}^{3}, \frac{1}{24}, x \cdot \frac{1}{2}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left({\left(e^{\frac{1}{\sin x}}\right)}^{\left(1 - \cos x\right)}\right)\\ \end{array}\]

Reproduce

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