?

Average Error: 31.7 → 0.3
Time: 12.8s
Precision: binary64
Cost: 13640

?

\[\frac{1 - \cos x}{x \cdot x} \]
\[\begin{array}{l} \mathbf{if}\;x \leq -0.033:\\ \;\;\;\;{x}^{-2} \cdot \left(1 - \cos x\right)\\ \mathbf{elif}\;x \leq 2 \cdot 10^{-5}:\\ \;\;\;\;0.5 + \left(x \cdot x\right) \cdot \left(\left(x \cdot x\right) \cdot 0.001388888888888889 + -0.041666666666666664\right)\\ \mathbf{else}:\\ \;\;\;\;\tan \left(x \cdot 0.5\right) \cdot \frac{\sin x}{x \cdot x}\\ \end{array} \]
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (* x x)))
(FPCore (x)
 :precision binary64
 (if (<= x -0.033)
   (* (pow x -2.0) (- 1.0 (cos x)))
   (if (<= x 2e-5)
     (+
      0.5
      (* (* x x) (+ (* (* x x) 0.001388888888888889) -0.041666666666666664)))
     (* (tan (* x 0.5)) (/ (sin x) (* x x))))))
double code(double x) {
	return (1.0 - cos(x)) / (x * x);
}
double code(double x) {
	double tmp;
	if (x <= -0.033) {
		tmp = pow(x, -2.0) * (1.0 - cos(x));
	} else if (x <= 2e-5) {
		tmp = 0.5 + ((x * x) * (((x * x) * 0.001388888888888889) + -0.041666666666666664));
	} else {
		tmp = tan((x * 0.5)) * (sin(x) / (x * 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.033d0)) then
        tmp = (x ** (-2.0d0)) * (1.0d0 - cos(x))
    else if (x <= 2d-5) then
        tmp = 0.5d0 + ((x * x) * (((x * x) * 0.001388888888888889d0) + (-0.041666666666666664d0)))
    else
        tmp = tan((x * 0.5d0)) * (sin(x) / (x * 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.033) {
		tmp = Math.pow(x, -2.0) * (1.0 - Math.cos(x));
	} else if (x <= 2e-5) {
		tmp = 0.5 + ((x * x) * (((x * x) * 0.001388888888888889) + -0.041666666666666664));
	} else {
		tmp = Math.tan((x * 0.5)) * (Math.sin(x) / (x * x));
	}
	return tmp;
}
def code(x):
	return (1.0 - math.cos(x)) / (x * x)
def code(x):
	tmp = 0
	if x <= -0.033:
		tmp = math.pow(x, -2.0) * (1.0 - math.cos(x))
	elif x <= 2e-5:
		tmp = 0.5 + ((x * x) * (((x * x) * 0.001388888888888889) + -0.041666666666666664))
	else:
		tmp = math.tan((x * 0.5)) * (math.sin(x) / (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.033)
		tmp = Float64((x ^ -2.0) * Float64(1.0 - cos(x)));
	elseif (x <= 2e-5)
		tmp = Float64(0.5 + Float64(Float64(x * x) * Float64(Float64(Float64(x * x) * 0.001388888888888889) + -0.041666666666666664)));
	else
		tmp = Float64(tan(Float64(x * 0.5)) * Float64(sin(x) / Float64(x * 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.033)
		tmp = (x ^ -2.0) * (1.0 - cos(x));
	elseif (x <= 2e-5)
		tmp = 0.5 + ((x * x) * (((x * x) * 0.001388888888888889) + -0.041666666666666664));
	else
		tmp = tan((x * 0.5)) * (sin(x) / (x * 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.033], N[(N[Power[x, -2.0], $MachinePrecision] * N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 2e-5], N[(0.5 + N[(N[(x * x), $MachinePrecision] * N[(N[(N[(x * x), $MachinePrecision] * 0.001388888888888889), $MachinePrecision] + -0.041666666666666664), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision] * N[(N[Sin[x], $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\frac{1 - \cos x}{x \cdot x}
\begin{array}{l}
\mathbf{if}\;x \leq -0.033:\\
\;\;\;\;{x}^{-2} \cdot \left(1 - \cos x\right)\\

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

\mathbf{else}:\\
\;\;\;\;\tan \left(x \cdot 0.5\right) \cdot \frac{\sin x}{x \cdot 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.033000000000000002

    1. Initial program 1.1

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

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

    if -0.033000000000000002 < x < 2.00000000000000016e-5

    1. Initial program 62.5

      \[\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}, x \cdot \left(x \cdot -0.041666666666666664\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)} \]

      *-commutative [=>]0.0

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

      unpow2 [=>]0.0

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

      associate-*l* [=>]0.0

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

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

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

    if 2.00000000000000016e-5 < x

    1. Initial program 1.1

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

      \[\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} \]

      associate-*l* [=>]1.1

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

      associate-*r/ [=>]1.0

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

      *-rgt-identity [=>]1.0

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

      hang-0p-tan [=>]0.7

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

      \[\leadsto \color{blue}{\frac{\sin x}{x \cdot x} \cdot \tan \left(x \cdot 0.5\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.3

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

Alternatives

Alternative 1
Error0.4
Cost13576
\[\begin{array}{l} \mathbf{if}\;x \leq -0.033:\\ \;\;\;\;{x}^{-2} \cdot \left(1 - \cos x\right)\\ \mathbf{elif}\;x \leq 0.0285:\\ \;\;\;\;0.5 + \left(x \cdot x\right) \cdot \left(\left(x \cdot x\right) \cdot 0.001388888888888889 + -0.041666666666666664\right)\\ \mathbf{else}:\\ \;\;\;\;{x}^{-2} - \frac{\cos x}{x \cdot x}\\ \end{array} \]
Alternative 2
Error0.4
Cost13376
\[\frac{\tan \left(x \cdot 0.5\right)}{x \cdot \frac{x}{\sin x}} \]
Alternative 3
Error0.4
Cost13316
\[\begin{array}{l} \mathbf{if}\;x \leq -0.033:\\ \;\;\;\;{x}^{-2} \cdot \left(1 - \cos x\right)\\ \mathbf{elif}\;x \leq 0.0285:\\ \;\;\;\;0.5 + \left(x \cdot x\right) \cdot \left(\left(x \cdot x\right) \cdot 0.001388888888888889 + -0.041666666666666664\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x \cdot x} + \cos x \cdot \frac{-1}{x \cdot x}\\ \end{array} \]
Alternative 4
Error0.4
Cost7496
\[\begin{array}{l} \mathbf{if}\;x \leq -0.033:\\ \;\;\;\;\left(\cos x + -1\right) \cdot \frac{\frac{-1}{x}}{x}\\ \mathbf{elif}\;x \leq 0.0285:\\ \;\;\;\;0.5 + \left(x \cdot x\right) \cdot \left(\left(x \cdot x\right) \cdot 0.001388888888888889 + -0.041666666666666664\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x \cdot x} + \cos x \cdot \frac{-1}{x \cdot x}\\ \end{array} \]
Alternative 5
Error0.3
Cost7241
\[\begin{array}{l} \mathbf{if}\;x \leq -0.033 \lor \neg \left(x \leq 0.035\right):\\ \;\;\;\;\left(\cos x + -1\right) \cdot \frac{\frac{-1}{x}}{x}\\ \mathbf{else}:\\ \;\;\;\;0.5 + \left(x \cdot x\right) \cdot \left(\left(x \cdot x\right) \cdot 0.001388888888888889 + -0.041666666666666664\right)\\ \end{array} \]
Alternative 6
Error0.5
Cost7113
\[\begin{array}{l} \mathbf{if}\;x \leq -0.033 \lor \neg \left(x \leq 0.035\right):\\ \;\;\;\;\frac{1 - \cos x}{x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;0.5 + \left(x \cdot x\right) \cdot \left(\left(x \cdot x\right) \cdot 0.001388888888888889 + -0.041666666666666664\right)\\ \end{array} \]
Alternative 7
Error0.4
Cost7112
\[\begin{array}{l} t_0 := 1 - \cos x\\ \mathbf{if}\;x \leq -0.033:\\ \;\;\;\;\frac{\frac{t_0}{x}}{x}\\ \mathbf{elif}\;x \leq 0.035:\\ \;\;\;\;0.5 + \left(x \cdot x\right) \cdot \left(\left(x \cdot x\right) \cdot 0.001388888888888889 + -0.041666666666666664\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{x \cdot x}\\ \end{array} \]
Alternative 8
Error15.1
Cost1097
\[\begin{array}{l} \mathbf{if}\;x \leq -7.5 \cdot 10^{+38} \lor \neg \left(x \leq 6.7 \cdot 10^{+38}\right):\\ \;\;\;\;\frac{0}{x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;0.5 + \left(x \cdot x\right) \cdot \left(\left(x \cdot x\right) \cdot 0.001388888888888889 + -0.041666666666666664\right)\\ \end{array} \]
Alternative 9
Error13.7
Cost896
\[\frac{\frac{-1}{x \cdot 0.16666666666666666 + 2 \cdot \frac{1}{x}}}{-x} \]
Alternative 10
Error15.3
Cost713
\[\begin{array}{l} \mathbf{if}\;x \leq -3.5 \lor \neg \left(x \leq 3.5\right):\\ \;\;\;\;\frac{0}{x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;0.5 + \left(x \cdot x\right) \cdot -0.041666666666666664\\ \end{array} \]
Alternative 11
Error30.8
Cost64
\[0.5 \]

Error

Reproduce?

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