Average Error: 31.3 → 0.5
Time: 11.3s
Precision: binary64
Cost: 13508
\[\frac{1 - \cos x}{x \cdot x} \]
\[\begin{array}{l} \mathbf{if}\;x \leq -0.2:\\ \;\;\;\;\frac{\tan \left(\frac{x}{2}\right)}{x \cdot x} \cdot \sin x\\ \mathbf{elif}\;x \leq 0.0305:\\ \;\;\;\;0.5 + \left(0.001388888888888889 \cdot {x}^{4} + \left(x \cdot x\right) \cdot -0.041666666666666664\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{-1}{x \cdot \frac{x}{-1 + \cos x}}\\ \end{array} \]
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (* x x)))
(FPCore (x)
 :precision binary64
 (if (<= x -0.2)
   (* (/ (tan (/ x 2.0)) (* x x)) (sin x))
   (if (<= x 0.0305)
     (+
      0.5
      (+
       (* 0.001388888888888889 (pow x 4.0))
       (* (* x x) -0.041666666666666664)))
     (/ -1.0 (* x (/ x (+ -1.0 (cos x))))))))
double code(double x) {
	return (1.0 - cos(x)) / (x * x);
}
double code(double x) {
	double tmp;
	if (x <= -0.2) {
		tmp = (tan((x / 2.0)) / (x * x)) * sin(x);
	} else if (x <= 0.0305) {
		tmp = 0.5 + ((0.001388888888888889 * pow(x, 4.0)) + ((x * x) * -0.041666666666666664));
	} else {
		tmp = -1.0 / (x * (x / (-1.0 + cos(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) :: tmp
    if (x <= (-0.2d0)) then
        tmp = (tan((x / 2.0d0)) / (x * x)) * sin(x)
    else if (x <= 0.0305d0) then
        tmp = 0.5d0 + ((0.001388888888888889d0 * (x ** 4.0d0)) + ((x * x) * (-0.041666666666666664d0)))
    else
        tmp = (-1.0d0) / (x * (x / ((-1.0d0) + cos(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 tmp;
	if (x <= -0.2) {
		tmp = (Math.tan((x / 2.0)) / (x * x)) * Math.sin(x);
	} else if (x <= 0.0305) {
		tmp = 0.5 + ((0.001388888888888889 * Math.pow(x, 4.0)) + ((x * x) * -0.041666666666666664));
	} else {
		tmp = -1.0 / (x * (x / (-1.0 + Math.cos(x))));
	}
	return tmp;
}
def code(x):
	return (1.0 - math.cos(x)) / (x * x)
def code(x):
	tmp = 0
	if x <= -0.2:
		tmp = (math.tan((x / 2.0)) / (x * x)) * math.sin(x)
	elif x <= 0.0305:
		tmp = 0.5 + ((0.001388888888888889 * math.pow(x, 4.0)) + ((x * x) * -0.041666666666666664))
	else:
		tmp = -1.0 / (x * (x / (-1.0 + math.cos(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.2)
		tmp = Float64(Float64(tan(Float64(x / 2.0)) / Float64(x * x)) * sin(x));
	elseif (x <= 0.0305)
		tmp = Float64(0.5 + Float64(Float64(0.001388888888888889 * (x ^ 4.0)) + Float64(Float64(x * x) * -0.041666666666666664)));
	else
		tmp = Float64(-1.0 / Float64(x * Float64(x / Float64(-1.0 + cos(x)))));
	end
	return tmp
end
function tmp = code(x)
	tmp = (1.0 - cos(x)) / (x * x);
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= -0.2)
		tmp = (tan((x / 2.0)) / (x * x)) * sin(x);
	elseif (x <= 0.0305)
		tmp = 0.5 + ((0.001388888888888889 * (x ^ 4.0)) + ((x * x) * -0.041666666666666664));
	else
		tmp = -1.0 / (x * (x / (-1.0 + cos(x))));
	end
	tmp_2 = tmp;
end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]
code[x_] := If[LessEqual[x, -0.2], N[(N[(N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 0.0305], N[(0.5 + N[(N[(0.001388888888888889 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] + N[(N[(x * x), $MachinePrecision] * -0.041666666666666664), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-1.0 / N[(x * N[(x / N[(-1.0 + N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\frac{1 - \cos x}{x \cdot x}
\begin{array}{l}
\mathbf{if}\;x \leq -0.2:\\
\;\;\;\;\frac{\tan \left(\frac{x}{2}\right)}{x \cdot x} \cdot \sin x\\

\mathbf{elif}\;x \leq 0.0305:\\
\;\;\;\;0.5 + \left(0.001388888888888889 \cdot {x}^{4} + \left(x \cdot x\right) \cdot -0.041666666666666664\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{-1}{x \cdot \frac{x}{-1 + \cos 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.20000000000000001

    1. Initial program 1.0

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

      \[\leadsto \frac{\color{blue}{\left(\sin x \cdot \sin x\right) \cdot \frac{1}{1 + \cos x}}}{x \cdot x} \]
    3. Simplified0.8

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

      [Start]1.1

      \[ \frac{\left(\sin x \cdot \sin x\right) \cdot \frac{1}{1 + \cos x}}{x \cdot x} \]

      /-rgt-identity [<=]1.1

      \[ \frac{\color{blue}{\frac{\sin x \cdot \sin x}{1}} \cdot \frac{1}{1 + \cos x}}{x \cdot x} \]

      associate-/r/ [<=]1.1

      \[ \frac{\color{blue}{\frac{\sin x \cdot \sin x}{\frac{1}{\frac{1}{1 + \cos x}}}}}{x \cdot x} \]

      remove-double-div [=>]1.1

      \[ \frac{\frac{\sin x \cdot \sin x}{\color{blue}{1 + \cos x}}}{x \cdot x} \]

      associate-*l/ [<=]1.1

      \[ \frac{\color{blue}{\frac{\sin x}{1 + \cos x} \cdot \sin x}}{x \cdot x} \]

      *-commutative [=>]1.1

      \[ \frac{\color{blue}{\sin x \cdot \frac{\sin x}{1 + \cos x}}}{x \cdot x} \]

      hang-0p-tan [=>]0.8

      \[ \frac{\sin x \cdot \color{blue}{\tan \left(\frac{x}{2}\right)}}{x \cdot x} \]
    4. Applied egg-rr0.8

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

    if -0.20000000000000001 < x < 0.030499999999999999

    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 + \left(-0.041666666666666664 \cdot {x}^{2} + 0.001388888888888889 \cdot {x}^{4}\right)} \]
    3. Simplified0.0

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

      [Start]0.0

      \[ 0.5 + \left(-0.041666666666666664 \cdot {x}^{2} + 0.001388888888888889 \cdot {x}^{4}\right) \]

      +-commutative [=>]0.0

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

      fma-def [=>]0.0

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

      unpow2 [=>]0.0

      \[ 0.5 + \mathsf{fma}\left(0.001388888888888889, {x}^{4}, -0.041666666666666664 \cdot \color{blue}{\left(x \cdot x\right)}\right) \]
    4. Applied egg-rr0.0

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

    if 0.030499999999999999 < x

    1. Initial program 1.1

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

      \[\leadsto \color{blue}{\frac{-1}{x \cdot x} \cdot \left(-1 + \cos x\right)} \]
    3. Applied egg-rr1.2

      \[\leadsto \color{blue}{{\left(x \cdot \left(-x\right)\right)}^{-1}} \cdot \left(-1 + \cos x\right) \]
    4. Simplified0.5

      \[\leadsto \color{blue}{\frac{\frac{1}{x}}{-x}} \cdot \left(-1 + \cos x\right) \]
      Proof

      [Start]1.2

      \[ {\left(x \cdot \left(-x\right)\right)}^{-1} \cdot \left(-1 + \cos x\right) \]

      unpow-1 [=>]1.2

      \[ \color{blue}{\frac{1}{x \cdot \left(-x\right)}} \cdot \left(-1 + \cos x\right) \]

      associate-/r* [=>]0.5

      \[ \color{blue}{\frac{\frac{1}{x}}{-x}} \cdot \left(-1 + \cos x\right) \]
    5. Applied egg-rr1.2

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -0.2:\\ \;\;\;\;\frac{\tan \left(\frac{x}{2}\right)}{x \cdot x} \cdot \sin x\\ \mathbf{elif}\;x \leq 0.0305:\\ \;\;\;\;0.5 + \left(0.001388888888888889 \cdot {x}^{4} + \left(x \cdot x\right) \cdot -0.041666666666666664\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{-1}{x \cdot \frac{x}{-1 + \cos x}}\\ \end{array} \]

Alternatives

Alternative 1
Error0.4
Cost7432
\[\begin{array}{l} \mathbf{if}\;x \leq -0.035:\\ \;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\ \mathbf{elif}\;x \leq 0.0305:\\ \;\;\;\;0.5 + \left(0.001388888888888889 \cdot {x}^{4} + \left(x \cdot x\right) \cdot -0.041666666666666664\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{-1}{x \cdot \frac{x}{-1 + \cos x}}\\ \end{array} \]
Alternative 2
Error0.4
Cost7240
\[\begin{array}{l} \mathbf{if}\;x \leq -0.0052:\\ \;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\ \mathbf{elif}\;x \leq 0.0053:\\ \;\;\;\;0.5 + \left(-1 + \left(\left(x \cdot x\right) \cdot -0.041666666666666664 + 1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{-1}{x \cdot \frac{x}{-1 + \cos x}}\\ \end{array} \]
Alternative 3
Error0.6
Cost7113
\[\begin{array}{l} \mathbf{if}\;x \leq -0.0052 \lor \neg \left(x \leq 0.0053\right):\\ \;\;\;\;\frac{1 - \cos x}{x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;0.5 + \left(-1 + \left(\left(x \cdot x\right) \cdot -0.041666666666666664 + 1\right)\right)\\ \end{array} \]
Alternative 4
Error0.3
Cost7113
\[\begin{array}{l} \mathbf{if}\;x \leq -0.0052 \lor \neg \left(x \leq 0.0053\right):\\ \;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\ \mathbf{else}:\\ \;\;\;\;0.5 + \left(-1 + \left(\left(x \cdot x\right) \cdot -0.041666666666666664 + 1\right)\right)\\ \end{array} \]
Alternative 5
Error13.3
Cost832
\[\frac{\frac{1}{x}}{x \cdot 0.16666666666666666 + 2 \cdot \frac{1}{x}} \]
Alternative 6
Error14.8
Cost328
\[\begin{array}{l} \mathbf{if}\;x \leq -1.35 \cdot 10^{+77}:\\ \;\;\;\;0\\ \mathbf{elif}\;x \leq 1.55 \cdot 10^{+77}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
Alternative 7
Error45.5
Cost64
\[0 \]

Error

Reproduce

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