Average Error: 31.7 → 0.3
Time: 9.8s
Precision: binary64
Cost: 13316
\[\frac{1 - \cos x}{x \cdot x} \]
\[\begin{array}{l} t_0 := 1 - \cos x\\ \mathbf{if}\;x \leq -0.11390511714187097:\\ \;\;\;\;{x}^{-2} \cdot t_0\\ \mathbf{elif}\;x \leq 6.856760985821968 \cdot 10^{-5}:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{x} \cdot \frac{1}{x}\\ \end{array} \]
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (* x x)))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (- 1.0 (cos x))))
   (if (<= x -0.11390511714187097)
     (* (pow x -2.0) t_0)
     (if (<= x 6.856760985821968e-5)
       (+ 0.5 (* -0.041666666666666664 (* x x)))
       (* (/ t_0 x) (/ 1.0 x))))))
double code(double x) {
	return (1.0 - cos(x)) / (x * x);
}
double code(double x) {
	double t_0 = 1.0 - cos(x);
	double tmp;
	if (x <= -0.11390511714187097) {
		tmp = pow(x, -2.0) * t_0;
	} else if (x <= 6.856760985821968e-5) {
		tmp = 0.5 + (-0.041666666666666664 * (x * x));
	} else {
		tmp = (t_0 / x) * (1.0 / x);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 - cos(x)) / (x * x)
end function
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: tmp
    t_0 = 1.0d0 - cos(x)
    if (x <= (-0.11390511714187097d0)) then
        tmp = (x ** (-2.0d0)) * t_0
    else if (x <= 6.856760985821968d-5) then
        tmp = 0.5d0 + ((-0.041666666666666664d0) * (x * x))
    else
        tmp = (t_0 / x) * (1.0d0 / x)
    end if
    code = tmp
end function
public static double code(double x) {
	return (1.0 - Math.cos(x)) / (x * x);
}
public static double code(double x) {
	double t_0 = 1.0 - Math.cos(x);
	double tmp;
	if (x <= -0.11390511714187097) {
		tmp = Math.pow(x, -2.0) * t_0;
	} else if (x <= 6.856760985821968e-5) {
		tmp = 0.5 + (-0.041666666666666664 * (x * x));
	} else {
		tmp = (t_0 / x) * (1.0 / x);
	}
	return tmp;
}
def code(x):
	return (1.0 - math.cos(x)) / (x * x)
def code(x):
	t_0 = 1.0 - math.cos(x)
	tmp = 0
	if x <= -0.11390511714187097:
		tmp = math.pow(x, -2.0) * t_0
	elif x <= 6.856760985821968e-5:
		tmp = 0.5 + (-0.041666666666666664 * (x * x))
	else:
		tmp = (t_0 / x) * (1.0 / x)
	return tmp
function code(x)
	return Float64(Float64(1.0 - cos(x)) / Float64(x * x))
end
function code(x)
	t_0 = Float64(1.0 - cos(x))
	tmp = 0.0
	if (x <= -0.11390511714187097)
		tmp = Float64((x ^ -2.0) * t_0);
	elseif (x <= 6.856760985821968e-5)
		tmp = Float64(0.5 + Float64(-0.041666666666666664 * Float64(x * x)));
	else
		tmp = Float64(Float64(t_0 / x) * Float64(1.0 / x));
	end
	return tmp
end
function tmp = code(x)
	tmp = (1.0 - cos(x)) / (x * x);
end
function tmp_2 = code(x)
	t_0 = 1.0 - cos(x);
	tmp = 0.0;
	if (x <= -0.11390511714187097)
		tmp = (x ^ -2.0) * t_0;
	elseif (x <= 6.856760985821968e-5)
		tmp = 0.5 + (-0.041666666666666664 * (x * x));
	else
		tmp = (t_0 / x) * (1.0 / x);
	end
	tmp_2 = tmp;
end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]
code[x_] := Block[{t$95$0 = N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -0.11390511714187097], N[(N[Power[x, -2.0], $MachinePrecision] * t$95$0), $MachinePrecision], If[LessEqual[x, 6.856760985821968e-5], N[(0.5 + N[(-0.041666666666666664 * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(t$95$0 / x), $MachinePrecision] * N[(1.0 / x), $MachinePrecision]), $MachinePrecision]]]]
\frac{1 - \cos x}{x \cdot x}
\begin{array}{l}
t_0 := 1 - \cos x\\
\mathbf{if}\;x \leq -0.11390511714187097:\\
\;\;\;\;{x}^{-2} \cdot t_0\\

