Average Error: 30.3 → 0.8
Time: 7.8s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.01816359460916088:\\ \;\;\;\;\frac{1}{\sin x} - \cos x \cdot \frac{1}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 6.0277248497108966 \cdot 10^{-4}:\\ \;\;\;\;\mathsf{fma}\left(0.041666666666666671, {x}^{3}, \mathsf{fma}\left(0.00416666666666666661, {x}^{5}, 0.5 \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\log \left(e^{1 - \cos x}\right)}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.01816359460916088:\\
\;\;\;\;\frac{1}{\sin x} - \cos x \cdot \frac{1}{\sin x}\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 6.0277248497108966 \cdot 10^{-4}:\\
\;\;\;\;\mathsf{fma}\left(0.041666666666666671, {x}^{3}, \mathsf{fma}\left(0.00416666666666666661, {x}^{5}, 0.5 \cdot x\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{\log \left(e^{1 - \cos x}\right)}{\sin x}\\

\end{array}
double f(double x) {
        double r62060 = 1.0;
        double r62061 = x;
        double r62062 = cos(r62061);
        double r62063 = r62060 - r62062;
        double r62064 = sin(r62061);
        double r62065 = r62063 / r62064;
        return r62065;
}

double f(double x) {
        double r62066 = 1.0;
        double r62067 = x;
        double r62068 = cos(r62067);
        double r62069 = r62066 - r62068;
        double r62070 = sin(r62067);
        double r62071 = r62069 / r62070;
        double r62072 = -0.01816359460916088;
        bool r62073 = r62071 <= r62072;
        double r62074 = r62066 / r62070;
        double r62075 = 1.0;
        double r62076 = r62075 / r62070;
        double r62077 = r62068 * r62076;
        double r62078 = r62074 - r62077;
        double r62079 = 0.0006027724849710897;
        bool r62080 = r62071 <= r62079;
        double r62081 = 0.04166666666666667;
        double r62082 = 3.0;
        double r62083 = pow(r62067, r62082);
        double r62084 = 0.004166666666666667;
        double r62085 = 5.0;
        double r62086 = pow(r62067, r62085);
        double r62087 = 0.5;
        double r62088 = r62087 * r62067;
        double r62089 = fma(r62084, r62086, r62088);
        double r62090 = fma(r62081, r62083, r62089);
        double r62091 = exp(r62069);
        double r62092 = log(r62091);
        double r62093 = r62092 / r62070;
        double r62094 = r62080 ? r62090 : r62093;
        double r62095 = r62073 ? r62078 : r62094;
        return r62095;
}

Error

Bits error versus x

Target

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

Derivation

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

    1. Initial program 0.8

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied div-sub1.0

      \[\leadsto \color{blue}{\frac{1}{\sin x} - \frac{\cos x}{\sin x}}\]
    4. Using strategy rm
    5. Applied div-inv0.9

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

    if -0.01816359460916088 < (/ (- 1.0 (cos x)) (sin x)) < 0.0006027724849710897

    1. Initial program 59.6

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied div-sub59.6

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

      \[\leadsto \color{blue}{0.041666666666666671 \cdot {x}^{3} + \left(0.00416666666666666661 \cdot {x}^{5} + 0.5 \cdot x\right)}\]
    5. Simplified0.5

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.041666666666666671, {x}^{3}, \mathsf{fma}\left(0.00416666666666666661, {x}^{5}, 0.5 \cdot x\right)\right)}\]

    if 0.0006027724849710897 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 1.0

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

      \[\leadsto \frac{1 - \color{blue}{\log \left(e^{\cos x}\right)}}{\sin x}\]
    4. Applied add-log-exp1.2

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

      \[\leadsto \frac{\color{blue}{\log \left(\frac{e^{1}}{e^{\cos x}}\right)}}{\sin x}\]
    6. Simplified1.2

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.01816359460916088:\\ \;\;\;\;\frac{1}{\sin x} - \cos x \cdot \frac{1}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 6.0277248497108966 \cdot 10^{-4}:\\ \;\;\;\;\mathsf{fma}\left(0.041666666666666671, {x}^{3}, \mathsf{fma}\left(0.00416666666666666661, {x}^{5}, 0.5 \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\log \left(e^{1 - \cos x}\right)}{\sin x}\\ \end{array}\]

Reproduce

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