Average Error: 30.0 → 0.6
Time: 25.3s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.002585231506460964:\\ \;\;\;\;\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 1.53832503201192376 \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}:\\ \;\;\;\;\frac{e^{\log \left({1}^{3} - {\left(\cos x\right)}^{3}\right)}}{\mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x + 1\right)\right) \cdot \sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.002585231506460964:\\
\;\;\;\;\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}\\

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

\end{array}
double f(double x) {
        double r75022 = 1.0;
        double r75023 = x;
        double r75024 = cos(r75023);
        double r75025 = r75022 - r75024;
        double r75026 = sin(r75023);
        double r75027 = r75025 / r75026;
        return r75027;
}

double f(double x) {
        double r75028 = 1.0;
        double r75029 = x;
        double r75030 = cos(r75029);
        double r75031 = r75028 - r75030;
        double r75032 = sin(r75029);
        double r75033 = r75031 / r75032;
        double r75034 = -0.0025852315064609638;
        bool r75035 = r75033 <= r75034;
        double r75036 = log(r75031);
        double r75037 = exp(r75036);
        double r75038 = r75037 / r75032;
        double r75039 = 1.5383250320119238e-08;
        bool r75040 = r75033 <= r75039;
        double r75041 = 0.041666666666666664;
        double r75042 = 3.0;
        double r75043 = pow(r75029, r75042);
        double r75044 = 0.004166666666666667;
        double r75045 = 5.0;
        double r75046 = pow(r75029, r75045);
        double r75047 = 0.5;
        double r75048 = r75047 * r75029;
        double r75049 = fma(r75044, r75046, r75048);
        double r75050 = fma(r75041, r75043, r75049);
        double r75051 = pow(r75028, r75042);
        double r75052 = pow(r75030, r75042);
        double r75053 = r75051 - r75052;
        double r75054 = log(r75053);
        double r75055 = exp(r75054);
        double r75056 = r75030 + r75028;
        double r75057 = r75030 * r75056;
        double r75058 = fma(r75028, r75028, r75057);
        double r75059 = r75058 * r75032;
        double r75060 = r75055 / r75059;
        double r75061 = r75040 ? r75050 : r75060;
        double r75062 = r75035 ? r75038 : r75061;
        return r75062;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.9

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

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

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

    if -0.0025852315064609638 < (/ (- 1.0 (cos x)) (sin x)) < 1.5383250320119238e-08

    1. Initial program 60.3

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

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

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

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

    1. Initial program 1.3

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied add-exp-log1.3

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

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

      \[\leadsto 1 \cdot \frac{e^{\log \color{blue}{\left(\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}\right)}}}{\sin x}\]
    8. Applied log-div1.5

      \[\leadsto 1 \cdot \frac{e^{\color{blue}{\log \left({1}^{3} - {\left(\cos x\right)}^{3}\right) - \log \left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right)}}}{\sin x}\]
    9. Applied exp-diff1.4

      \[\leadsto 1 \cdot \frac{\color{blue}{\frac{e^{\log \left({1}^{3} - {\left(\cos x\right)}^{3}\right)}}{e^{\log \left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right)}}}}{\sin x}\]
    10. Applied associate-/l/1.4

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

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

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

Reproduce

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