Average Error: 30.0 → 0.5
Time: 9.2s
Precision: 64
\[\frac{1 - \cos x}{\sin x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.0182702360351804267:\\ \;\;\;\;\frac{e^{\log \left(\log \left(e^{{1}^{3} - {\left(\cos x\right)}^{3}}\right)\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.0213290789512861033:\\ \;\;\;\;\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}:\\ \;\;\;\;\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}\\ \end{array}\]
\frac{1 - \cos x}{\sin x}
\begin{array}{l}
\mathbf{if}\;x \le -0.0182702360351804267:\\
\;\;\;\;\frac{e^{\log \left(\log \left(e^{{1}^{3} - {\left(\cos x\right)}^{3}}\right)\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.0213290789512861033:\\
\;\;\;\;\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}:\\
\;\;\;\;\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}\\

\end{array}
double code(double x) {
	return ((1.0 - cos(x)) / sin(x));
}
double code(double x) {
	double VAR;
	if ((x <= -0.018270236035180427)) {
		VAR = (exp((log(log(exp((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 VAR_1;
		if ((x <= 0.021329078951286103)) {
			VAR_1 = fma(0.041666666666666664, pow(x, 3.0), fma(0.004166666666666667, pow(x, 5.0), (0.5 * x)));
		} else {
			VAR_1 = (exp(log((1.0 - cos(x)))) / sin(x));
		}
		VAR = VAR_1;
	}
	return VAR;
}

Error

Bits error versus x

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

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

Derivation

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

    1. Initial program 0.8

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

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

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

      \[\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}\]
    7. Using strategy rm
    8. Applied add-log-exp1.0

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

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

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

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

    if -0.018270236035180427 < x < 0.021329078951286103

    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.021329078951286103 < 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}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.0182702360351804267:\\ \;\;\;\;\frac{e^{\log \left(\log \left(e^{{1}^{3} - {\left(\cos x\right)}^{3}}\right)\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.0213290789512861033:\\ \;\;\;\;\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}:\\ \;\;\;\;\frac{e^{\log \left(1 - \cos x\right)}}{\sin x}\\ \end{array}\]

Reproduce

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