
(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 7 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 (fma (cos x) (sin eps) (- (* (cos eps) (sin x)) (sin x))))
double code(double x, double eps) {
return fma(cos(x), sin(eps), ((cos(eps) * sin(x)) - sin(x)));
}
function code(x, eps) return fma(cos(x), sin(eps), Float64(Float64(cos(eps) * sin(x)) - sin(x))) end
code[x_, eps_] := N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision] + N[(N[(N[Cos[eps], $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\cos x, \sin \varepsilon, \cos \varepsilon \cdot \sin x - \sin x\right)
\end{array}
Initial program 40.5%
sin-sum64.9%
associate--l+64.9%
Applied egg-rr64.9%
+-commutative64.9%
associate-+l-99.5%
*-commutative99.5%
*-rgt-identity99.5%
distribute-lft-out--99.5%
Simplified99.5%
Taylor expanded in eps around inf 99.5%
fma-neg99.5%
distribute-rgt-neg-in99.5%
neg-sub099.5%
associate--r-99.5%
metadata-eval99.5%
+-commutative99.5%
Simplified99.5%
distribute-rgt-in99.5%
neg-mul-199.5%
Applied egg-rr99.5%
Taylor expanded in eps around inf 99.5%
Final simplification99.5%
(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}
Initial program 40.5%
sin-sum64.9%
associate--l+64.9%
Applied egg-rr64.9%
+-commutative64.9%
associate-+l-99.5%
*-commutative99.5%
*-rgt-identity99.5%
distribute-lft-out--99.5%
Simplified99.5%
Taylor expanded in eps around inf 99.5%
fma-neg99.5%
distribute-rgt-neg-in99.5%
neg-sub099.5%
associate--r-99.5%
metadata-eval99.5%
+-commutative99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x eps) :precision binary64 (+ (* (cos x) (sin eps)) (* (sin x) (+ (cos eps) -1.0))))
double code(double x, double eps) {
return (cos(x) * sin(eps)) + (sin(x) * (cos(eps) + -1.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (cos(x) * sin(eps)) + (sin(x) * (cos(eps) + (-1.0d0)))
end function
public static double code(double x, double eps) {
return (Math.cos(x) * Math.sin(eps)) + (Math.sin(x) * (Math.cos(eps) + -1.0));
}
def code(x, eps): return (math.cos(x) * math.sin(eps)) + (math.sin(x) * (math.cos(eps) + -1.0))
function code(x, eps) return Float64(Float64(cos(x) * sin(eps)) + Float64(sin(x) * Float64(cos(eps) + -1.0))) end
function tmp = code(x, eps) tmp = (cos(x) * sin(eps)) + (sin(x) * (cos(eps) + -1.0)); end
code[x_, eps_] := N[(N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos x \cdot \sin \varepsilon + \sin x \cdot \left(\cos \varepsilon + -1\right)
\end{array}
Initial program 40.5%
sin-sum64.9%
associate--l+64.9%
Applied egg-rr64.9%
+-commutative64.9%
associate-+l-99.5%
*-commutative99.5%
*-rgt-identity99.5%
distribute-lft-out--99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x eps) :precision binary64 (* 2.0 (* (sin (/ (+ eps (- x x)) 2.0)) (cos (/ (+ x (+ x eps)) 2.0)))))
double code(double x, double eps) {
return 2.0 * (sin(((eps + (x - x)) / 2.0)) * cos(((x + (x + eps)) / 2.0)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 2.0d0 * (sin(((eps + (x - x)) / 2.0d0)) * cos(((x + (x + eps)) / 2.0d0)))
end function
public static double code(double x, double eps) {
return 2.0 * (Math.sin(((eps + (x - x)) / 2.0)) * Math.cos(((x + (x + eps)) / 2.0)));
}
def code(x, eps): return 2.0 * (math.sin(((eps + (x - x)) / 2.0)) * math.cos(((x + (x + eps)) / 2.0)))
function code(x, eps) return Float64(2.0 * Float64(sin(Float64(Float64(eps + Float64(x - x)) / 2.0)) * cos(Float64(Float64(x + Float64(x + eps)) / 2.0)))) end
function tmp = code(x, eps) tmp = 2.0 * (sin(((eps + (x - x)) / 2.0)) * cos(((x + (x + eps)) / 2.0))); end
code[x_, eps_] := N[(2.0 * N[(N[Sin[N[(N[(eps + N[(x - x), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(N[(x + N[(x + eps), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(\sin \left(\frac{\varepsilon + \left(x - x\right)}{2}\right) \cdot \cos \left(\frac{x + \left(x + \varepsilon\right)}{2}\right)\right)
\end{array}
Initial program 40.5%
log1p-expm1-u40.3%
Applied egg-rr40.3%
log1p-expm1-u40.5%
diff-sin39.9%
+-commutative39.9%
+-commutative39.9%
Applied egg-rr39.9%
associate--l+75.8%
+-commutative75.8%
Simplified75.8%
Final simplification75.8%
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.00014) (not (<= eps 3.2e-7))) (sin eps) (* (cos x) eps)))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.00014) || !(eps <= 3.2e-7)) {
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.00014d0)) .or. (.not. (eps <= 3.2d-7))) 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.00014) || !(eps <= 3.2e-7)) {
tmp = Math.sin(eps);
} else {
tmp = Math.cos(x) * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.00014) or not (eps <= 3.2e-7): tmp = math.sin(eps) else: tmp = math.cos(x) * eps return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.00014) || !(eps <= 3.2e-7)) 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.00014) || ~((eps <= 3.2e-7))) tmp = sin(eps); else tmp = cos(x) * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.00014], N[Not[LessEqual[eps, 3.2e-7]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.00014 \lor \neg \left(\varepsilon \leq 3.2 \cdot 10^{-7}\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\end{array}
\end{array}
if eps < -1.3999999999999999e-4 or 3.2000000000000001e-7 < eps Initial program 52.6%
Taylor expanded in x around 0 54.3%
if -1.3999999999999999e-4 < eps < 3.2000000000000001e-7Initial program 28.0%
Taylor expanded in eps around 0 99.4%
Final simplification76.5%
(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}
Initial program 40.5%
Taylor expanded in x around 0 57.4%
Final simplification57.4%
(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}
Initial program 40.5%
Taylor expanded in eps around 0 51.7%
+-commutative51.7%
fma-def51.7%
*-commutative51.7%
associate-*l*51.7%
Simplified51.7%
Taylor expanded in x around 0 32.3%
Final simplification32.3%
(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 2024019
(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)))