Average Error: 30.2 → 0.7
Time: 8.0s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0311367922052378544 \lor \neg \left(\frac{1 - \cos x}{\sin x} \le 0.0038090217065521576\right):\\ \;\;\;\;\frac{1 - \cos x}{\sin x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0311367922052378544 \lor \neg \left(\frac{1 - \cos x}{\sin x} \le 0.0038090217065521576\right):\\
\;\;\;\;\frac{1 - \cos x}{\sin x}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\

\end{array}
double f(double x) {
        double r66090 = 1.0;
        double r66091 = x;
        double r66092 = cos(r66091);
        double r66093 = r66090 - r66092;
        double r66094 = sin(r66091);
        double r66095 = r66093 / r66094;
        return r66095;
}

double f(double x) {
        double r66096 = 1.0;
        double r66097 = x;
        double r66098 = cos(r66097);
        double r66099 = r66096 - r66098;
        double r66100 = sin(r66097);
        double r66101 = r66099 / r66100;
        double r66102 = -0.031136792205237854;
        bool r66103 = r66101 <= r66102;
        double r66104 = 0.0038090217065521576;
        bool r66105 = r66101 <= r66104;
        double r66106 = !r66105;
        bool r66107 = r66103 || r66106;
        double r66108 = 0.041666666666666664;
        double r66109 = 3.0;
        double r66110 = pow(r66097, r66109);
        double r66111 = 0.004166666666666667;
        double r66112 = 5.0;
        double r66113 = pow(r66097, r66112);
        double r66114 = 0.5;
        double r66115 = r66114 * r66097;
        double r66116 = fma(r66111, r66113, r66115);
        double r66117 = fma(r66108, r66110, r66116);
        double r66118 = r66107 ? r66101 : r66117;
        return r66118;
}

Error

Bits error versus x

Target

Original30.2
Target0.0
Herbie0.7
\[\tan \left(\frac{x}{2}\right)\]

Derivation

  1. Split input into 2 regimes
  2. if (/ (- 1.0 (cos x)) (sin x)) < -0.031136792205237854 or 0.0038090217065521576 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 0.8

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

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

      \[\leadsto \log \left(e^{\frac{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 - \cos x\right)\right)}}{\sin x}}\right)\]
    6. Taylor expanded around inf 0.8

      \[\leadsto \color{blue}{\frac{1 - \cos x}{\sin x}}\]

    if -0.031136792205237854 < (/ (- 1.0 (cos x)) (sin x)) < 0.0038090217065521576

    1. Initial program 59.3

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

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

      \[\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)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0311367922052378544 \lor \neg \left(\frac{1 - \cos x}{\sin x} \le 0.0038090217065521576\right):\\ \;\;\;\;\frac{1 - \cos x}{\sin x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left(\frac{1}{240}, {x}^{5}, \frac{1}{2} \cdot x\right)\right)\\ \end{array}\]

Reproduce

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