
(FPCore (x eps) :precision binary64 (- (sin (+ x eps)) (sin x)))
double code(double x, double eps) {
return sin((x + eps)) - sin(x);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin((x + eps)) - sin(x)
end function
public static double code(double x, double eps) {
return Math.sin((x + eps)) - Math.sin(x);
}
def code(x, eps): return math.sin((x + eps)) - math.sin(x)
function code(x, eps) return Float64(sin(Float64(x + eps)) - sin(x)) end
function tmp = code(x, eps) tmp = sin((x + eps)) - sin(x); end
code[x_, eps_] := N[(N[Sin[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \left(x + \varepsilon\right) - \sin x
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x eps) :precision binary64 (- (sin (+ x eps)) (sin x)))
double code(double x, double eps) {
return sin((x + eps)) - sin(x);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin((x + eps)) - sin(x)
end function
public static double code(double x, double eps) {
return Math.sin((x + eps)) - Math.sin(x);
}
def code(x, eps): return math.sin((x + eps)) - math.sin(x)
function code(x, eps) return Float64(sin(Float64(x + eps)) - sin(x)) end
function tmp = code(x, eps) tmp = sin((x + eps)) - sin(x); end
code[x_, eps_] := N[(N[Sin[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \left(x + \varepsilon\right) - \sin x
\end{array}
(FPCore (x eps)
:precision binary64
(let* ((t_0 (* (sin x) (- 1.0 (cos eps)))))
(+
(- (* (cos x) (sin eps)) t_0)
(* 2.0 (fma (+ (cos eps) -1.0) (sin x) t_0)))))
double code(double x, double eps) {
double t_0 = sin(x) * (1.0 - cos(eps));
return ((cos(x) * sin(eps)) - t_0) + (2.0 * fma((cos(eps) + -1.0), sin(x), t_0));
}
function code(x, eps) t_0 = Float64(sin(x) * Float64(1.0 - cos(eps))) return Float64(Float64(Float64(cos(x) * sin(eps)) - t_0) + Float64(2.0 * fma(Float64(cos(eps) + -1.0), sin(x), t_0))) end
code[x_, eps_] := Block[{t$95$0 = N[(N[Sin[x], $MachinePrecision] * N[(1.0 - N[Cos[eps], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision]), $MachinePrecision] - t$95$0), $MachinePrecision] + N[(2.0 * N[(N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision] * N[Sin[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin x \cdot \left(1 - \cos \varepsilon\right)\\
\left(\cos x \cdot \sin \varepsilon - t_0\right) + 2 \cdot \mathsf{fma}\left(\cos \varepsilon + -1, \sin x, t_0\right)
\end{array}
\end{array}
(FPCore (x eps)
:precision binary64
(let* ((t_0 (- (sin (+ x eps)) (sin x))))
(if (or (<= t_0 -2e-8) (not (<= t_0 0.05)))
t_0
(* (cos x) (* 2.0 (sin (* eps 0.5)))))))
double code(double x, double eps) {
double t_0 = sin((x + eps)) - sin(x);
double tmp;
if ((t_0 <= -2e-8) || !(t_0 <= 0.05)) {
tmp = t_0;
} else {
tmp = cos(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 = sin((x + eps)) - sin(x)
if ((t_0 <= (-2d-8)) .or. (.not. (t_0 <= 0.05d0))) then
tmp = t_0
else
tmp = cos(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.sin((x + eps)) - Math.sin(x);
double tmp;
if ((t_0 <= -2e-8) || !(t_0 <= 0.05)) {
tmp = t_0;
} else {
tmp = Math.cos(x) * (2.0 * Math.sin((eps * 0.5)));
}
return tmp;
}
def code(x, eps): t_0 = math.sin((x + eps)) - math.sin(x) tmp = 0 if (t_0 <= -2e-8) or not (t_0 <= 0.05): tmp = t_0 else: tmp = math.cos(x) * (2.0 * math.sin((eps * 0.5))) return tmp
function code(x, eps) t_0 = Float64(sin(Float64(x + eps)) - sin(x)) tmp = 0.0 if ((t_0 <= -2e-8) || !(t_0 <= 0.05)) tmp = t_0; else tmp = Float64(cos(x) * Float64(2.0 * sin(Float64(eps * 0.5)))); end return tmp end
function tmp_2 = code(x, eps) t_0 = sin((x + eps)) - sin(x); tmp = 0.0; if ((t_0 <= -2e-8) || ~((t_0 <= 0.05))) tmp = t_0; else tmp = cos(x) * (2.0 * sin((eps * 0.5))); end tmp_2 = tmp; end
code[x_, eps_] := Block[{t$95$0 = N[(N[Sin[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -2e-8], N[Not[LessEqual[t$95$0, 0.05]], $MachinePrecision]], t$95$0, N[(N[Cos[x], $MachinePrecision] * N[(2.0 * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(x + \varepsilon\right) - \sin x\\
\mathbf{if}\;t_0 \leq -2 \cdot 10^{-8} \lor \neg \left(t_0 \leq 0.05\right):\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\cos x \cdot \left(2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)\\
\end{array}
\end{array}
(FPCore (x eps) :precision binary64 (fma (cos x) (sin eps) (* (sin x) (+ (cos eps) -1.0))))
double code(double x, double eps) {
return fma(cos(x), sin(eps), (sin(x) * (cos(eps) + -1.0)));
}
function code(x, eps) return fma(cos(x), sin(eps), Float64(sin(x) * Float64(cos(eps) + -1.0))) end
code[x_, eps_] := N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\cos x, \sin \varepsilon, \sin x \cdot \left(\cos \varepsilon + -1\right)\right)
\end{array}
(FPCore (x eps) :precision binary64 (- (* (cos x) (sin eps)) (* (sin x) (- 1.0 (cos eps)))))
double code(double x, double eps) {
return (cos(x) * sin(eps)) - (sin(x) * (1.0 - cos(eps)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (cos(x) * sin(eps)) - (sin(x) * (1.0d0 - cos(eps)))
end function
public static double code(double x, double eps) {
return (Math.cos(x) * Math.sin(eps)) - (Math.sin(x) * (1.0 - Math.cos(eps)));
}
def code(x, eps): return (math.cos(x) * math.sin(eps)) - (math.sin(x) * (1.0 - math.cos(eps)))
function code(x, eps) return Float64(Float64(cos(x) * sin(eps)) - Float64(sin(x) * Float64(1.0 - cos(eps)))) end
function tmp = code(x, eps) tmp = (cos(x) * sin(eps)) - (sin(x) * (1.0 - cos(eps))); end
code[x_, eps_] := N[(N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision]), $MachinePrecision] - N[(N[Sin[x], $MachinePrecision] * N[(1.0 - N[Cos[eps], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos x \cdot \sin \varepsilon - \sin x \cdot \left(1 - \cos \varepsilon\right)
\end{array}
(FPCore (x eps) :precision binary64 (* 2.0 (* (cos (* 0.5 (+ eps (* x 2.0)))) (sin (* eps 0.5)))))
double code(double x, double eps) {
return 2.0 * (cos((0.5 * (eps + (x * 2.0)))) * sin((eps * 0.5)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 2.0d0 * (cos((0.5d0 * (eps + (x * 2.0d0)))) * sin((eps * 0.5d0)))
end function
public static double code(double x, double eps) {
return 2.0 * (Math.cos((0.5 * (eps + (x * 2.0)))) * Math.sin((eps * 0.5)));
}
def code(x, eps): return 2.0 * (math.cos((0.5 * (eps + (x * 2.0)))) * math.sin((eps * 0.5)))
function code(x, eps) return Float64(2.0 * Float64(cos(Float64(0.5 * Float64(eps + Float64(x * 2.0)))) * sin(Float64(eps * 0.5)))) end
function tmp = code(x, eps) tmp = 2.0 * (cos((0.5 * (eps + (x * 2.0)))) * sin((eps * 0.5))); end
code[x_, eps_] := N[(2.0 * N[(N[Cos[N[(0.5 * N[(eps + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(\cos \left(0.5 \cdot \left(\varepsilon + x \cdot 2\right)\right) \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)
\end{array}
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.00019) (not (<= eps 8e-12))) (sin eps) (* (cos x) eps)))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.00019) || !(eps <= 8e-12)) {
tmp = sin(eps);
} else {
tmp = cos(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 <= (-0.00019d0)) .or. (.not. (eps <= 8d-12))) then
tmp = sin(eps)
else
tmp = cos(x) * eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((eps <= -0.00019) || !(eps <= 8e-12)) {
tmp = Math.sin(eps);
} else {
tmp = Math.cos(x) * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.00019) or not (eps <= 8e-12): tmp = math.sin(eps) else: tmp = math.cos(x) * eps return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.00019) || !(eps <= 8e-12)) tmp = sin(eps); else tmp = Float64(cos(x) * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -0.00019) || ~((eps <= 8e-12))) tmp = sin(eps); else tmp = cos(x) * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.00019], N[Not[LessEqual[eps, 8e-12]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.00019 \lor \neg \left(\varepsilon \leq 8 \cdot 10^{-12}\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\end{array}
\end{array}
(FPCore (x eps) :precision binary64 (sin eps))
double code(double x, double eps) {
return sin(eps);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin(eps)
end function
public static double code(double x, double eps) {
return Math.sin(eps);
}
def code(x, eps): return math.sin(eps)
function code(x, eps) return sin(eps) end
function tmp = code(x, eps) tmp = sin(eps); end
code[x_, eps_] := N[Sin[eps], $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon
\end{array}
(FPCore (x eps) :precision binary64 eps)
double code(double x, double eps) {
return eps;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps
end function
public static double code(double x, double eps) {
return eps;
}
def code(x, eps): return eps
function code(x, eps) return eps end
function tmp = code(x, eps) tmp = eps; end
code[x_, eps_] := eps
\begin{array}{l}
\\
\varepsilon
\end{array}
(FPCore (x eps) :precision binary64 (fma (sin x) (- (cos eps) 1.0) (* (sin eps) (cos x))))
double code(double x, double eps) {
return fma(sin(x), (cos(eps) - 1.0), (sin(eps) * cos(x)));
}
function code(x, eps) return fma(sin(x), Float64(cos(eps) - 1.0), Float64(sin(eps) * cos(x))) end
code[x_, eps_] := N[(N[Sin[x], $MachinePrecision] * N[(N[Cos[eps], $MachinePrecision] - 1.0), $MachinePrecision] + N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sin x, \cos \varepsilon - 1, \sin \varepsilon \cdot \cos x\right)
\end{array}
herbie shell --seed 2023340
(FPCore (x eps)
:name "2sin (example 3.3)"
:precision binary64
:herbie-target
(fma (sin x) (- (cos eps) 1.0) (* (sin eps) (cos x)))
(- (sin (+ x eps)) (sin x)))