
(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:
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(FPCore (x eps) :precision binary64 (if (or (<= x -0.027) (not (<= x 6.8e-52))) (- (* (cos x) (+ (cos eps) -1.0)) (* (sin x) (sin eps))) (* (sin (* 0.5 (fma 2.0 x eps))) (* -2.0 (sin (* eps 0.5))))))
double code(double x, double eps) {
double tmp;
if ((x <= -0.027) || !(x <= 6.8e-52)) {
tmp = (cos(x) * (cos(eps) + -1.0)) - (sin(x) * sin(eps));
} else {
tmp = sin((0.5 * fma(2.0, x, eps))) * (-2.0 * sin((eps * 0.5)));
}
return tmp;
}
function code(x, eps) tmp = 0.0 if ((x <= -0.027) || !(x <= 6.8e-52)) tmp = Float64(Float64(cos(x) * Float64(cos(eps) + -1.0)) - Float64(sin(x) * sin(eps))); else tmp = Float64(sin(Float64(0.5 * fma(2.0, x, eps))) * Float64(-2.0 * sin(Float64(eps * 0.5)))); end return tmp end
code[x_, eps_] := If[Or[LessEqual[x, -0.027], N[Not[LessEqual[x, 6.8e-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[(0.5 * N[(2.0 * x + eps), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[(-2.0 * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.027 \lor \neg \left(x \leq 6.8 \cdot 10^{-52}\right):\\
\;\;\;\;\cos x \cdot \left(\cos \varepsilon + -1\right) - \sin x \cdot \sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\sin \left(0.5 \cdot \mathsf{fma}\left(2, x, \varepsilon\right)\right) \cdot \left(-2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)\\
\end{array}
\end{array}
(FPCore (x eps) :precision binary64 (let* ((t_0 (- (cos (+ x eps)) (cos x)))) (if (<= t_0 -4e-14) t_0 (* (sin x) (* -2.0 (sin (* eps 0.5)))))))
double code(double x, double eps) {
double t_0 = cos((x + eps)) - cos(x);
double tmp;
if (t_0 <= -4e-14) {
tmp = t_0;
} else {
tmp = sin(x) * (-2.0 * sin((eps * 0.5)));
}
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((x + eps)) - cos(x)
if (t_0 <= (-4d-14)) then
tmp = t_0
else
tmp = sin(x) * ((-2.0d0) * sin((eps * 0.5d0)))
end if
code = tmp
end function
public static double code(double x, double eps) {
double t_0 = Math.cos((x + eps)) - Math.cos(x);
double tmp;
if (t_0 <= -4e-14) {
tmp = t_0;
} else {
tmp = Math.sin(x) * (-2.0 * Math.sin((eps * 0.5)));
}
return tmp;
}
def code(x, eps): t_0 = math.cos((x + eps)) - math.cos(x) tmp = 0 if t_0 <= -4e-14: tmp = t_0 else: tmp = math.sin(x) * (-2.0 * math.sin((eps * 0.5))) return tmp
function code(x, eps) t_0 = Float64(cos(Float64(x + eps)) - cos(x)) tmp = 0.0 if (t_0 <= -4e-14) tmp = t_0; else tmp = Float64(sin(x) * Float64(-2.0 * sin(Float64(eps * 0.5)))); end return tmp end
function tmp_2 = code(x, eps) t_0 = cos((x + eps)) - cos(x); tmp = 0.0; if (t_0 <= -4e-14) tmp = t_0; else tmp = sin(x) * (-2.0 * sin((eps * 0.5))); end tmp_2 = tmp; end
code[x_, eps_] := Block[{t$95$0 = N[(N[Cos[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -4e-14], t$95$0, N[(N[Sin[x], $MachinePrecision] * N[(-2.0 * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(x + \varepsilon\right) - \cos x\\
\mathbf{if}\;t_0 \leq -4 \cdot 10^{-14}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\sin x \cdot \left(-2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)\\
\end{array}
\end{array}
(FPCore (x eps) :precision binary64 (let* ((t_0 (- (cos (+ x eps)) (cos x)))) (if (<= t_0 -4e-14) t_0 (* (sin x) (- eps)))))
double code(double x, double eps) {
double t_0 = cos((x + eps)) - cos(x);
double tmp;
if (t_0 <= -4e-14) {
tmp = t_0;
} else {
tmp = sin(x) * -eps;
}
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((x + eps)) - cos(x)
if (t_0 <= (-4d-14)) then
tmp = t_0
else
tmp = sin(x) * -eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double t_0 = Math.cos((x + eps)) - Math.cos(x);
double tmp;
if (t_0 <= -4e-14) {
tmp = t_0;
} else {
tmp = Math.sin(x) * -eps;
}
return tmp;
}
def code(x, eps): t_0 = math.cos((x + eps)) - math.cos(x) tmp = 0 if t_0 <= -4e-14: tmp = t_0 else: tmp = math.sin(x) * -eps return tmp
function code(x, eps) t_0 = Float64(cos(Float64(x + eps)) - cos(x)) tmp = 0.0 if (t_0 <= -4e-14) tmp = t_0; else tmp = Float64(sin(x) * Float64(-eps)); end return tmp end
function tmp_2 = code(x, eps) t_0 = cos((x + eps)) - cos(x); tmp = 0.0; if (t_0 <= -4e-14) tmp = t_0; else tmp = sin(x) * -eps; end tmp_2 = tmp; end
code[x_, eps_] := Block[{t$95$0 = N[(N[Cos[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -4e-14], t$95$0, N[(N[Sin[x], $MachinePrecision] * (-eps)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(x + \varepsilon\right) - \cos x\\
\mathbf{if}\;t_0 \leq -4 \cdot 10^{-14}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\sin x \cdot \left(-\varepsilon\right)\\
\end{array}
\end{array}
(FPCore (x eps) :precision binary64 (* (sin (* 0.5 (fma 2.0 x eps))) (* -2.0 (sin (* eps 0.5)))))
double code(double x, double eps) {
return sin((0.5 * fma(2.0, x, eps))) * (-2.0 * sin((eps * 0.5)));
}
function code(x, eps) return Float64(sin(Float64(0.5 * fma(2.0, x, eps))) * Float64(-2.0 * sin(Float64(eps * 0.5)))) end
code[x_, eps_] := N[(N[Sin[N[(0.5 * N[(2.0 * x + eps), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[(-2.0 * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \left(0.5 \cdot \mathsf{fma}\left(2, x, \varepsilon\right)\right) \cdot \left(-2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)
\end{array}
(FPCore (x eps) :precision binary64 (* -2.0 (* (sin (* eps 0.5)) (sin (* 0.5 (- eps (* x -2.0)))))))
double code(double x, double eps) {
return -2.0 * (sin((eps * 0.5)) * sin((0.5 * (eps - (x * -2.0)))));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (-2.0d0) * (sin((eps * 0.5d0)) * sin((0.5d0 * (eps - (x * (-2.0d0))))))
end function
public static double code(double x, double eps) {
return -2.0 * (Math.sin((eps * 0.5)) * Math.sin((0.5 * (eps - (x * -2.0)))));
}
def code(x, eps): return -2.0 * (math.sin((eps * 0.5)) * math.sin((0.5 * (eps - (x * -2.0)))))
function code(x, eps) return Float64(-2.0 * Float64(sin(Float64(eps * 0.5)) * sin(Float64(0.5 * Float64(eps - Float64(x * -2.0)))))) end
function tmp = code(x, eps) tmp = -2.0 * (sin((eps * 0.5)) * sin((0.5 * (eps - (x * -2.0))))); end
code[x_, eps_] := N[(-2.0 * N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(0.5 * N[(eps - N[(x * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-2 \cdot \left(\sin \left(\varepsilon \cdot 0.5\right) \cdot \sin \left(0.5 \cdot \left(\varepsilon - x \cdot -2\right)\right)\right)
\end{array}
(FPCore (x eps)
:precision binary64
(if (<= eps -9.2e-6)
(- (cos eps) (cos x))
(if (<= eps 1.05e-34)
(* (sin x) (- eps))
(* -2.0 (pow (sin (* eps 0.5)) 2.0)))))
double code(double x, double eps) {
double tmp;
if (eps <= -9.2e-6) {
tmp = cos(eps) - cos(x);
} else if (eps <= 1.05e-34) {
tmp = sin(x) * -eps;
} 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 <= (-9.2d-6)) then
tmp = cos(eps) - cos(x)
else if (eps <= 1.05d-34) then
tmp = sin(x) * -eps
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 <= -9.2e-6) {
tmp = Math.cos(eps) - Math.cos(x);
} else if (eps <= 1.05e-34) {
tmp = Math.sin(x) * -eps;
} else {
tmp = -2.0 * Math.pow(Math.sin((eps * 0.5)), 2.0);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -9.2e-6: tmp = math.cos(eps) - math.cos(x) elif eps <= 1.05e-34: tmp = math.sin(x) * -eps else: tmp = -2.0 * math.pow(math.sin((eps * 0.5)), 2.0) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -9.2e-6) tmp = Float64(cos(eps) - cos(x)); elseif (eps <= 1.05e-34) tmp = Float64(sin(x) * Float64(-eps)); 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 <= -9.2e-6) tmp = cos(eps) - cos(x); elseif (eps <= 1.05e-34) tmp = sin(x) * -eps; else tmp = -2.0 * (sin((eps * 0.5)) ^ 2.0); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -9.2e-6], N[(N[Cos[eps], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision], If[LessEqual[eps, 1.05e-34], N[(N[Sin[x], $MachinePrecision] * (-eps)), $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 -9.2 \cdot 10^{-6}:\\
\;\;\;\;\cos \varepsilon - \cos x\\
\mathbf{elif}\;\varepsilon \leq 1.05 \cdot 10^{-34}:\\
\;\;\;\;\sin x \cdot \left(-\varepsilon\right)\\
\mathbf{else}:\\
\;\;\;\;-2 \cdot {\sin \left(\varepsilon \cdot 0.5\right)}^{2}\\
\end{array}
\end{array}
(FPCore (x eps) :precision binary64 (if (or (<= eps -7.5e-7) (not (<= eps 1.05e-7))) (- (cos eps) (cos x)) (* (sin x) (- eps))))
double code(double x, double eps) {
double tmp;
if ((eps <= -7.5e-7) || !(eps <= 1.05e-7)) {
tmp = cos(eps) - cos(x);
} else {
tmp = sin(x) * -eps;
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if ((eps <= (-7.5d-7)) .or. (.not. (eps <= 1.05d-7))) then
tmp = cos(eps) - cos(x)
else
tmp = sin(x) * -eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((eps <= -7.5e-7) || !(eps <= 1.05e-7)) {
tmp = Math.cos(eps) - Math.cos(x);
} else {
tmp = Math.sin(x) * -eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -7.5e-7) or not (eps <= 1.05e-7): tmp = math.cos(eps) - math.cos(x) else: tmp = math.sin(x) * -eps return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -7.5e-7) || !(eps <= 1.05e-7)) tmp = Float64(cos(eps) - cos(x)); else tmp = Float64(sin(x) * Float64(-eps)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -7.5e-7) || ~((eps <= 1.05e-7))) tmp = cos(eps) - cos(x); else tmp = sin(x) * -eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -7.5e-7], N[Not[LessEqual[eps, 1.05e-7]], $MachinePrecision]], N[(N[Cos[eps], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision], N[(N[Sin[x], $MachinePrecision] * (-eps)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -7.5 \cdot 10^{-7} \lor \neg \left(\varepsilon \leq 1.05 \cdot 10^{-7}\right):\\
\;\;\;\;\cos \varepsilon - \cos x\\
\mathbf{else}:\\
\;\;\;\;\sin x \cdot \left(-\varepsilon\right)\\
\end{array}
\end{array}
(FPCore (x eps) :precision binary64 (if (or (<= eps -3.5e-5) (not (<= eps 1.85e-7))) (+ (cos eps) -1.0) (* (sin x) (- eps))))
double code(double x, double eps) {
double tmp;
if ((eps <= -3.5e-5) || !(eps <= 1.85e-7)) {
tmp = cos(eps) + -1.0;
} else {
tmp = sin(x) * -eps;
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if ((eps <= (-3.5d-5)) .or. (.not. (eps <= 1.85d-7))) then
tmp = cos(eps) + (-1.0d0)
else
tmp = sin(x) * -eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((eps <= -3.5e-5) || !(eps <= 1.85e-7)) {
tmp = Math.cos(eps) + -1.0;
} else {
tmp = Math.sin(x) * -eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -3.5e-5) or not (eps <= 1.85e-7): tmp = math.cos(eps) + -1.0 else: tmp = math.sin(x) * -eps return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -3.5e-5) || !(eps <= 1.85e-7)) tmp = Float64(cos(eps) + -1.0); else tmp = Float64(sin(x) * Float64(-eps)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -3.5e-5) || ~((eps <= 1.85e-7))) tmp = cos(eps) + -1.0; else tmp = sin(x) * -eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -3.5e-5], N[Not[LessEqual[eps, 1.85e-7]], $MachinePrecision]], N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision], N[(N[Sin[x], $MachinePrecision] * (-eps)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -3.5 \cdot 10^{-5} \lor \neg \left(\varepsilon \leq 1.85 \cdot 10^{-7}\right):\\
\;\;\;\;\cos \varepsilon + -1\\
\mathbf{else}:\\
\;\;\;\;\sin x \cdot \left(-\varepsilon\right)\\
\end{array}
\end{array}
(FPCore (x eps) :precision binary64 (if (or (<= eps -4.6e-9) (not (<= eps 2.2e-36))) (+ (cos eps) -1.0) (- (* x eps))))
double code(double x, double eps) {
double tmp;
if ((eps <= -4.6e-9) || !(eps <= 2.2e-36)) {
tmp = cos(eps) + -1.0;
} else {
tmp = -(x * eps);
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if ((eps <= (-4.6d-9)) .or. (.not. (eps <= 2.2d-36))) then
tmp = cos(eps) + (-1.0d0)
else
tmp = -(x * eps)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((eps <= -4.6e-9) || !(eps <= 2.2e-36)) {
tmp = Math.cos(eps) + -1.0;
} else {
tmp = -(x * eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -4.6e-9) or not (eps <= 2.2e-36): tmp = math.cos(eps) + -1.0 else: tmp = -(x * eps) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -4.6e-9) || !(eps <= 2.2e-36)) tmp = Float64(cos(eps) + -1.0); else tmp = Float64(-Float64(x * eps)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -4.6e-9) || ~((eps <= 2.2e-36))) tmp = cos(eps) + -1.0; else tmp = -(x * eps); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -4.6e-9], N[Not[LessEqual[eps, 2.2e-36]], $MachinePrecision]], N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision], (-N[(x * eps), $MachinePrecision])]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -4.6 \cdot 10^{-9} \lor \neg \left(\varepsilon \leq 2.2 \cdot 10^{-36}\right):\\
\;\;\;\;\cos \varepsilon + -1\\
\mathbf{else}:\\
\;\;\;\;-x \cdot \varepsilon\\
\end{array}
\end{array}
(FPCore (x eps) :precision binary64 (- (* x eps)))
double code(double x, double eps) {
return -(x * eps);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = -(x * eps)
end function
public static double code(double x, double eps) {
return -(x * eps);
}
def code(x, eps): return -(x * eps)
function code(x, eps) return Float64(-Float64(x * eps)) end
function tmp = code(x, eps) tmp = -(x * eps); end
code[x_, eps_] := (-N[(x * eps), $MachinePrecision])
\begin{array}{l}
\\
-x \cdot \varepsilon
\end{array}
herbie shell --seed 2024006
(FPCore (x eps)
:name "2cos (problem 3.3.5)"
:precision binary64
(- (cos (+ x eps)) (cos x)))