2cos (problem 3.3.5)

Percentage Accurate: 37.4% → 99.4%
Time: 18.8s
Alternatives: 13
Speedup: 1.8×

Specification

?
\[\begin{array}{l} \\ \cos \left(x + \varepsilon\right) - \cos x \end{array} \]
(FPCore (x eps) :precision binary64 (- (cos (+ x eps)) (cos x)))
double code(double x, double eps) {
	return cos((x + eps)) - cos(x);
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    code = cos((x + eps)) - cos(x)
end function
public static double code(double x, double eps) {
	return Math.cos((x + eps)) - Math.cos(x);
}
def code(x, eps):
	return math.cos((x + eps)) - math.cos(x)
function code(x, eps)
	return Float64(cos(Float64(x + eps)) - cos(x))
end
function tmp = code(x, eps)
	tmp = cos((x + eps)) - cos(x);
end
code[x_, eps_] := N[(N[Cos[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos \left(x + \varepsilon\right) - \cos 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 13 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: 37.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos \left(x + \varepsilon\right) - \cos x \end{array} \]
(FPCore (x eps) :precision binary64 (- (cos (+ x eps)) (cos x)))
double code(double x, double eps) {
	return cos((x + eps)) - cos(x);
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    code = cos((x + eps)) - cos(x)
end function
public static double code(double x, double eps) {
	return Math.cos((x + eps)) - Math.cos(x);
}
def code(x, eps):
	return math.cos((x + eps)) - math.cos(x)
function code(x, eps)
	return Float64(cos(Float64(x + eps)) - cos(x))
end
function tmp = code(x, eps)
	tmp = cos((x + eps)) - cos(x);
end
code[x_, eps_] := N[(N[Cos[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos \left(x + \varepsilon\right) - \cos x
\end{array}

Alternative 1: 99.4% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(\varepsilon \cdot 0.5\right)\\ \left(t_0 \cdot \cos x + \cos \left(\varepsilon \cdot 0.5\right) \cdot \sin x\right) \cdot \left(t_0 \cdot -2\right) \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (sin (* eps 0.5))))
   (* (+ (* t_0 (cos x)) (* (cos (* eps 0.5)) (sin x))) (* t_0 -2.0))))
double code(double x, double eps) {
	double t_0 = sin((eps * 0.5));
	return ((t_0 * cos(x)) + (cos((eps * 0.5)) * sin(x))) * (t_0 * -2.0);
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    real(8) :: t_0
    t_0 = sin((eps * 0.5d0))
    code = ((t_0 * cos(x)) + (cos((eps * 0.5d0)) * sin(x))) * (t_0 * (-2.0d0))
end function
public static double code(double x, double eps) {
	double t_0 = Math.sin((eps * 0.5));
	return ((t_0 * Math.cos(x)) + (Math.cos((eps * 0.5)) * Math.sin(x))) * (t_0 * -2.0);
}
def code(x, eps):
	t_0 = math.sin((eps * 0.5))
	return ((t_0 * math.cos(x)) + (math.cos((eps * 0.5)) * math.sin(x))) * (t_0 * -2.0)
function code(x, eps)
	t_0 = sin(Float64(eps * 0.5))
	return Float64(Float64(Float64(t_0 * cos(x)) + Float64(cos(Float64(eps * 0.5)) * sin(x))) * Float64(t_0 * -2.0))
end
function tmp = code(x, eps)
	t_0 = sin((eps * 0.5));
	tmp = ((t_0 * cos(x)) + (cos((eps * 0.5)) * sin(x))) * (t_0 * -2.0);
end
code[x_, eps_] := Block[{t$95$0 = N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(t$95$0 * N[Cos[x], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(t$95$0 * -2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(\varepsilon \cdot 0.5\right)\\
\left(t_0 \cdot \cos x + \cos \left(\varepsilon \cdot 0.5\right) \cdot \sin x\right) \cdot \left(t_0 \cdot -2\right)
\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 2: 98.6% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -0.027:\\ \;\;\;\;\mathsf{fma}\left(\cos \varepsilon + -1, \cos x, \sin x \cdot \left(-\sin \varepsilon\right)\right)\\ \mathbf{elif}\;x \leq 6.8 \cdot 10^{-52}:\\ \;\;\;\;\sin \left(\varepsilon \cdot 0.5\right) \cdot \left(-2 \cdot \sin \left(\varepsilon \cdot 0.5 + x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\cos x \cdot \cos \varepsilon - \cos x\right) - \sin x \cdot \sin \varepsilon\\ \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (if (<= x -0.027)
   (fma (+ (cos eps) -1.0) (cos x) (* (sin x) (- (sin eps))))
   (if (<= x 6.8e-52)
     (* (sin (* eps 0.5)) (* -2.0 (sin (+ (* eps 0.5) x))))
     (- (- (* (cos x) (cos eps)) (cos x)) (* (sin x) (sin eps))))))
double code(double x, double eps) {
	double tmp;
	if (x <= -0.027) {
		tmp = fma((cos(eps) + -1.0), cos(x), (sin(x) * -sin(eps)));
	} else if (x <= 6.8e-52) {
		tmp = sin((eps * 0.5)) * (-2.0 * sin(((eps * 0.5) + x)));
	} else {
		tmp = ((cos(x) * cos(eps)) - cos(x)) - (sin(x) * sin(eps));
	}
	return tmp;
}
function code(x, eps)
	tmp = 0.0
	if (x <= -0.027)
		tmp = fma(Float64(cos(eps) + -1.0), cos(x), Float64(sin(x) * Float64(-sin(eps))));
	elseif (x <= 6.8e-52)
		tmp = Float64(sin(Float64(eps * 0.5)) * Float64(-2.0 * sin(Float64(Float64(eps * 0.5) + x))));
	else
		tmp = Float64(Float64(Float64(cos(x) * cos(eps)) - cos(x)) - Float64(sin(x) * sin(eps)));
	end
	return tmp
end
code[x_, eps_] := If[LessEqual[x, -0.027], N[(N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision] * N[Cos[x], $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * (-N[Sin[eps], $MachinePrecision])), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 6.8e-52], N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[(-2.0 * N[Sin[N[(N[(eps * 0.5), $MachinePrecision] + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[Cos[x], $MachinePrecision] * N[Cos[eps], $MachinePrecision]), $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision] - N[(N[Sin[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.027:\\
\;\;\;\;\mathsf{fma}\left(\cos \varepsilon + -1, \cos x, \sin x \cdot \left(-\sin \varepsilon\right)\right)\\

\mathbf{elif}\;x \leq 6.8 \cdot 10^{-52}:\\
\;\;\;\;\sin \left(\varepsilon \cdot 0.5\right) \cdot \left(-2 \cdot \sin \left(\varepsilon \cdot 0.5 + x\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\left(\cos x \cdot \cos \varepsilon - \cos x\right) - \sin x \cdot \sin \varepsilon\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 3: 99.4% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(\varepsilon \cdot 0.5\right)\\ -2 \cdot \left(t_0 \cdot \left(t_0 \cdot \cos x + \cos \left(\varepsilon \cdot 0.5\right) \cdot \sin x\right)\right) \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (sin (* eps 0.5))))
   (* -2.0 (* t_0 (+ (* t_0 (cos x)) (* (cos (* eps 0.5)) (sin x)))))))
double code(double x, double eps) {
	double t_0 = sin((eps * 0.5));
	return -2.0 * (t_0 * ((t_0 * cos(x)) + (cos((eps * 0.5)) * sin(x))));
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    real(8) :: t_0
    t_0 = sin((eps * 0.5d0))
    code = (-2.0d0) * (t_0 * ((t_0 * cos(x)) + (cos((eps * 0.5d0)) * sin(x))))
end function
public static double code(double x, double eps) {
	double t_0 = Math.sin((eps * 0.5));
	return -2.0 * (t_0 * ((t_0 * Math.cos(x)) + (Math.cos((eps * 0.5)) * Math.sin(x))));
}
def code(x, eps):
	t_0 = math.sin((eps * 0.5))
	return -2.0 * (t_0 * ((t_0 * math.cos(x)) + (math.cos((eps * 0.5)) * math.sin(x))))
function code(x, eps)
	t_0 = sin(Float64(eps * 0.5))
	return Float64(-2.0 * Float64(t_0 * Float64(Float64(t_0 * cos(x)) + Float64(cos(Float64(eps * 0.5)) * sin(x)))))
end
function tmp = code(x, eps)
	t_0 = sin((eps * 0.5));
	tmp = -2.0 * (t_0 * ((t_0 * cos(x)) + (cos((eps * 0.5)) * sin(x))));
end
code[x_, eps_] := Block[{t$95$0 = N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]}, N[(-2.0 * N[(t$95$0 * N[(N[(t$95$0 * N[Cos[x], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(\varepsilon \cdot 0.5\right)\\
-2 \cdot \left(t_0 \cdot \left(t_0 \cdot \cos x + \cos \left(\varepsilon \cdot 0.5\right) \cdot \sin x\right)\right)
\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 4: 98.6% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \varepsilon + -1\\ \mathbf{if}\;x \leq -0.027:\\ \;\;\;\;\mathsf{fma}\left(t_0, \cos x, \sin x \cdot \left(-\sin \varepsilon\right)\right)\\ \mathbf{elif}\;x \leq 6.8 \cdot 10^{-52}:\\ \;\;\;\;\sin \left(\varepsilon \cdot 0.5\right) \cdot \left(-2 \cdot \sin \left(\varepsilon \cdot 0.5 + x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\cos x \cdot t_0 - \sin x \cdot \sin \varepsilon\\ \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (+ (cos eps) -1.0)))
   (if (<= x -0.027)
     (fma t_0 (cos x) (* (sin x) (- (sin eps))))
     (if (<= x 6.8e-52)
       (* (sin (* eps 0.5)) (* -2.0 (sin (+ (* eps 0.5) x))))
       (- (* (cos x) t_0) (* (sin x) (sin eps)))))))
double code(double x, double eps) {
	double t_0 = cos(eps) + -1.0;
	double tmp;
	if (x <= -0.027) {
		tmp = fma(t_0, cos(x), (sin(x) * -sin(eps)));
	} else if (x <= 6.8e-52) {
		tmp = sin((eps * 0.5)) * (-2.0 * sin(((eps * 0.5) + x)));
	} else {
		tmp = (cos(x) * t_0) - (sin(x) * sin(eps));
	}
	return tmp;
}
function code(x, eps)
	t_0 = Float64(cos(eps) + -1.0)
	tmp = 0.0
	if (x <= -0.027)
		tmp = fma(t_0, cos(x), Float64(sin(x) * Float64(-sin(eps))));
	elseif (x <= 6.8e-52)
		tmp = Float64(sin(Float64(eps * 0.5)) * Float64(-2.0 * sin(Float64(Float64(eps * 0.5) + x))));
	else
		tmp = Float64(Float64(cos(x) * t_0) - Float64(sin(x) * sin(eps)));
	end
	return tmp
end
code[x_, eps_] := Block[{t$95$0 = N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]}, If[LessEqual[x, -0.027], N[(t$95$0 * N[Cos[x], $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * (-N[Sin[eps], $MachinePrecision])), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 6.8e-52], N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[(-2.0 * N[Sin[N[(N[(eps * 0.5), $MachinePrecision] + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[Cos[x], $MachinePrecision] * t$95$0), $MachinePrecision] - N[(N[Sin[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \varepsilon + -1\\
\mathbf{if}\;x \leq -0.027:\\
\;\;\;\;\mathsf{fma}\left(t_0, \cos x, \sin x \cdot \left(-\sin \varepsilon\right)\right)\\

\mathbf{elif}\;x \leq 6.8 \cdot 10^{-52}:\\
\;\;\;\;\sin \left(\varepsilon \cdot 0.5\right) \cdot \left(-2 \cdot \sin \left(\varepsilon \cdot 0.5 + x\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\cos x \cdot t_0 - \sin x \cdot \sin \varepsilon\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 5: 98.6% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -0.027 \lor \neg \left(x \leq 5.6 \cdot 10^{-52}\right):\\ \;\;\;\;\cos x \cdot \left(\cos \varepsilon + -1\right) - \sin x \cdot \sin \varepsilon\\ \mathbf{else}:\\ \;\;\;\;\sin \left(\varepsilon \cdot 0.5\right) \cdot \left(-2 \cdot \sin \left(\varepsilon \cdot 0.5 + x\right)\right)\\ \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (if (or (<= x -0.027) (not (<= x 5.6e-52)))
   (- (* (cos x) (+ (cos eps) -1.0)) (* (sin x) (sin eps)))
   (* (sin (* eps 0.5)) (* -2.0 (sin (+ (* eps 0.5) x))))))
double code(double x, double eps) {
	double tmp;
	if ((x <= -0.027) || !(x <= 5.6e-52)) {
		tmp = (cos(x) * (cos(eps) + -1.0)) - (sin(x) * sin(eps));
	} else {
		tmp = sin((eps * 0.5)) * (-2.0 * sin(((eps * 0.5) + x)));
	}
	return tmp;
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    real(8) :: tmp
    if ((x <= (-0.027d0)) .or. (.not. (x <= 5.6d-52))) then
        tmp = (cos(x) * (cos(eps) + (-1.0d0))) - (sin(x) * sin(eps))
    else
        tmp = sin((eps * 0.5d0)) * ((-2.0d0) * sin(((eps * 0.5d0) + x)))
    end if
    code = tmp
end function
public static double code(double x, double eps) {
	double tmp;
	if ((x <= -0.027) || !(x <= 5.6e-52)) {
		tmp = (Math.cos(x) * (Math.cos(eps) + -1.0)) - (Math.sin(x) * Math.sin(eps));
	} else {
		tmp = Math.sin((eps * 0.5)) * (-2.0 * Math.sin(((eps * 0.5) + x)));
	}
	return tmp;
}
def code(x, eps):
	tmp = 0
	if (x <= -0.027) or not (x <= 5.6e-52):
		tmp = (math.cos(x) * (math.cos(eps) + -1.0)) - (math.sin(x) * math.sin(eps))
	else:
		tmp = math.sin((eps * 0.5)) * (-2.0 * math.sin(((eps * 0.5) + x)))
	return tmp
function code(x, eps)
	tmp = 0.0
	if ((x <= -0.027) || !(x <= 5.6e-52))
		tmp = Float64(Float64(cos(x) * Float64(cos(eps) + -1.0)) - Float64(sin(x) * sin(eps)));
	else
		tmp = Float64(sin(Float64(eps * 0.5)) * Float64(-2.0 * sin(Float64(Float64(eps * 0.5) + x))));
	end
	return tmp
end
function tmp_2 = code(x, eps)
	tmp = 0.0;
	if ((x <= -0.027) || ~((x <= 5.6e-52)))
		tmp = (cos(x) * (cos(eps) + -1.0)) - (sin(x) * sin(eps));
	else
		tmp = sin((eps * 0.5)) * (-2.0 * sin(((eps * 0.5) + x)));
	end
	tmp_2 = tmp;
end
code[x_, eps_] := If[Or[LessEqual[x, -0.027], N[Not[LessEqual[x, 5.6e-52]], $MachinePrecision]], N[(N[(N[Cos[x], $MachinePrecision] * N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision] - N[(N[Sin[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[(-2.0 * N[Sin[N[(N[(eps * 0.5), $MachinePrecision] + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.027 \lor \neg \left(x \leq 5.6 \cdot 10^{-52}\right):\\
\;\;\;\;\cos x \cdot \left(\cos \varepsilon + -1\right) - \sin x \cdot \sin \varepsilon\\

\mathbf{else}:\\
\;\;\;\;\sin \left(\varepsilon \cdot 0.5\right) \cdot \left(-2 \cdot \sin \left(\varepsilon \cdot 0.5 + x\right)\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 6: 73.8% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\varepsilon + x\right) - \cos x\\ \mathbf{if}\;t_0 \leq -2 \cdot 10^{-5}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-0.5 \cdot {\varepsilon}^{2} - \varepsilon \cdot \sin x\\ \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (- (cos (+ eps x)) (cos x))))
   (if (<= t_0 -2e-5) t_0 (- (* -0.5 (pow eps 2.0)) (* eps (sin x))))))
double code(double x, double eps) {
	double t_0 = cos((eps + x)) - cos(x);
	double tmp;
	if (t_0 <= -2e-5) {
		tmp = t_0;
	} else {
		tmp = (-0.5 * pow(eps, 2.0)) - (eps * sin(x));
	}
	return tmp;
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    real(8) :: t_0
    real(8) :: tmp
    t_0 = cos((eps + x)) - cos(x)
    if (t_0 <= (-2d-5)) then
        tmp = t_0
    else
        tmp = ((-0.5d0) * (eps ** 2.0d0)) - (eps * sin(x))
    end if
    code = tmp
end function
public static double code(double x, double eps) {
	double t_0 = Math.cos((eps + x)) - Math.cos(x);
	double tmp;
	if (t_0 <= -2e-5) {
		tmp = t_0;
	} else {
		tmp = (-0.5 * Math.pow(eps, 2.0)) - (eps * Math.sin(x));
	}
	return tmp;
}
def code(x, eps):
	t_0 = math.cos((eps + x)) - math.cos(x)
	tmp = 0
	if t_0 <= -2e-5:
		tmp = t_0
	else:
		tmp = (-0.5 * math.pow(eps, 2.0)) - (eps * math.sin(x))
	return tmp
function code(x, eps)
	t_0 = Float64(cos(Float64(eps + x)) - cos(x))
	tmp = 0.0
	if (t_0 <= -2e-5)
		tmp = t_0;
	else
		tmp = Float64(Float64(-0.5 * (eps ^ 2.0)) - Float64(eps * sin(x)));
	end
	return tmp
end
function tmp_2 = code(x, eps)
	t_0 = cos((eps + x)) - cos(x);
	tmp = 0.0;
	if (t_0 <= -2e-5)
		tmp = t_0;
	else
		tmp = (-0.5 * (eps ^ 2.0)) - (eps * sin(x));
	end
	tmp_2 = tmp;
end
code[x_, eps_] := Block[{t$95$0 = N[(N[Cos[N[(eps + x), $MachinePrecision]], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -2e-5], t$95$0, N[(N[(-0.5 * N[Power[eps, 2.0], $MachinePrecision]), $MachinePrecision] - N[(eps * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\varepsilon + x\right) - \cos x\\
\mathbf{if}\;t_0 \leq -2 \cdot 10^{-5}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;-0.5 \cdot {\varepsilon}^{2} - \varepsilon \cdot \sin x\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 7: 65.4% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\varepsilon + x\right) - \cos x\\ \mathbf{if}\;t_0 \leq -4 \cdot 10^{-14}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-\varepsilon \cdot \sin x\\ \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (- (cos (+ eps x)) (cos x))))
   (if (<= t_0 -4e-14) t_0 (- (* eps (sin x))))))
double code(double x, double eps) {
	double t_0 = cos((eps + x)) - cos(x);
	double tmp;
	if (t_0 <= -4e-14) {
		tmp = t_0;
	} else {
		tmp = -(eps * sin(x));
	}
	return tmp;
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    real(8) :: t_0
    real(8) :: tmp
    t_0 = cos((eps + x)) - cos(x)
    if (t_0 <= (-4d-14)) then
        tmp = t_0
    else
        tmp = -(eps * sin(x))
    end if
    code = tmp
end function
public static double code(double x, double eps) {
	double t_0 = Math.cos((eps + x)) - Math.cos(x);
	double tmp;
	if (t_0 <= -4e-14) {
		tmp = t_0;
	} else {
		tmp = -(eps * Math.sin(x));
	}
	return tmp;
}
def code(x, eps):
	t_0 = math.cos((eps + x)) - math.cos(x)
	tmp = 0
	if t_0 <= -4e-14:
		tmp = t_0
	else:
		tmp = -(eps * math.sin(x))
	return tmp
function code(x, eps)
	t_0 = Float64(cos(Float64(eps + x)) - cos(x))
	tmp = 0.0
	if (t_0 <= -4e-14)
		tmp = t_0;
	else
		tmp = Float64(-Float64(eps * sin(x)));
	end
	return tmp
end
function tmp_2 = code(x, eps)
	t_0 = cos((eps + x)) - cos(x);
	tmp = 0.0;
	if (t_0 <= -4e-14)
		tmp = t_0;
	else
		tmp = -(eps * sin(x));
	end
	tmp_2 = tmp;
end
code[x_, eps_] := Block[{t$95$0 = N[(N[Cos[N[(eps + x), $MachinePrecision]], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -4e-14], t$95$0, (-N[(eps * N[Sin[x], $MachinePrecision]), $MachinePrecision])]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\varepsilon + x\right) - \cos x\\
\mathbf{if}\;t_0 \leq -4 \cdot 10^{-14}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;-\varepsilon \cdot \sin x\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 8: 66.7% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\varepsilon \leq -6.6 \cdot 10^{-7}:\\ \;\;\;\;\cos \varepsilon - \cos x\\ \mathbf{elif}\;\varepsilon \leq 2.4 \cdot 10^{-41}:\\ \;\;\;\;-\varepsilon \cdot \sin x\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot {\sin \left(\varepsilon \cdot 0.5\right)}^{2}\\ \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (if (<= eps -6.6e-7)
   (- (cos eps) (cos x))
   (if (<= eps 2.4e-41)
     (- (* eps (sin x)))
     (* -2.0 (pow (sin (* eps 0.5)) 2.0)))))
double code(double x, double eps) {
	double tmp;
	if (eps <= -6.6e-7) {
		tmp = cos(eps) - cos(x);
	} else if (eps <= 2.4e-41) {
		tmp = -(eps * sin(x));
	} else {
		tmp = -2.0 * pow(sin((eps * 0.5)), 2.0);
	}
	return tmp;
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    real(8) :: tmp
    if (eps <= (-6.6d-7)) then
        tmp = cos(eps) - cos(x)
    else if (eps <= 2.4d-41) then
        tmp = -(eps * sin(x))
    else
        tmp = (-2.0d0) * (sin((eps * 0.5d0)) ** 2.0d0)
    end if
    code = tmp
end function
public static double code(double x, double eps) {
	double tmp;
	if (eps <= -6.6e-7) {
		tmp = Math.cos(eps) - Math.cos(x);
	} else if (eps <= 2.4e-41) {
		tmp = -(eps * Math.sin(x));
	} else {
		tmp = -2.0 * Math.pow(Math.sin((eps * 0.5)), 2.0);
	}
	return tmp;
}
def code(x, eps):
	tmp = 0
	if eps <= -6.6e-7:
		tmp = math.cos(eps) - math.cos(x)
	elif eps <= 2.4e-41:
		tmp = -(eps * math.sin(x))
	else:
		tmp = -2.0 * math.pow(math.sin((eps * 0.5)), 2.0)
	return tmp
function code(x, eps)
	tmp = 0.0
	if (eps <= -6.6e-7)
		tmp = Float64(cos(eps) - cos(x));
	elseif (eps <= 2.4e-41)
		tmp = Float64(-Float64(eps * sin(x)));
	else
		tmp = Float64(-2.0 * (sin(Float64(eps * 0.5)) ^ 2.0));
	end
	return tmp
end
function tmp_2 = code(x, eps)
	tmp = 0.0;
	if (eps <= -6.6e-7)
		tmp = cos(eps) - cos(x);
	elseif (eps <= 2.4e-41)
		tmp = -(eps * sin(x));
	else
		tmp = -2.0 * (sin((eps * 0.5)) ^ 2.0);
	end
	tmp_2 = tmp;
end
code[x_, eps_] := If[LessEqual[eps, -6.6e-7], N[(N[Cos[eps], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision], If[LessEqual[eps, 2.4e-41], (-N[(eps * N[Sin[x], $MachinePrecision]), $MachinePrecision]), N[(-2.0 * N[Power[N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -6.6 \cdot 10^{-7}:\\
\;\;\;\;\cos \varepsilon - \cos x\\

\mathbf{elif}\;\varepsilon \leq 2.4 \cdot 10^{-41}:\\
\;\;\;\;-\varepsilon \cdot \sin x\\

\mathbf{else}:\\
\;\;\;\;-2 \cdot {\sin \left(\varepsilon \cdot 0.5\right)}^{2}\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 9: 67.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\varepsilon \leq -0.000112 \lor \neg \left(\varepsilon \leq 2.2 \cdot 10^{-7}\right):\\ \;\;\;\;\cos \varepsilon - \cos x\\ \mathbf{else}:\\ \;\;\;\;-\varepsilon \cdot \sin x\\ \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (if (or (<= eps -0.000112) (not (<= eps 2.2e-7)))
   (- (cos eps) (cos x))
   (- (* eps (sin x)))))
double code(double x, double eps) {
	double tmp;
	if ((eps <= -0.000112) || !(eps <= 2.2e-7)) {
		tmp = cos(eps) - cos(x);
	} else {
		tmp = -(eps * sin(x));
	}
	return tmp;
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    real(8) :: tmp
    if ((eps <= (-0.000112d0)) .or. (.not. (eps <= 2.2d-7))) then
        tmp = cos(eps) - cos(x)
    else
        tmp = -(eps * sin(x))
    end if
    code = tmp
end function
public static double code(double x, double eps) {
	double tmp;
	if ((eps <= -0.000112) || !(eps <= 2.2e-7)) {
		tmp = Math.cos(eps) - Math.cos(x);
	} else {
		tmp = -(eps * Math.sin(x));
	}
	return tmp;
}
def code(x, eps):
	tmp = 0
	if (eps <= -0.000112) or not (eps <= 2.2e-7):
		tmp = math.cos(eps) - math.cos(x)
	else:
		tmp = -(eps * math.sin(x))
	return tmp
function code(x, eps)
	tmp = 0.0
	if ((eps <= -0.000112) || !(eps <= 2.2e-7))
		tmp = Float64(cos(eps) - cos(x));
	else
		tmp = Float64(-Float64(eps * sin(x)));
	end
	return tmp
end
function tmp_2 = code(x, eps)
	tmp = 0.0;
	if ((eps <= -0.000112) || ~((eps <= 2.2e-7)))
		tmp = cos(eps) - cos(x);
	else
		tmp = -(eps * sin(x));
	end
	tmp_2 = tmp;
end
code[x_, eps_] := If[Or[LessEqual[eps, -0.000112], N[Not[LessEqual[eps, 2.2e-7]], $MachinePrecision]], N[(N[Cos[eps], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision], (-N[(eps * N[Sin[x], $MachinePrecision]), $MachinePrecision])]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.000112 \lor \neg \left(\varepsilon \leq 2.2 \cdot 10^{-7}\right):\\
\;\;\;\;\cos \varepsilon - \cos x\\

\mathbf{else}:\\
\;\;\;\;-\varepsilon \cdot \sin x\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 10: 75.5% accurate, 1.0× speedup?

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

\\
\sin \left(\varepsilon \cdot 0.5\right) \cdot \left(-2 \cdot \sin \left(\varepsilon \cdot 0.5 + x\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 11: 66.4% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\varepsilon \leq -3.7 \cdot 10^{-7} \lor \neg \left(\varepsilon \leq 2.2 \cdot 10^{-7}\right):\\ \;\;\;\;\cos \varepsilon + -1\\ \mathbf{else}:\\ \;\;\;\;-\varepsilon \cdot \sin x\\ \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (if (or (<= eps -3.7e-7) (not (<= eps 2.2e-7)))
   (+ (cos eps) -1.0)
   (- (* eps (sin x)))))
double code(double x, double eps) {
	double tmp;
	if ((eps <= -3.7e-7) || !(eps <= 2.2e-7)) {
		tmp = cos(eps) + -1.0;
	} else {
		tmp = -(eps * sin(x));
	}
	return tmp;
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    real(8) :: tmp
    if ((eps <= (-3.7d-7)) .or. (.not. (eps <= 2.2d-7))) then
        tmp = cos(eps) + (-1.0d0)
    else
        tmp = -(eps * sin(x))
    end if
    code = tmp
end function
public static double code(double x, double eps) {
	double tmp;
	if ((eps <= -3.7e-7) || !(eps <= 2.2e-7)) {
		tmp = Math.cos(eps) + -1.0;
	} else {
		tmp = -(eps * Math.sin(x));
	}
	return tmp;
}
def code(x, eps):
	tmp = 0
	if (eps <= -3.7e-7) or not (eps <= 2.2e-7):
		tmp = math.cos(eps) + -1.0
	else:
		tmp = -(eps * math.sin(x))
	return tmp
function code(x, eps)
	tmp = 0.0
	if ((eps <= -3.7e-7) || !(eps <= 2.2e-7))
		tmp = Float64(cos(eps) + -1.0);
	else
		tmp = Float64(-Float64(eps * sin(x)));
	end
	return tmp
end
function tmp_2 = code(x, eps)
	tmp = 0.0;
	if ((eps <= -3.7e-7) || ~((eps <= 2.2e-7)))
		tmp = cos(eps) + -1.0;
	else
		tmp = -(eps * sin(x));
	end
	tmp_2 = tmp;
end
code[x_, eps_] := If[Or[LessEqual[eps, -3.7e-7], N[Not[LessEqual[eps, 2.2e-7]], $MachinePrecision]], N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision], (-N[(eps * N[Sin[x], $MachinePrecision]), $MachinePrecision])]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -3.7 \cdot 10^{-7} \lor \neg \left(\varepsilon \leq 2.2 \cdot 10^{-7}\right):\\
\;\;\;\;\cos \varepsilon + -1\\

\mathbf{else}:\\
\;\;\;\;-\varepsilon \cdot \sin x\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 12: 43.1% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\varepsilon \leq -1.75 \cdot 10^{-9} \lor \neg \left(\varepsilon \leq 2.5 \cdot 10^{-35}\right):\\ \;\;\;\;\cos \varepsilon + -1\\ \mathbf{else}:\\ \;\;\;\;\varepsilon \cdot \left(-x\right)\\ \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (if (or (<= eps -1.75e-9) (not (<= eps 2.5e-35)))
   (+ (cos eps) -1.0)
   (* eps (- x))))
double code(double x, double eps) {
	double tmp;
	if ((eps <= -1.75e-9) || !(eps <= 2.5e-35)) {
		tmp = cos(eps) + -1.0;
	} else {
		tmp = eps * -x;
	}
	return tmp;
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    real(8) :: tmp
    if ((eps <= (-1.75d-9)) .or. (.not. (eps <= 2.5d-35))) then
        tmp = cos(eps) + (-1.0d0)
    else
        tmp = eps * -x
    end if
    code = tmp
end function
public static double code(double x, double eps) {
	double tmp;
	if ((eps <= -1.75e-9) || !(eps <= 2.5e-35)) {
		tmp = Math.cos(eps) + -1.0;
	} else {
		tmp = eps * -x;
	}
	return tmp;
}
def code(x, eps):
	tmp = 0
	if (eps <= -1.75e-9) or not (eps <= 2.5e-35):
		tmp = math.cos(eps) + -1.0
	else:
		tmp = eps * -x
	return tmp
function code(x, eps)
	tmp = 0.0
	if ((eps <= -1.75e-9) || !(eps <= 2.5e-35))
		tmp = Float64(cos(eps) + -1.0);
	else
		tmp = Float64(eps * Float64(-x));
	end
	return tmp
end
function tmp_2 = code(x, eps)
	tmp = 0.0;
	if ((eps <= -1.75e-9) || ~((eps <= 2.5e-35)))
		tmp = cos(eps) + -1.0;
	else
		tmp = eps * -x;
	end
	tmp_2 = tmp;
end
code[x_, eps_] := If[Or[LessEqual[eps, -1.75e-9], N[Not[LessEqual[eps, 2.5e-35]], $MachinePrecision]], N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision], N[(eps * (-x)), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -1.75 \cdot 10^{-9} \lor \neg \left(\varepsilon \leq 2.5 \cdot 10^{-35}\right):\\
\;\;\;\;\cos \varepsilon + -1\\

\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \left(-x\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 13: 17.8% accurate, 51.3× speedup?

\[\begin{array}{l} \\ \varepsilon \cdot \left(-x\right) \end{array} \]
(FPCore (x eps) :precision binary64 (* eps (- x)))
double code(double x, double eps) {
	return eps * -x;
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    code = eps * -x
end function
public static double code(double x, double eps) {
	return eps * -x;
}
def code(x, eps):
	return eps * -x
function code(x, eps)
	return Float64(eps * Float64(-x))
end
function tmp = code(x, eps)
	tmp = eps * -x;
end
code[x_, eps_] := N[(eps * (-x)), $MachinePrecision]
\begin{array}{l}

\\
\varepsilon \cdot \left(-x\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Reproduce

?
herbie shell --seed 2024006 
(FPCore (x eps)
  :name "2cos (problem 3.3.5)"
  :precision binary64
  (- (cos (+ x eps)) (cos x)))