cos2 (problem 3.4.1)

Percentage Accurate: 51.4% → 99.8%
Time: 9.5s
Alternatives: 5
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \frac{1 - \cos x}{x \cdot x} \end{array} \]
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (* x x)))
double code(double x) {
	return (1.0 - cos(x)) / (x * x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 - cos(x)) / (x * x)
end function
public static double code(double x) {
	return (1.0 - Math.cos(x)) / (x * x);
}
def code(x):
	return (1.0 - math.cos(x)) / (x * x)
function code(x)
	return Float64(Float64(1.0 - cos(x)) / Float64(x * x))
end
function tmp = code(x)
	tmp = (1.0 - cos(x)) / (x * x);
end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1 - \cos x}{x \cdot x}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 5 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 51.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1 - \cos x}{x \cdot x} \end{array} \]
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (* x x)))
double code(double x) {
	return (1.0 - cos(x)) / (x * x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 - cos(x)) / (x * x)
end function
public static double code(double x) {
	return (1.0 - Math.cos(x)) / (x * x);
}
def code(x):
	return (1.0 - math.cos(x)) / (x * x)
function code(x)
	return Float64(Float64(1.0 - cos(x)) / Float64(x * x))
end
function tmp = code(x)
	tmp = (1.0 - cos(x)) / (x * x);
end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1 - \cos x}{x \cdot x}
\end{array}

