Average Error: 29.9 → 0.6
Time: 1.4m
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.004972647923616153355086400011941805132665:\\ \;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \cos x \cdot \left(\cos x \cdot \cos x\right)}{\mathsf{fma}\left(\cos x, \frac{\cos x \cdot \cos x - 1 \cdot 1}{\cos x - 1}, 1 \cdot 1\right)}}{\sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 1.24535172833726550875588223732393089449 \cdot 10^{-5}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{240}, {x}^{5}, x \cdot \mathsf{fma}\left(x \cdot \frac{1}{24}, x, \frac{1}{2}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \cos x \cdot \left(\cos x \cdot \cos x\right)}{\mathsf{fma}\left(\cos x, \frac{\cos x \cdot \cos x - 1 \cdot 1}{\cos x - 1}, 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.004972647923616153355086400011941805132665:\\
\;\;\;\;\frac{\frac{1 \cdot \left(1 \cdot 1\right) - \cos x \cdot \left(\cos x \cdot \cos x\right)}{\mathsf{fma}\left(\cos x, \frac{\cos x \cdot \cos x - 1 \cdot 1}{\cos x - 1}, 1 \cdot 1\right)}}{\sin x}\\

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

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

\end{array}
double f(double x) {
        double r2217025 = 1.0;
        double r2217026 = x;
        double r2217027 = cos(r2217026);
        double r2217028 = r2217025 - r2217027;
        double r2217029 = sin(r2217026);
        double r2217030 = r2217028 / r2217029;
        return r2217030;
}

double f(double x) {
        double r2217031 = 1.0;
        double r2217032 = x;
        double r2217033 = cos(r2217032);
        double r2217034 = r2217031 - r2217033;
        double r2217035 = sin(r2217032);
        double r2217036 = r2217034 / r2217035;
        double r2217037 = -0.004972647923616153;
        bool r2217038 = r2217036 <= r2217037;
        double r2217039 = r2217031 * r2217031;
        double r2217040 = r2217031 * r2217039;
        double r2217041 = r2217033 * r2217033;
        double r2217042 = r2217033 * r2217041;
        double r2217043 = r2217040 - r2217042;
        double r2217044 = r2217041 - r2217039;
        double r2217045 = r2217033 - r2217031;
        double r2217046 = r2217044 / r2217045;
        double r2217047 = fma(r2217033, r2217046, r2217039);
        double r2217048 = r2217043 / r2217047;
        double r2217049 = r2217048 / r2217035;
        double r2217050 = 1.2453517283372655e-05;
        bool r2217051 = r2217036 <= r2217050;
        double r2217052 = 0.004166666666666667;
        double r2217053 = 5.0;
        double r2217054 = pow(r2217032, r2217053);
        double r2217055 = 0.041666666666666664;
        double r2217056 = r2217032 * r2217055;
        double r2217057 = 0.5;
        double r2217058 = fma(r2217056, r2217032, r2217057);
        double r2217059 = r2217032 * r2217058;
        double r2217060 = fma(r2217052, r2217054, r2217059);
        double r2217061 = r2217051 ? r2217060 : r2217049;
        double r2217062 = r2217038 ? r2217049 : r2217061;
        return r2217062;
}

Error

Bits error versus x

Target

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

Derivation

  1. Split input into 2 regimes
  2. if (/ (- 1.0 (cos x)) (sin x)) < -0.004972647923616153 or 1.2453517283372655e-05 < (/ (- 1.0 (cos x)) (sin x))

    1. Initial program 1.0

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

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

      \[\leadsto \frac{\frac{\color{blue}{1 \cdot \left(1 \cdot 1\right) - \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.1

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

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

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

    1. Initial program 60.0

      \[\frac{1 - \cos x}{\sin x}\]
    2. 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)}\]
    3. Simplified0.1

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{240}, {x}^{5}, x \cdot \mathsf{fma}\left(\frac{1}{24} \cdot x, x, \frac{1}{2}\right)\right)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.6

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

Reproduce

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