\mathbf{elif}\;x \leq 6.856760985821968 \cdot 10^{-5}:\\
\;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{t_0}{x} \cdot \frac{1}{x}\\


\end{array}

Error

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.113905117141870973

    1. Initial program 1.1

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

      \[\leadsto \color{blue}{{x}^{-2} \cdot \left(1 - \cos x\right)} \]

    if -0.113905117141870973 < x < 6.8567609858219679e-5

    1. Initial program 62.4

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

      \[\leadsto \color{blue}{0.5 + -0.041666666666666664 \cdot {x}^{2}} \]
    3. Simplified0.0

      \[\leadsto \color{blue}{\mathsf{fma}\left(-0.041666666666666664, x \cdot x, 0.5\right)} \]
      Proof
      (fma.f64 -1/24 (*.f64 x x) 1/2): 0 points increase in error, 0 points decrease in error
      (fma.f64 -1/24 (Rewrite<= unpow2_binary64 (pow.f64 x 2)) 1/2): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 -1/24 (pow.f64 x 2)) 1/2)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= +-commutative_binary64 (+.f64 1/2 (*.f64 -1/24 (pow.f64 x 2)))): 0 points increase in error, 0 points decrease in error
    4. Applied egg-rr0.0

      \[\leadsto \color{blue}{-0.041666666666666664 \cdot \left(x \cdot x\right) + 0.5} \]

    if 6.8567609858219679e-5 < x

    1. Initial program 1.2

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -0.11390511714187097:\\ \;\;\;\;{x}^{-2} \cdot \left(1 - \cos x\right)\\ \mathbf{elif}\;x \leq 6.856760985821968 \cdot 10^{-5}:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1 - \cos x}{x} \cdot \frac{1}{x}\\ \end{array} \]

Alternatives

Alternative 1
Error0.4
Cost13376
\[\frac{\sin x}{\frac{x}{\frac{\tan \left(x \cdot 0.5\right)}{x}}} \]
Alternative 2
Error0.3
Cost7240
\[\begin{array}{l} t_0 := \left(\cos x + -1\right) \cdot \frac{\frac{-1}{x}}{x}\\ \mathbf{if}\;x \leq -0.11390511714187097:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 6.856760985821968 \cdot 10^{-5}:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error0.3
Cost7240
\[\begin{array}{l} \mathbf{if}\;x \leq -0.11390511714187097:\\ \;\;\;\;\left(\cos x + -1\right) \cdot \frac{\frac{-1}{x}}{x}\\ \mathbf{elif}\;x \leq 6.856760985821968 \cdot 10^{-5}:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1 - \cos x}{x} \cdot \frac{1}{x}\\ \end{array} \]
Alternative 4
Error0.6
Cost7112
\[\begin{array}{l} t_0 := \frac{1 - \cos x}{x \cdot x}\\ \mathbf{if}\;x \leq -0.11390511714187097:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 6.856760985821968 \cdot 10^{-5}:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error15.4
Cost328
\[\begin{array}{l} \mathbf{if}\;x \leq -2.361297473996937 \cdot 10^{+77}:\\ \;\;\;\;0\\ \mathbf{elif}\;x \leq 9.161298344969711 \cdot 10^{+67}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
Alternative 6
Error46.5
Cost64
\[0 \]

Error

Reproduce

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