Average Error: 31.4 → 0.3
Time: 27.9s
Precision: binary64
Cost: 13772
\[\frac{1 - \cos x}{x \cdot x} \]
\[\begin{array}{l} t_0 := 1 - \cos x\\ \mathbf{if}\;x \leq -2 \cdot 10^{-5}:\\ \;\;\;\;\frac{\begin{array}{l} \mathbf{if}\;2 \ne 0:\\ \;\;\;\;\sin x \cdot \tan \left(x \cdot 0.5\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array}}{x \cdot x}\\ \mathbf{elif}\;x \leq 4 \cdot 10^{-42}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\begin{array}{l} \mathbf{if}\;2 \ne 0:\\ \;\;\;\;\sin x \cdot \tan \left(\frac{x}{2}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array}}{x}}{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 -2e-5)
     (/ (if (!= 2.0 0.0) (* (sin x) (tan (* x 0.5))) t_0) (* x x))
     (if (<= x 4e-42)
       0.5
       (/ (/ (if (!= 2.0 0.0) (* (sin x) (tan (/ x 2.0))) t_0) x) 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_1;
	if (x <= -2e-5) {
		double tmp_2;
		if (2.0 != 0.0) {
			tmp_2 = sin(x) * tan((x * 0.5));
		} else {
			tmp_2 = t_0;
		}
		tmp_1 = tmp_2 / (x * x);
	} else if (x <= 4e-42) {
		tmp_1 = 0.5;
	} else {
		double tmp_3;
		if (2.0 != 0.0) {
			tmp_3 = sin(x) * tan((x / 2.0));
		} else {
			tmp_3 = t_0;
		}
		tmp_1 = (tmp_3 / x) / x;
	}
	return tmp_1;
}
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
    real(8) :: tmp_1
    real(8) :: tmp_2
    real(8) :: tmp_3
    t_0 = 1.0d0 - cos(x)
    if (x <= (-2d-5)) then
        if (2.0d0 /= 0.0d0) then
            tmp_2 = sin(x) * tan((x * 0.5d0))
        else
            tmp_2 = t_0
        end if
        tmp_1 = tmp_2 / (x * x)
    else if (x <= 4d-42) then
        tmp_1 = 0.5d0
    else
        if (2.0d0 /= 0.0d0) then
            tmp_3 = sin(x) * tan((x / 2.0d0))
        else
            tmp_3 = t_0
        end if
        tmp_1 = (tmp_3 / x) / x
    end if
    code = tmp_1
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_1;
	if (x <= -2e-5) {
		double tmp_2;
		if (2.0 != 0.0) {
			tmp_2 = Math.sin(x) * Math.tan((x * 0.5));
		} else {
			tmp_2 = t_0;
		}
		tmp_1 = tmp_2 / (x * x);
	} else if (x <= 4e-42) {
		tmp_1 = 0.5;
	} else {
		double tmp_3;
		if (2.0 != 0.0) {
			tmp_3 = Math.sin(x) * Math.tan((x / 2.0));
		} else {
			tmp_3 = t_0;
		}
		tmp_1 = (tmp_3 / x) / x;
	}
	return tmp_1;
}
def code(x):
	return (1.0 - math.cos(x)) / (x * x)
def code(x):
	t_0 = 1.0 - math.cos(x)
	tmp_1 = 0
	if x <= -2e-5:
		tmp_2 = 0
		if 2.0 != 0.0:
			tmp_2 = math.sin(x) * math.tan((x * 0.5))
		else:
			tmp_2 = t_0
		tmp_1 = tmp_2 / (x * x)
	elif x <= 4e-42:
		tmp_1 = 0.5
	else:
		tmp_3 = 0
		if 2.0 != 0.0:
			tmp_3 = math.sin(x) * math.tan((x / 2.0))
		else:
			tmp_3 = t_0
		tmp_1 = (tmp_3 / x) / x
	return tmp_1
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_1 = 0.0
	if (x <= -2e-5)
		tmp_2 = 0.0
		if (2.0 != 0.0)
			tmp_2 = Float64(sin(x) * tan(Float64(x * 0.5)));
		else
			tmp_2 = t_0;
		end
		tmp_1 = Float64(tmp_2 / Float64(x * x));
	elseif (x <= 4e-42)
		tmp_1 = 0.5;
	else
		tmp_3 = 0.0
		if (2.0 != 0.0)
			tmp_3 = Float64(sin(x) * tan(Float64(x / 2.0)));
		else
			tmp_3 = t_0;
		end
		tmp_1 = Float64(Float64(tmp_3 / x) / x);
	end
	return tmp_1
