Average Error: 30.5 → 0.6
Time: 8.2s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.022980230603723749:\\ \;\;\;\;\frac{e^{\log \left({1}^{3} - {\left(\cos x\right)}^{3}\right) - \log \left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right)}}{\sin x}\\ \mathbf{elif}\;x \le 0.018602392475598327:\\ \;\;\;\;\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}\;x \le -0.022980230603723749:\\
\;\;\;\;\frac{e^{\log \left({1}^{3} - {\left(\cos x\right)}^{3}\right) - \log \left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right)}}{\sin x}\\

\mathbf{elif}\;x \le 0.018602392475598327:\\
\;\;\;\;\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 ((x <= -0.02298023060372375)) {
		temp = (exp((log((pow(1.0, 3.0) - pow(cos(x), 3.0))) - log(((1.0 * 1.0) + ((cos(x) * cos(x)) + (1.0 * cos(x))))))) / sin(x));
	} else {
		double temp_1;
		if ((x <= 0.018602392475598327)) {
			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

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

Derivation

  1. Split input into 3 regimes
  2. if x < -0.02298023060372375

    1. Initial program 1.0

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied add-exp-log1.0

      \[\leadsto \frac{\color{blue}{e^{\log \left(1 - \cos x\right)}}}{\sin x}\]
    4. Using strategy rm
    5. Applied flip3--1.1

      \[\leadsto \frac{e^{\log \color{blue}{\left(\frac{{1}^{3} - {\left(\cos x\right)}^{3}}{1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)}\right)}}}{\sin x}\]
    6. Applied log-div1.1

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

    if -0.02298023060372375 < x < 0.018602392475598327

    1. Initial program 59.9

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

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

      \[\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.018602392475598327 < x

    1. Initial program 0.9

      \[\frac{1 - \cos x}{\sin x}\]
    2. Using strategy rm
    3. Applied add-exp-log0.9

      \[\leadsto \frac{\color{blue}{e^{\log \left(1 - \cos x\right)}}}{\sin x}\]
    4. Using strategy rm
    5. Applied pow10.9

      \[\leadsto \frac{e^{\log \color{blue}{\left({\left(1 - \cos x\right)}^{1}\right)}}}{\sin x}\]
    6. Applied log-pow0.9

      \[\leadsto \frac{e^{\color{blue}{1 \cdot \log \left(1 - \cos x\right)}}}{\sin x}\]
    7. Applied exp-prod1.0

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

      \[\leadsto \frac{{\color{blue}{e}}^{\left(\log \left(1 - \cos x\right)\right)}}{\sin x}\]
    9. Using strategy rm
    10. Applied *-un-lft-identity1.0

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

      \[\leadsto \frac{\color{blue}{\sqrt{{e}^{\left(\log \left(1 - \cos x\right)\right)}} \cdot \sqrt{{e}^{\left(\log \left(1 - \cos x\right)\right)}}}}{1 \cdot \sin x}\]
    12. Applied times-frac1.1

      \[\leadsto \color{blue}{\frac{\sqrt{{e}^{\left(\log \left(1 - \cos x\right)\right)}}}{1} \cdot \frac{\sqrt{{e}^{\left(\log \left(1 - \cos x\right)\right)}}}{\sin x}}\]
    13. Simplified1.1

      \[\leadsto \color{blue}{\sqrt{1 - \cos x}} \cdot \frac{\sqrt{{e}^{\left(\log \left(1 - \cos x\right)\right)}}}{\sin x}\]
    14. Simplified1.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.022980230603723749:\\ \;\;\;\;\frac{e^{\log \left({1}^{3} - {\left(\cos x\right)}^{3}\right) - \log \left(1 \cdot 1 + \left(\cos x \cdot \cos x + 1 \cdot \cos x\right)\right)}}{\sin x}\\ \mathbf{elif}\;x \le 0.018602392475598327:\\ \;\;\;\;\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 2020058 +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)))