Average Error: 29.7 → 1.0
Time: 7.6s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0443112716287218308:\\ \;\;\;\;\frac{\mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x + 1\right)\right) \cdot \left(1 - \cos x\right)}{\mathsf{fma}\left(1, 1, \log \left({\left(e^{\cos x}\right)}^{\left(1 + \cos x\right)}\right)\right) \cdot \sin x}\\ \mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 0.002190375129707059:\\ \;\;\;\;\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}:\\ \;\;\;\;\sqrt{1 - \cos x} \cdot \frac{\sqrt{1 - \cos x}}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{\sin x} \le -0.0443112716287218308:\\
\;\;\;\;\frac{\mathsf{fma}\left(1, 1, \cos x \cdot \left(\cos x + 1\right)\right) \cdot \left(1 - \cos x\right)}{\mathsf{fma}\left(1, 1, \log \left({\left(e^{\cos x}\right)}^{\left(1 + \cos x\right)}\right)\right) \cdot \sin x}\\

\mathbf{elif}\;\frac{1 - \cos x}{\sin x} \le 0.002190375129707059:\\
\;\;\;\;\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}:\\
\;\;\;\;\sqrt{1 - \cos x} \cdot \frac{\sqrt{1 - \cos x}}{\sin x}\\

\end{array}
double code(double x) {
	return ((1.0 - cos(x)) / sin(x));
}
double code(double x) {
	double temp;
	if ((((1.0 - cos(x)) / sin(x)) <= -0.04431127162872183)) {
		temp = ((fma(1.0, 1.0, (cos(x) * (cos(x) + 1.0))) * (1.0 - cos(x))) / (fma(1.0, 1.0, log(pow(exp(cos(x)), (1.0 + cos(x))))) * sin(x)));
	} else {
		double temp_1;
		if ((((1.0 - cos(x)) / sin(x)) <= 0.002190375129707059)) {
			temp_1 = fma(0.041666666666666664, pow(x, 3.0), fma(0.004166666666666667, pow(x, 5.0), (0.5 * x)));
		} else {
			temp_1 = (sqrt((1.0 - cos(x))) * (sqrt((1.0 - cos(x))) / sin(x)));
		}
		temp = temp_1;
	}
	return temp;
}

Error

Bits error versus x

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

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

Derivation

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

    1. Initial program 0.7

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

      \[\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. Applied associate-/l/0.8

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

      \[\leadsto \frac{{1}^{3} - {\left(\cos x\right)}^{3}}{\color{blue}{\mathsf{fma}\left(1, 1, \mathsf{fma}\left(\cos x, \cos x, 1 \cdot \cos x\right)\right) \cdot \sin x}}\]
    6. Using strategy rm
    7. Applied difference-cubes0.8

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

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

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

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

    if -0.04431127162872183 < (/ (- 1.0 (cos x)) (sin x)) < 0.002190375129707059

    1. Initial program 59.1

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

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

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

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied *-un-lft-identity0.9

      \[\leadsto \frac{1 - \cos x}{\color{blue}{1 \cdot \sin x}}\]
    4. Applied add-sqr-sqrt1.1

      \[\leadsto \frac{\color{blue}{\sqrt{1 - \cos x} \cdot \sqrt{1 - \cos x}}}{1 \cdot \sin x}\]
    5. Applied times-frac1.1

      \[\leadsto \color{blue}{\frac{\sqrt{1 - \cos x}}{1} \cdot \frac{\sqrt{1 - \cos x}}{\sin x}}\]
    6. Simplified1.1

      \[\leadsto \color{blue}{\sqrt{1 - \cos x}} \cdot \frac{\sqrt{1 - \cos x}}{\sin x}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification1.0

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

Reproduce

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