Average Error: 30.0 → 0.6
Time: 24.7s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.0218674568504588634487717513366078492254:\\ \;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \frac{\mathsf{fma}\left(\frac{1}{8}, \left(\cos \left(x + x\right) \cdot \cos \left(x + x\right)\right) \cdot \cos \left(x + x\right), \frac{1}{8}\right)}{\mathsf{fma}\left(\frac{1}{4} \cdot \cos \left(x + x\right), \cos \left(x + x\right), \frac{1}{4}\right) - \frac{1}{4} \cdot \cos \left(x + x\right)} \cdot \cos x}{\mathsf{fma}\left(1 + \cos x, \cos x, 1 \cdot 1\right)}}{\sin x}\\ \mathbf{elif}\;x \le 0.02069943920891310559784237455005495576188:\\ \;\;\;\;\mathsf{fma}\left(x, \mathsf{fma}\left(x, \frac{1}{24} \cdot x, \frac{1}{2}\right), \frac{1}{240} \cdot {x}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right)}{\mathsf{fma}\left(1 + \cos x, \cos x, 1 \cdot 1\right)}}{\sin x} - \frac{\frac{\frac{\mathsf{fma}\left(\cos \left(x + x\right), \cos x, \cos x\right)}{2}}{\mathsf{fma}\left(1 + \cos x, \cos x, 1 \cdot 1\right)}}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.0218674568504588634487717513366078492254:\\
\;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \frac{\mathsf{fma}\left(\frac{1}{8}, \left(\cos \left(x + x\right) \cdot \cos \left(x + x\right)\right) \cdot \cos \left(x + x\right), \frac{1}{8}\right)}{\mathsf{fma}\left(\frac{1}{4} \cdot \cos \left(x + x\right), \cos \left(x + x\right), \frac{1}{4}\right) - \frac{1}{4} \cdot \cos \left(x + x\right)} \cdot \cos x}{\mathsf{fma}\left(1 + \cos x, \cos x, 1 \cdot 1\right)}}{\sin x}\\

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

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

\end{array}
double f(double x) {
        double r4386156 = 1.0;
        double r4386157 = x;
        double r4386158 = cos(r4386157);
        double r4386159 = r4386156 - r4386158;
        double r4386160 = sin(r4386157);
        double r4386161 = r4386159 / r4386160;
        return r4386161;
}

