Average Error: 29.9 → 0.9
Time: 7.7s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0328316610415967866:\\ \;\;\;\;\log \left(\sqrt{e^{\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}}}\right) + \log \left(\sqrt{e^{\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}}}\right)\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 4.742529791284501 \cdot 10^{-8}:\\ \;\;\;\;\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}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}\right)\right)\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0328316610415967866:\\
\;\;\;\;\log \left(\sqrt{e^{\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}}}\right) + \log \left(\sqrt{e^{\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}}}\right)\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 4.742529791284501 \cdot 10^{-8}:\\
\;\;\;\;\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}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}\right)\right)\\

\end{array}
double f(double x) {
        double r56498 = 1.0;
        double r56499 = x;
        double r56500 = cos(r56499);
        double r56501 = r56498 - r56500;
        double r56502 = sin(r56499);
        double r56503 = r56501 / r56502;
        return r56503;
}

double f(double x) {
        double r56504 = 1.0;
        double r56505 = x;
        double r56506 = cos(r56505);
        double r56507 = r56504 - r56506;
        double r56508 = sin(r56505);
        double r56509 = r56507 / r56508;
        double r56510 = -0.03283166104159679;
        bool r56511 = r56509 <= r56510;
        double r56512 = log(r56507);
        double r56513 = exp(r56512);
        double r56514 = r56513 / r56508;
        double r56515 = exp(r56514);
        double r56516 = sqrt(r56515);
        double r56517 = log(r56516);
        double r56518 = r56517 + r56517;
        double r56519 = 4.7425297912845007e-08;
        bool r56520 = r56509 <= r56519;
        double r56521 = 0.041666666666666664;
        double r56522 = 3.0;
        double r56523 = pow(r56505, r56522);
        double r56524 = 0.004166666666666667;
        double r56525 = 5.0;
        double r56526 = pow(r56505, r56525);
        double r56527 = 0.5;
        double r56528 = r56527 * r56505;
        double r56529 = fma(r56524, r56526, r56528);
        double r56530 = fma(r56521, r56523, r56529);
        double r56531 = expm1(r56514);
        double r56532 = log1p(r56531);
        double r56533 = r56520 ? r56530 : r56532;
        double r56534 = r56511 ? r56518 : r56533;
        return r56534;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.8

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

      \[\leadsto \color{blue}{\log \left(e^{\frac{1 - \cos x}{\sin x}}\right)}\]
    4. Using strategy rm
    5. Applied add-exp-log0.8

      \[\leadsto \log \left(e^{\frac{\color{blue}{e^{\log \left(1 - \cos x\right)}}}{\sin x}}\right)\]
    6. Using strategy rm
    7. Applied add-sqr-sqrt1.0

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

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

    if -0.03283166104159679 < (/ (- 1.0 (cos x)) (sin x)) < 4.7425297912845007e-08

    1. Initial program 59.6

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

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

      \[\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 4.7425297912845007e-08 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 1.2

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

      \[\leadsto \color{blue}{\log \left(e^{\frac{1 - \cos x}{\sin x}}\right)}\]
    4. Using strategy rm
    5. Applied add-exp-log1.5

      \[\leadsto \log \left(e^{\frac{\color{blue}{e^{\log \left(1 - \cos x\right)}}}{\sin x}}\right)\]
    6. Using strategy rm
    7. Applied rem-log-exp1.2

      \[\leadsto \color{blue}{\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}}\]
    8. Using strategy rm
    9. Applied log1p-expm1-u1.4

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}\right)\right)}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0328316610415967866:\\ \;\;\;\;\log \left(\sqrt{e^{\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}}}\right) + \log \left(\sqrt{e^{\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}}}\right)\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 4.742529791284501 \cdot 10^{-8}:\\ \;\;\;\;\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}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}\right)\right)\\ \end{array}\]

Reproduce

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