end
function tmp = code(x)
	tmp = (1.0 - cos(x)) / (x * x);
end
function tmp_5 = code(x)
	t_0 = 1.0 - cos(x);
	tmp_2 = 0.0;
	if (x <= -2e-5)
		tmp_3 = 0.0;
		if (2.0 ~= 0.0)
			tmp_3 = sin(x) * tan((x * 0.5));
		else
			tmp_3 = t_0;
		end
		tmp_2 = tmp_3 / (x * x);
	elseif (x <= 4e-42)
		tmp_2 = 0.5;
	else
		tmp_4 = 0.0;
		if (2.0 ~= 0.0)
			tmp_4 = sin(x) * tan((x / 2.0));
		else
			tmp_4 = t_0;
		end
		tmp_2 = (tmp_4 / x) / x;
	end
	tmp_5 = tmp_2;
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, -2e-5], N[(If[Unequal[2.0, 0.0], N[(N[Sin[x], $MachinePrecision] * N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$0] / N[(x * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 4e-42], 0.5, N[(N[(If[Unequal[2.0, 0.0], N[(N[Sin[x], $MachinePrecision] * N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$0] / x), $MachinePrecision] / x), $MachinePrecision]]]]
\frac{1 - \cos x}{x \cdot x}
\begin{array}{l}
t_0 := 1 - \cos x\\
\mathbf{if}\;x \leq -2 \cdot 10^{-5}:\\
\;\;\;\;\frac{\begin{array}{l}
\mathbf{if}\;2 \ne 0:\\
\;\;\;\;\sin x \cdot \tan \left(x \cdot 0.5\right)\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}}{x \cdot x}\\

\mathbf{elif}\;x \leq 4 \cdot 10^{-42}:\\
\;\;\;\;0.5\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{\begin{array}{l}
\mathbf{if}\;2 \ne 0:\\
\;\;\;\;\sin x \cdot \tan \left(\frac{x}{2}\right)\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}}{x}}{x}\\


\end{array}

Error

Derivation

  1. Split input into 3 regimes
  2. if x < -2.00000000000000016e-5

    1. Initial program 1.4

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

      \[\leadsto \frac{\color{blue}{\begin{array}{l} \color{blue}{\mathbf{if}\;1 + \cos x \ne 0:\\ \;\;\;\;\frac{{\sin x}^{2}}{1 + \cos x}\\ \mathbf{else}:\\ \;\;\;\;1 - \cos x\\ } \end{array}}}{x \cdot x} \]
    3. Applied egg-rr0.9

      \[\leadsto \color{blue}{0 - \frac{\begin{array}{l} \mathbf{if}\;1 + \cos x \ne 0:\\ \;\;\;\;\sin x \cdot \tan \left(\frac{x}{2}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \cos x\\ \end{array}}{-x \cdot x}} \]
    4. Simplified0.2

      \[\leadsto \color{blue}{\frac{\frac{\begin{array}{l} \mathbf{if}\;1 + \cos x \ne 0:\\ \;\;\;\;\sin x \cdot \tan \left(\frac{x}{2}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \cos x\\ \end{array}}{x}}{x}} \]
      Proof
    5. Taylor expanded in x around 0 0.2

      \[\leadsto \frac{\frac{\begin{array}{l} \mathbf{if}\;\color{blue}{2} \ne 0:\\ \;\;\;\;\sin x \cdot \tan \left(\frac{x}{2}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \cos x\\ \end{array}}{x}}{x} \]
    6. Applied egg-rr0.9

      \[\leadsto \color{blue}{\frac{\begin{array}{l} \mathbf{if}\;2 \ne 0:\\ \;\;\;\;\sin x \cdot \tan \left(x \cdot 0.5\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \cos x\\ \end{array}}{x \cdot x}} \]

    if -2.00000000000000016e-5 < x < 4.00000000000000015e-42

    1. Initial program 62.9

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

      \[\leadsto \color{blue}{0.5} \]

    if 4.00000000000000015e-42 < x

    1. Initial program 7.7

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

      \[\leadsto \frac{\color{blue}{\begin{array}{l} \color{blue}{\mathbf{if}\;1 + \cos x \ne 0:\\ \;\;\;\;\frac{{\sin x}^{2}}{1 + \cos x}\\ \mathbf{else}:\\ \;\;\;\;1 - \cos x\\ } \end{array}}}{x \cdot x} \]
    3. Applied egg-rr0.8

      \[\leadsto \color{blue}{0 - \frac{\begin{array}{l} \mathbf{if}\;1 + \cos x \ne 0:\\ \;\;\;\;\sin x \cdot \tan \left(\frac{x}{2}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \cos x\\ \end{array}}{-x \cdot x}} \]
    4. Simplified0.2

      \[\leadsto \color{blue}{\frac{\frac{\begin{array}{l} \mathbf{if}\;1 + \cos x \ne 0:\\ \;\;\;\;\sin x \cdot \tan \left(\frac{x}{2}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \cos x\\ \end{array}}{x}}{x}} \]
      Proof
    5. Taylor expanded in x around 0 0.2

      \[\leadsto \frac{\frac{\begin{array}{l} \mathbf{if}\;\color{blue}{2} \ne 0:\\ \;\;\;\;\sin x \cdot \tan \left(\frac{x}{2}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \cos x\\ \end{array}}{x}}{x} \]
  3. Recombined 3 regimes into one program.

Alternatives

Alternative 1
Error0.1
Cost13772
\[\begin{array}{l} t_0 := \frac{\frac{\begin{array}{l} \mathbf{if}\;2 \ne 0:\\ \;\;\;\;\sin x \cdot \tan \left(\frac{x}{2}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \cos x\\ \end{array}}{x}}{x}\\ \mathbf{if}\;x \leq -2 \cdot 10^{-5}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 4 \cdot 10^{-42}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error0.4
Cost7496
\[\begin{array}{l} t_0 := 1 - \cos x\\ \mathbf{if}\;x \leq -0.0047:\\ \;\;\;\;\begin{array}{l} \mathbf{if}\;0.5 \cdot \left(x \cdot x\right) \ne 0:\\ \;\;\;\;\frac{-1}{\frac{x \cdot x}{\cos x + -1}}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{x \cdot x}\\ \end{array}\\ \mathbf{elif}\;x \leq 0.005:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{x}}{x} \cdot t_0\\ \end{array} \]
Alternative 3
Error0.4
Cost7240
\[\begin{array}{l} t_0 := 1 - \cos x\\ \mathbf{if}\;x \leq -0.0047:\\ \;\;\;\;\frac{t_0}{x \cdot x}\\ \mathbf{elif}\;x \leq 0.005:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{x} \cdot \frac{1}{x}\\ \end{array} \]
Alternative 4
Error0.4
Cost7240
\[\begin{array}{l} t_0 := 1 - \cos x\\ \mathbf{if}\;x \leq -0.0047:\\ \;\;\;\;\frac{t_0}{x \cdot x}\\ \mathbf{elif}\;x \leq 0.005:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{x}}{x} \cdot t_0\\ \end{array} \]
Alternative 5
Error0.6
Cost7112
\[\begin{array}{l} t_0 := \frac{1 - \cos x}{x \cdot x}\\ \mathbf{if}\;x \leq -0.0047:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.005:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error0.4
Cost7112
\[\begin{array}{l} t_0 := 1 - \cos x\\ \mathbf{if}\;x \leq -0.0047:\\ \;\;\;\;\frac{t_0}{x \cdot x}\\ \mathbf{elif}\;x \leq 0.005:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{t_0}{x}}{x}\\ \end{array} \]
Alternative 7
Error15.7
Cost968
\[\begin{array}{l} t_0 := \frac{1}{x \cdot x} - \frac{\frac{1}{x}}{x}\\ \mathbf{if}\;x \leq -1.05 \cdot 10^{+46}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.8 \cdot 10^{+60}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error15.7
Cost840
\[\begin{array}{l} t_0 := \frac{0.5 \cdot x}{x \cdot x} \cdot x\\ \mathbf{if}\;x \leq -3.2:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 3.2:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 9
Error15.7
Cost840
\[\begin{array}{l} t_0 := \frac{\frac{1}{x} - \frac{1}{x}}{x}\\ \mathbf{if}\;x \leq -6.8 \cdot 10^{+76}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.6 \cdot 10^{+77}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 10
Error31.0
Cost64
\[0.5 \]

Error

Reproduce

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