double f(double x) {
        double r4386162 = x;
        double r4386163 = -0.021867456850458863;
        bool r4386164 = r4386162 <= r4386163;
        double r4386165 = 1.0;
        double r4386166 = r4386165 * r4386165;
        double r4386167 = r4386165 * r4386166;
        double r4386168 = 0.125;
        double r4386169 = r4386162 + r4386162;
        double r4386170 = cos(r4386169);
        double r4386171 = r4386170 * r4386170;
        double r4386172 = r4386171 * r4386170;
        double r4386173 = fma(r4386168, r4386172, r4386168);
        double r4386174 = 0.25;
        double r4386175 = r4386174 * r4386170;
        double r4386176 = fma(r4386175, r4386170, r4386174);
        double r4386177 = r4386176 - r4386175;
        double r4386178 = r4386173 / r4386177;
        double r4386179 = cos(r4386162);
        double r4386180 = r4386178 * r4386179;
        double r4386181 = r4386167 - r4386180;
        double r4386182 = r4386165 + r4386179;
        double r4386183 = fma(r4386182, r4386179, r4386166);
        double r4386184 = r4386181 / r4386183;
        double r4386185 = sin(r4386162);
        double r4386186 = r4386184 / r4386185;
        double r4386187 = 0.020699439208913106;
        bool r4386188 = r4386162 <= r4386187;
        double r4386189 = 0.041666666666666664;
        double r4386190 = r4386189 * r4386162;
        double r4386191 = 0.5;
        double r4386192 = fma(r4386162, r4386190, r4386191);
        double r4386193 = 0.004166666666666667;
        double r4386194 = 5.0;
        double r4386195 = pow(r4386162, r4386194);
        double r4386196 = r4386193 * r4386195;
        double r4386197 = fma(r4386162, r4386192, r4386196);
        double r4386198 = r4386167 / r4386183;
        double r4386199 = r4386198 / r4386185;
        double r4386200 = fma(r4386170, r4386179, r4386179);
        double r4386201 = 2.0;
        double r4386202 = r4386200 / r4386201;
        double r4386203 = r4386202 / r4386183;
        double r4386204 = r4386203 / r4386185;
        double r4386205 = r4386199 - r4386204;
        double r4386206 = r4386188 ? r4386197 : r4386205;
        double r4386207 = r4386164 ? r4386186 : r4386206;
        return r4386207;
}

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 x < -0.021867456850458863

    1. Initial program 0.9

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

      \[\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.0

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

      \[\leadsto \frac{\frac{\left(1 \cdot 1\right) \cdot 1 - \left(\cos x \cdot \cos x\right) \cdot \cos x}{\color{blue}{\mathsf{fma}\left(\cos x + 1, \cos x, 1 \cdot 1\right)}}}{\sin x}\]
    6. Using strategy rm
    7. Applied sqr-cos1.0

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

      \[\leadsto \frac{\frac{\left(1 \cdot 1\right) \cdot 1 - \left(\frac{1}{2} + \color{blue}{\frac{1}{2} \cdot \cos \left(x + x\right)}\right) \cdot \cos x}{\mathsf{fma}\left(\cos x + 1, \cos x, 1 \cdot 1\right)}}{\sin x}\]
    9. Using strategy rm
    10. Applied flip3-+1.0

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

      \[\leadsto \frac{\frac{\left(1 \cdot 1\right) \cdot 1 - \frac{\color{blue}{\mathsf{fma}\left(\frac{1}{8}, \left(\cos \left(x + x\right) \cdot \cos \left(x + x\right)\right) \cdot \cos \left(x + x\right), \frac{1}{8}\right)}}{\frac{1}{2} \cdot \frac{1}{2} + \left(\left(\frac{1}{2} \cdot \cos \left(x + x\right)\right) \cdot \left(\frac{1}{2} \cdot \cos \left(x + x\right)\right) - \frac{1}{2} \cdot \left(\frac{1}{2} \cdot \cos \left(x + x\right)\right)\right)} \cdot \cos x}{\mathsf{fma}\left(\cos x + 1, \cos x, 1 \cdot 1\right)}}{\sin x}\]
    12. Simplified1.1

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

    if -0.021867456850458863 < x < 0.020699439208913106

    1. Initial program 59.9

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

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

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

    if 0.020699439208913106 < x

    1. Initial program 0.9

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

      \[\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.0

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

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

      \[\leadsto \frac{\frac{\left(1 \cdot 1\right) \cdot 1 - \color{blue}{\frac{\cos \left(x + x\right) + \cos \left(x - x\right)}{2}} \cdot \cos x}{\mathsf{fma}\left(\cos x + 1, \cos x, 1 \cdot 1\right)}}{\sin x}\]
    8. Applied associate-*l/1.0

      \[\leadsto \frac{\frac{\left(1 \cdot 1\right) \cdot 1 - \color{blue}{\frac{\left(\cos \left(x + x\right) + \cos \left(x - x\right)\right) \cdot \cos x}{2}}}{\mathsf{fma}\left(\cos x + 1, \cos x, 1 \cdot 1\right)}}{\sin x}\]
    9. Simplified0.9

      \[\leadsto \frac{\frac{\left(1 \cdot 1\right) \cdot 1 - \frac{\color{blue}{\mathsf{fma}\left(\cos \left(x + x\right), \cos x, \cos x\right)}}{2}}{\mathsf{fma}\left(\cos x + 1, \cos x, 1 \cdot 1\right)}}{\sin x}\]
    10. Using strategy rm
    11. Applied div-sub1.0

      \[\leadsto \frac{\color{blue}{\frac{\left(1 \cdot 1\right) \cdot 1}{\mathsf{fma}\left(\cos x + 1, \cos x, 1 \cdot 1\right)} - \frac{\frac{\mathsf{fma}\left(\cos \left(x + x\right), \cos x, \cos x\right)}{2}}{\mathsf{fma}\left(\cos x + 1, \cos x, 1 \cdot 1\right)}}}{\sin x}\]
    12. Applied div-sub1.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.0218674568504588634487717513366078492254:\\ \;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \frac{\mathsf{fma}\left(\frac{1}{8}, \left(\cos \left(x + x\right) \cdot \cos \left(x + x\right)\right) \cdot \cos \left(x + x\right), \frac{1}{8}\right)}{\mathsf{fma}\left(\frac{1}{4} \cdot \cos \left(x + x\right), \cos \left(x + x\right), \frac{1}{4}\right) - \frac{1}{4} \cdot \cos \left(x + x\right)} \cdot \cos x}{\mathsf{fma}\left(1 + \cos x, \cos x, 1 \cdot 1\right)}}{\sin x}\\ \mathbf{elif}\;x \le 0.02069943920891310559784237455005495576188:\\ \;\;\;\;\mathsf{fma}\left(x, \mathsf{fma}\left(x, \frac{1}{24} \cdot x, \frac{1}{2}\right), \frac{1}{240} \cdot {x}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right)}{\mathsf{fma}\left(1 + \cos x, \cos x, 1 \cdot 1\right)}}{\sin x} - \frac{\frac{\frac{\mathsf{fma}\left(\cos \left(x + x\right), \cos x, \cos x\right)}{2}}{\mathsf{fma}\left(1 + \cos x, \cos x, 1 \cdot 1\right)}}{\sin x}\\ \end{array}\]

Reproduce

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