Average Error: 30.7 → 0.3
Time: 3.6s
Precision: 64
\[\frac{1 - \cos x}{x \cdot x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.0296756945743743734:\\ \;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\ \mathbf{elif}\;x \le 0.03505810655614032:\\ \;\;\;\;\left(\frac{1}{720} \cdot {x}^{4} + \frac{1}{2}\right) - \frac{1}{24} \cdot {x}^{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\log \left(e^{1 - \cos x}\right)}{x}}{x}\\ \end{array}\]
\frac{1 - \cos x}{x \cdot x}
\begin{array}{l}
\mathbf{if}\;x \le -0.0296756945743743734:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\

\mathbf{elif}\;x \le 0.03505810655614032:\\
\;\;\;\;\left(\frac{1}{720} \cdot {x}^{4} + \frac{1}{2}\right) - \frac{1}{24} \cdot {x}^{2}\\

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

\end{array}
double code(double x) {
	return ((1.0 - cos(x)) / (x * x));
}
double code(double x) {
	double VAR;
	if ((x <= -0.029675694574374373)) {
		VAR = (((1.0 - cos(x)) / x) / x);
	} else {
		double VAR_1;
		if ((x <= 0.03505810655614032)) {
			VAR_1 = (((0.001388888888888889 * pow(x, 4.0)) + 0.5) - (0.041666666666666664 * pow(x, 2.0)));
		} else {
			VAR_1 = ((log(exp((1.0 - cos(x)))) / x) / 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

Derivation

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

    1. Initial program 1.1

      \[\frac{1 - \cos x}{x \cdot x}\]
    2. Using strategy rm
    3. Applied associate-/r*0.5

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

    if -0.029675694574374373 < x < 0.03505810655614032

    1. Initial program 62.2

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

      \[\leadsto \color{blue}{\left(\frac{1}{720} \cdot {x}^{4} + \frac{1}{2}\right) - \frac{1}{24} \cdot {x}^{2}}\]

    if 0.03505810655614032 < x

    1. Initial program 1.1

      \[\frac{1 - \cos x}{x \cdot x}\]
    2. Using strategy rm
    3. Applied associate-/r*0.4

      \[\leadsto \color{blue}{\frac{\frac{1 - \cos x}{x}}{x}}\]
    4. Using strategy rm
    5. Applied add-log-exp0.6

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

      \[\leadsto \frac{\frac{\color{blue}{\log \left(e^{1}\right)} - \log \left(e^{\cos x}\right)}{x}}{x}\]
    7. Applied diff-log0.6

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.0296756945743743734:\\ \;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\ \mathbf{elif}\;x \le 0.03505810655614032:\\ \;\;\;\;\left(\frac{1}{720} \cdot {x}^{4} + \frac{1}{2}\right) - \frac{1}{24} \cdot {x}^{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\log \left(e^{1 - \cos x}\right)}{x}}{x}\\ \end{array}\]

Reproduce

herbie shell --seed 2020102 
(FPCore (x)
  :name "cos2 (problem 3.4.1)"
  :precision binary64
  (/ (- 1 (cos x)) (* x x)))