Alternative 1: 99.8% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \frac{\tan \left(x \cdot 0.5\right)}{x} \cdot \frac{\sin x}{x} \end{array} \]
(FPCore (x) :precision binary64 (* (/ (tan (* x 0.5)) x) (/ (sin x) x)))
double code(double x) {
	return (tan((x * 0.5)) / x) * (sin(x) / x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (tan((x * 0.5d0)) / x) * (sin(x) / x)
end function
public static double code(double x) {
	return (Math.tan((x * 0.5)) / x) * (Math.sin(x) / x);
}
def code(x):
	return (math.tan((x * 0.5)) / x) * (math.sin(x) / x)
function code(x)
	return Float64(Float64(tan(Float64(x * 0.5)) / x) * Float64(sin(x) / x))
end
function tmp = code(x)
	tmp = (tan((x * 0.5)) / x) * (sin(x) / x);
end
code[x_] := N[(N[(N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision] * N[(N[Sin[x], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\tan \left(x \cdot 0.5\right)}{x} \cdot \frac{\sin x}{x}
\end{array}
Derivation
  1. Initial program 53.3%

    \[\frac{1 - \cos x}{x \cdot x} \]
  2. Step-by-step derivation
    1. flip--53.1%

      \[\leadsto \frac{\color{blue}{\frac{1 \cdot 1 - \cos x \cdot \cos x}{1 + \cos x}}}{x \cdot x} \]
    2. div-inv53.0%

      \[\leadsto \frac{\color{blue}{\left(1 \cdot 1 - \cos x \cdot \cos x\right) \cdot \frac{1}{1 + \cos x}}}{x \cdot x} \]
    3. metadata-eval53.0%

      \[\leadsto \frac{\left(\color{blue}{1} - \cos x \cdot \cos x\right) \cdot \frac{1}{1 + \cos x}}{x \cdot x} \]
    4. 1-sub-cos74.4%

      \[\leadsto \frac{\color{blue}{\left(\sin x \cdot \sin x\right)} \cdot \frac{1}{1 + \cos x}}{x \cdot x} \]
    5. pow274.4%

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

    \[\leadsto \frac{\color{blue}{{\sin x}^{2} \cdot \frac{1}{1 + \cos x}}}{x \cdot x} \]
  4. Step-by-step derivation
    1. unpow274.4%

      \[\leadsto \frac{\color{blue}{\left(\sin x \cdot \sin x\right)} \cdot \frac{1}{1 + \cos x}}{x \cdot x} \]
    2. associate-*l*74.4%

      \[\leadsto \frac{\color{blue}{\sin x \cdot \left(\sin x \cdot \frac{1}{1 + \cos x}\right)}}{x \cdot x} \]
    3. associate-*r/74.4%

      \[\leadsto \frac{\sin x \cdot \color{blue}{\frac{\sin x \cdot 1}{1 + \cos x}}}{x \cdot x} \]
    4. *-rgt-identity74.4%

      \[\leadsto \frac{\sin x \cdot \frac{\color{blue}{\sin x}}{1 + \cos x}}{x \cdot x} \]
    5. hang-0p-tan74.7%

      \[\leadsto \frac{\sin x \cdot \color{blue}{\tan \left(\frac{x}{2}\right)}}{x \cdot x} \]
  5. Simplified74.7%

    \[\leadsto \frac{\color{blue}{\sin x \cdot \tan \left(\frac{x}{2}\right)}}{x \cdot x} \]
  6. Step-by-step derivation
    1. *-commutative74.7%

      \[\leadsto \frac{\color{blue}{\tan \left(\frac{x}{2}\right) \cdot \sin x}}{x \cdot x} \]
    2. times-frac99.8%

      \[\leadsto \color{blue}{\frac{\tan \left(\frac{x}{2}\right)}{x} \cdot \frac{\sin x}{x}} \]
    3. div-inv99.8%

      \[\leadsto \frac{\tan \color{blue}{\left(x \cdot \frac{1}{2}\right)}}{x} \cdot \frac{\sin x}{x} \]
    4. metadata-eval99.8%

      \[\leadsto \frac{\tan \left(x \cdot \color{blue}{0.5}\right)}{x} \cdot \frac{\sin x}{x} \]
  7. Applied egg-rr99.8%

    \[\leadsto \color{blue}{\frac{\tan \left(x \cdot 0.5\right)}{x} \cdot \frac{\sin x}{x}} \]
  8. Final simplification99.8%

    \[\leadsto \frac{\tan \left(x \cdot 0.5\right)}{x} \cdot \frac{\sin x}{x} \]

Alternative 2: 74.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 0.0045:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1 - \cos x}{x \cdot x}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x 0.0045)
   (+ 0.5 (* -0.041666666666666664 (* x x)))
   (/ (- 1.0 (cos x)) (* x x))))
double code(double x) {
	double tmp;
	if (x <= 0.0045) {
		tmp = 0.5 + (-0.041666666666666664 * (x * x));
	} else {
		tmp = (1.0 - cos(x)) / (x * x);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= 0.0045d0) then
        tmp = 0.5d0 + ((-0.041666666666666664d0) * (x * x))
    else
        tmp = (1.0d0 - cos(x)) / (x * x)
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (x <= 0.0045) {
		tmp = 0.5 + (-0.041666666666666664 * (x * x));
	} else {
		tmp = (1.0 - Math.cos(x)) / (x * x);
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= 0.0045:
		tmp = 0.5 + (-0.041666666666666664 * (x * x))
	else:
		tmp = (1.0 - math.cos(x)) / (x * x)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= 0.0045)
		tmp = Float64(0.5 + Float64(-0.041666666666666664 * Float64(x * x)));
	else
		tmp = Float64(Float64(1.0 - cos(x)) / Float64(x * x));
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= 0.0045)
		tmp = 0.5 + (-0.041666666666666664 * (x * x));
	else
		tmp = (1.0 - cos(x)) / (x * x);
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[x, 0.0045], N[(0.5 + N[(-0.041666666666666664 * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0045:\\
\;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x \cdot x}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 0.00449999999999999966

    1. Initial program 35.7%

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

      \[\leadsto \color{blue}{0.5 + -0.041666666666666664 \cdot {x}^{2}} \]
    3. Step-by-step derivation
      1. unpow266.3%

        \[\leadsto 0.5 + -0.041666666666666664 \cdot \color{blue}{\left(x \cdot x\right)} \]
    4. Simplified66.3%

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

    if 0.00449999999999999966 < x

    1. Initial program 98.0%

      \[\frac{1 - \cos x}{x \cdot x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification75.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 0.0045:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1 - \cos x}{x \cdot x}\\ \end{array} \]

Alternative 3: 74.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 0.0045:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x 0.0045)
   (+ 0.5 (* -0.041666666666666664 (* x x)))
   (/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
	double tmp;
	if (x <= 0.0045) {
		tmp = 0.5 + (-0.041666666666666664 * (x * x));
	} else {
		tmp = ((1.0 - cos(x)) / x) / x;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= 0.0045d0) then
        tmp = 0.5d0 + ((-0.041666666666666664d0) * (x * x))
    else
        tmp = ((1.0d0 - cos(x)) / x) / x
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (x <= 0.0045) {
		tmp = 0.5 + (-0.041666666666666664 * (x * x));
	} else {
		tmp = ((1.0 - Math.cos(x)) / x) / x;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= 0.0045:
		tmp = 0.5 + (-0.041666666666666664 * (x * x))
	else:
		tmp = ((1.0 - math.cos(x)) / x) / x
	return tmp
function code(x)
	tmp = 0.0
	if (x <= 0.0045)
		tmp = Float64(0.5 + Float64(-0.041666666666666664 * Float64(x * x)));
	else
		tmp = Float64(Float64(Float64(1.0 - cos(x)) / x) / x);
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= 0.0045)
		tmp = 0.5 + (-0.041666666666666664 * (x * x));
	else
		tmp = ((1.0 - cos(x)) / x) / x;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[x, 0.0045], N[(0.5 + N[(-0.041666666666666664 * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0045:\\
\;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 0.00449999999999999966

    1. Initial program 35.7%

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

      \[\leadsto \color{blue}{0.5 + -0.041666666666666664 \cdot {x}^{2}} \]
    3. Step-by-step derivation
      1. unpow266.3%

        \[\leadsto 0.5 + -0.041666666666666664 \cdot \color{blue}{\left(x \cdot x\right)} \]
    4. Simplified66.3%

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

    if 0.00449999999999999966 < x

    1. Initial program 98.0%

      \[\frac{1 - \cos x}{x \cdot x} \]
    2. Step-by-step derivation
      1. associate-/r*99.2%

        \[\leadsto \color{blue}{\frac{\frac{1 - \cos x}{x}}{x}} \]
      2. div-inv99.2%

        \[\leadsto \color{blue}{\frac{1 - \cos x}{x} \cdot \frac{1}{x}} \]
    3. Applied egg-rr99.2%

      \[\leadsto \color{blue}{\frac{1 - \cos x}{x} \cdot \frac{1}{x}} \]
    4. Step-by-step derivation
      1. un-div-inv99.2%

        \[\leadsto \color{blue}{\frac{\frac{1 - \cos x}{x}}{x}} \]
    5. Applied egg-rr99.2%

      \[\leadsto \color{blue}{\frac{\frac{1 - \cos x}{x}}{x}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification75.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 0.0045:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\ \end{array} \]

Alternative 4: 52.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \frac{\sin x}{x} \end{array} \]
(FPCore (x) :precision binary64 (* 0.5 (/ (sin x) x)))
double code(double x) {
	return 0.5 * (sin(x) / x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 0.5d0 * (sin(x) / x)
end function
public static double code(double x) {
	return 0.5 * (Math.sin(x) / x);
}
def code(x):
	return 0.5 * (math.sin(x) / x)
function code(x)
	return Float64(0.5 * Float64(sin(x) / x))
end
function tmp = code(x)
	tmp = 0.5 * (sin(x) / x);
end
code[x_] := N[(0.5 * N[(N[Sin[x], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \frac{\sin x}{x}
\end{array}
Derivation
  1. Initial program 53.3%

    \[\frac{1 - \cos x}{x \cdot x} \]
  2. Step-by-step derivation
    1. flip--53.1%

      \[\leadsto \frac{\color{blue}{\frac{1 \cdot 1 - \cos x \cdot \cos x}{1 + \cos x}}}{x \cdot x} \]
    2. div-inv53.0%

      \[\leadsto \frac{\color{blue}{\left(1 \cdot 1 - \cos x \cdot \cos x\right) \cdot \frac{1}{1 + \cos x}}}{x \cdot x} \]
    3. metadata-eval53.0%

      \[\leadsto \frac{\left(\color{blue}{1} - \cos x \cdot \cos x\right) \cdot \frac{1}{1 + \cos x}}{x \cdot x} \]
    4. 1-sub-cos74.4%

      \[\leadsto \frac{\color{blue}{\left(\sin x \cdot \sin x\right)} \cdot \frac{1}{1 + \cos x}}{x \cdot x} \]
    5. pow274.4%

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

    \[\leadsto \frac{\color{blue}{{\sin x}^{2} \cdot \frac{1}{1 + \cos x}}}{x \cdot x} \]
  4. Step-by-step derivation
    1. unpow274.4%

      \[\leadsto \frac{\color{blue}{\left(\sin x \cdot \sin x\right)} \cdot \frac{1}{1 + \cos x}}{x \cdot x} \]
    2. associate-*l*74.4%

      \[\leadsto \frac{\color{blue}{\sin x \cdot \left(\sin x \cdot \frac{1}{1 + \cos x}\right)}}{x \cdot x} \]
    3. associate-*r/74.4%

      \[\leadsto \frac{\sin x \cdot \color{blue}{\frac{\sin x \cdot 1}{1 + \cos x}}}{x \cdot x} \]
    4. *-rgt-identity74.4%

      \[\leadsto \frac{\sin x \cdot \frac{\color{blue}{\sin x}}{1 + \cos x}}{x \cdot x} \]
    5. hang-0p-tan74.7%

      \[\leadsto \frac{\sin x \cdot \color{blue}{\tan \left(\frac{x}{2}\right)}}{x \cdot x} \]
  5. Simplified74.7%

    \[\leadsto \frac{\color{blue}{\sin x \cdot \tan \left(\frac{x}{2}\right)}}{x \cdot x} \]
  6. Step-by-step derivation
    1. *-commutative74.7%

      \[\leadsto \frac{\color{blue}{\tan \left(\frac{x}{2}\right) \cdot \sin x}}{x \cdot x} \]
    2. times-frac99.8%

      \[\leadsto \color{blue}{\frac{\tan \left(\frac{x}{2}\right)}{x} \cdot \frac{\sin x}{x}} \]
    3. div-inv99.8%

      \[\leadsto \frac{\tan \color{blue}{\left(x \cdot \frac{1}{2}\right)}}{x} \cdot \frac{\sin x}{x} \]
    4. metadata-eval99.8%

      \[\leadsto \frac{\tan \left(x \cdot \color{blue}{0.5}\right)}{x} \cdot \frac{\sin x}{x} \]
  7. Applied egg-rr99.8%

    \[\leadsto \color{blue}{\frac{\tan \left(x \cdot 0.5\right)}{x} \cdot \frac{\sin x}{x}} \]
  8. Taylor expanded in x around 0 50.3%

    \[\leadsto \color{blue}{0.5} \cdot \frac{\sin x}{x} \]
  9. Final simplification50.3%

    \[\leadsto 0.5 \cdot \frac{\sin x}{x} \]

Alternative 5: 51.1% accurate, 107.0× speedup?

\[\begin{array}{l} \\ 0.5 \end{array} \]
(FPCore (x) :precision binary64 0.5)
double code(double x) {
	return 0.5;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 0.5d0
end function
public static double code(double x) {
	return 0.5;
}
def code(x):
	return 0.5
function code(x)
	return 0.5
end
function tmp = code(x)
	tmp = 0.5;
end
code[x_] := 0.5
\begin{array}{l}

\\
0.5
\end{array}
Derivation
  1. Initial program 53.3%

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

    \[\leadsto \color{blue}{0.5} \]
  3. Final simplification49.2%

    \[\leadsto 0.5 \]

Reproduce

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