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

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

\mathbf{else}:\\
\;\;\;\;\frac{\frac{e^{\log \left({1}^{3} - \log \left(e^{{\left(\cos x\right)}^{3}}\right)\right)}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\

\end{array}
double f(double x) {
        double r81019 = 1.0;
        double r81020 = x;
        double r81021 = cos(r81020);
        double r81022 = r81019 - r81021;
        double r81023 = sin(r81020);
        double r81024 = r81022 / r81023;
        return r81024;
}

double f(double x) {
        double r81025 = 1.0;
        double r81026 = x;
        double r81027 = cos(r81026);
        double r81028 = r81025 - r81027;
        double r81029 = sin(r81026);
        double r81030 = r81028 / r81029;
        double r81031 = -0.014198217077059884;
        bool r81032 = r81030 <= r81031;
        double r81033 = 3.0;
        double r81034 = pow(r81025, r81033);
        double r81035 = pow(r81027, r81033);
        double r81036 = r81034 - r81035;
        double r81037 = 1.0;
        double r81038 = r81025 + r81027;
        double r81039 = r81025 * r81025;
        double r81040 = fma(r81027, r81038, r81039);
        double r81041 = r81037 / r81040;
        double r81042 = r81036 * r81041;
        double r81043 = r81042 / r81029;
        double r81044 = 2.589932435073377e-05;
        bool r81045 = r81030 <= r81044;
        double r81046 = 0.041666666666666664;
        double r81047 = pow(r81026, r81033);
        double r81048 = 5.0;
        double r81049 = pow(r81026, r81048);
        double r81050 = 0.004166666666666667;
        double r81051 = 0.5;
        double r81052 = r81051 * r81026;
        double r81053 = fma(r81049, r81050, r81052);
        double r81054 = fma(r81046, r81047, r81053);
        double r81055 = exp(r81035);
        double r81056 = log(r81055);
        double r81057 = r81034 - r81056;
        double r81058 = log(r81057);
        double r81059 = exp(r81058);
        double r81060 = r81059 / r81040;
        double r81061 = r81060 / r81029;
        double r81062 = r81045 ? r81054 : r81061;
        double r81063 = r81032 ? r81043 : r81062;
        return r81063;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.8

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

      \[\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. Simplified0.9

      \[\leadsto \frac{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\color{blue}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right)}}}{\sin x}\]
    5. Using strategy rm
    6. Applied div-inv1.0

      \[\leadsto \frac{\color{blue}{\left({1}^{3} - {\left(\cos x\right)}^{3}\right) \cdot \frac{1}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right)}}}{\sin x}\]
    7. Simplified1.0

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

    if -0.014198217077059884 < (/ (- 1.0 (cos x)) (sin x)) < 2.589932435073377e-05

    1. Initial program 59.8

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

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

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

    if 2.589932435073377e-05 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 1.1

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

      \[\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. Simplified1.2

      \[\leadsto \frac{\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\color{blue}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right)}}}{\sin x}\]
    5. Using strategy rm
    6. Applied add-exp-log1.2

      \[\leadsto \frac{\frac{\color{blue}{e^{\log \left({1}^{3} - {\left(\cos x\right)}^{3}\right)}}}{\mathsf{fma}\left(\cos x, \cos x + 1, 1 \cdot 1\right)}}{\sin x}\]
    7. Using strategy rm
    8. Applied add-log-exp1.3

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.01419821707705988360348481336359327542596:\\ \;\;\;\;\frac{\left({1}^{3} - {\left(\cos x\right)}^{3}\right) \cdot \frac{1}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 2.589932435073376851492402139776061176235 \cdot 10^{-5}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{24}, {x}^{3}, \mathsf{fma}\left({x}^{5}, \frac{1}{240}, \frac{1}{2} \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{e^{\log \left({1}^{3} - \log \left(e^{{\left(\cos x\right)}^{3}}\right)\right)}}{\mathsf{fma}\left(\cos x, 1 + \cos x, 1 \cdot 1\right)}}{\sin x}\\ \end{array}\]

Reproduce

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