Average Error: 31.5 → 0.4
Time: 4.6s
Precision: binary64
\[\frac{1 - \cos x}{x \cdot x} \]
\[\begin{array}{l} \mathbf{if}\;x \leq -0.0025:\\ \;\;\;\;\tan \left(\frac{x}{2}\right) \cdot \frac{\sin x}{x \cdot x}\\ \mathbf{elif}\;x \leq 10^{-90}:\\ \;\;\;\;\mathsf{fma}\left(x, x \cdot -0.041666666666666664, \mathsf{fma}\left(0.001388888888888889, {x}^{4}, 0.5\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{{\sin x}^{2}}{x} \cdot \frac{\frac{1}{e^{\mathsf{log1p}\left(\cos x\right)}}}{x}\\ \end{array} \]
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (* x x)))
(FPCore (x)
 :precision binary64
 (if (<= x -0.0025)
   (* (tan (/ x 2.0)) (/ (sin x) (* x x)))
   (if (<= x 1e-90)
     (fma
      x
      (* x -0.041666666666666664)
      (fma 0.001388888888888889 (pow x 4.0) 0.5))
     (* (/ (pow (sin x) 2.0) x) (/ (/ 1.0 (exp (log1p (cos x)))) x)))))
double code(double x) {
	return (1.0 - cos(x)) / (x * x);
}
double code(double x) {
	double tmp;
	if (x <= -0.0025) {
		tmp = tan((x / 2.0)) * (sin(x) / (x * x));
	} else if (x <= 1e-90) {
		tmp = fma(x, (x * -0.041666666666666664), fma(0.001388888888888889, pow(x, 4.0), 0.5));
	} else {
		tmp = (pow(sin(x), 2.0) / x) * ((1.0 / exp(log1p(cos(x)))) / x);
	}
	return tmp;
}
function code(x)
	return Float64(Float64(1.0 - cos(x)) / Float64(x * x))
end
function code(x)
	tmp = 0.0
	if (x <= -0.0025)
		tmp = Float64(tan(Float64(x / 2.0)) * Float64(sin(x) / Float64(x * x)));
	elseif (x <= 1e-90)
		tmp = fma(x, Float64(x * -0.041666666666666664), fma(0.001388888888888889, (x ^ 4.0), 0.5));
	else
		tmp = Float64(Float64((sin(x) ^ 2.0) / x) * Float64(Float64(1.0 / exp(log1p(cos(x)))) / x));
	end
	return tmp
end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]
code[x_] := If[LessEqual[x, -0.0025], N[(N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision] * N[(N[Sin[x], $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 1e-90], N[(x * N[(x * -0.041666666666666664), $MachinePrecision] + N[(0.001388888888888889 * N[Power[x, 4.0], $MachinePrecision] + 0.5), $MachinePrecision]), $MachinePrecision], N[(N[(N[Power[N[Sin[x], $MachinePrecision], 2.0], $MachinePrecision] / x), $MachinePrecision] * N[(N[(1.0 / N[Exp[N[Log[1 + N[Cos[x], $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]]]
\frac{1 - \cos x}{x \cdot x}
\begin{array}{l}
\mathbf{if}\;x \leq -0.0025:\\
\;\;\;\;\tan \left(\frac{x}{2}\right) \cdot \frac{\sin x}{x \cdot x}\\

\mathbf{elif}\;x \leq 10^{-90}:\\
\;\;\;\;\mathsf{fma}\left(x, x \cdot -0.041666666666666664, \mathsf{fma}\left(0.001388888888888889, {x}^{4}, 0.5\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{{\sin x}^{2}}{x} \cdot \frac{\frac{1}{e^{\mathsf{log1p}\left(\cos x\right)}}}{x}\\


\end{array}

Error

Bits error versus x

Derivation

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

    1. Initial program 1.2

      \[\frac{1 - \cos x}{x \cdot x} \]
    2. Applied egg-rr1.2

      \[\leadsto \frac{\color{blue}{\frac{\sin x \cdot \sin x}{1 + \cos x}}}{x \cdot x} \]
    3. Taylor expanded in x around inf 1.2

      \[\leadsto \color{blue}{\frac{{\sin x}^{2}}{\left(1 + \cos x\right) \cdot {x}^{2}}} \]
    4. Applied egg-rr0.9

      \[\leadsto \color{blue}{\tan \left(\frac{x}{2}\right) \cdot \frac{\sin x}{x \cdot x}} \]

    if -0.00250000000000000005 < x < 9.99999999999999995e-91

    1. Initial program 62.9

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

      \[\leadsto \color{blue}{\left(0.001388888888888889 \cdot {x}^{4} + 0.5\right) - 0.041666666666666664 \cdot {x}^{2}} \]
    3. Simplified0.0

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, x \cdot -0.041666666666666664, \mathsf{fma}\left(0.001388888888888889, {x}^{4}, 0.5\right)\right)} \]

    if 9.99999999999999995e-91 < x

    1. Initial program 14.2

      \[\frac{1 - \cos x}{x \cdot x} \]
    2. Applied egg-rr0.9

      \[\leadsto \frac{\color{blue}{\frac{\sin x \cdot \sin x}{1 + \cos x}}}{x \cdot x} \]
    3. Applied egg-rr0.5

      \[\leadsto \color{blue}{\frac{{\sin x}^{2}}{x} \cdot \frac{\frac{1}{1 + \cos x}}{x}} \]
    4. Applied egg-rr0.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -0.0025:\\ \;\;\;\;\tan \left(\frac{x}{2}\right) \cdot \frac{\sin x}{x \cdot x}\\ \mathbf{elif}\;x \leq 10^{-90}:\\ \;\;\;\;\mathsf{fma}\left(x, x \cdot -0.041666666666666664, \mathsf{fma}\left(0.001388888888888889, {x}^{4}, 0.5\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{{\sin x}^{2}}{x} \cdot \frac{\frac{1}{e^{\mathsf{log1p}\left(\cos x\right)}}}{x}\\ \end{array} \]

Reproduce

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