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

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

\mathbf{else}:\\
\;\;\;\;\frac{1}{x} \cdot \frac{1}{\frac{x}{1 - \cos x}}\\

\end{array}
double code(double x) {
	return ((1.0 - cos(x)) / (x * x));
}
double code(double x) {
	double temp;
	if ((x <= -0.03128865823944801)) {
		temp = ((1.0 / x) * ((1.0 - cos(x)) / x));
	} else {
		double temp_1;
		if ((x <= 0.024673669128913823)) {
			temp_1 = (((0.001388888888888889 * pow(x, 4.0)) + 0.5) - (0.041666666666666664 * pow(x, 2.0)));
		} else {
			temp_1 = ((1.0 / x) * (1.0 / (x / (1.0 - cos(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

Derivation

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

    1. Initial program 0.8

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

      \[\leadsto \frac{\color{blue}{1 \cdot \left(1 - \cos x\right)}}{x \cdot x}\]
    4. Applied times-frac0.5

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

    if -0.03128865823944801 < x < 0.024673669128913823

    1. Initial program 62.3

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

    1. Initial program 1.1

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

      \[\leadsto \frac{\color{blue}{1 \cdot \left(1 - \cos x\right)}}{x \cdot x}\]
    4. Applied times-frac0.6

      \[\leadsto \color{blue}{\frac{1}{x} \cdot \frac{1 - \cos x}{x}}\]
    5. Using strategy rm
    6. Applied clear-num0.6

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

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

Reproduce

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