
(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 9 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 (sin eps) (cos x) (* (sin x) (log (exp (+ (cos eps) -1.0))))))
double code(double x, double eps) {
return fma(sin(eps), cos(x), (sin(x) * log(exp((cos(eps) + -1.0)))));
}
function code(x, eps) return fma(sin(eps), cos(x), Float64(sin(x) * log(exp(Float64(cos(eps) + -1.0))))) end
code[x_, eps_] := N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * N[Log[N[Exp[N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sin \varepsilon, \cos x, \sin x \cdot \log \left(e^{\cos \varepsilon + -1}\right)\right)
\end{array}
Initial program 39.6%
sin-sum64.4%
associate--l+64.4%
Applied egg-rr64.4%
associate-+r-64.4%
+-commutative64.4%
associate-+r-99.4%
*-commutative99.4%
sub-neg99.4%
fma-def99.4%
*-commutative99.4%
neg-mul-199.4%
distribute-rgt-out99.5%
Simplified99.5%
add-log-exp99.5%
Applied egg-rr99.5%
Final simplification99.5%
(FPCore (x eps) :precision binary64 (let* ((t_0 (- (sin (+ eps x)) (sin x)))) (if (or (<= t_0 -0.04) (not (<= t_0 4e-171))) t_0 (* eps (cos x)))))
double code(double x, double eps) {
double t_0 = sin((eps + x)) - sin(x);
double tmp;
if ((t_0 <= -0.04) || !(t_0 <= 4e-171)) {
tmp = t_0;
} else {
tmp = eps * cos(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 = sin((eps + x)) - sin(x)
if ((t_0 <= (-0.04d0)) .or. (.not. (t_0 <= 4d-171))) then
tmp = t_0
else
tmp = eps * cos(x)
end if
code = tmp
end function
public static double code(double x, double eps) {
double t_0 = Math.sin((eps + x)) - Math.sin(x);
double tmp;
if ((t_0 <= -0.04) || !(t_0 <= 4e-171)) {
tmp = t_0;
} else {
tmp = eps * Math.cos(x);
}
return tmp;
}
def code(x, eps): t_0 = math.sin((eps + x)) - math.sin(x) tmp = 0 if (t_0 <= -0.04) or not (t_0 <= 4e-171): tmp = t_0 else: tmp = eps * math.cos(x) return tmp
function code(x, eps) t_0 = Float64(sin(Float64(eps + x)) - sin(x)) tmp = 0.0 if ((t_0 <= -0.04) || !(t_0 <= 4e-171)) tmp = t_0; else tmp = Float64(eps * cos(x)); end return tmp end
function tmp_2 = code(x, eps) t_0 = sin((eps + x)) - sin(x); tmp = 0.0; if ((t_0 <= -0.04) || ~((t_0 <= 4e-171))) tmp = t_0; else tmp = eps * cos(x); end tmp_2 = tmp; end
code[x_, eps_] := Block[{t$95$0 = N[(N[Sin[N[(eps + x), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -0.04], N[Not[LessEqual[t$95$0, 4e-171]], $MachinePrecision]], t$95$0, N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(\varepsilon + x\right) - \sin x\\
\mathbf{if}\;t_0 \leq -0.04 \lor \neg \left(t_0 \leq 4 \cdot 10^{-171}\right):\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\end{array}
\end{array}
if (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < -0.0400000000000000008 or 3.9999999999999999e-171 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) Initial program 66.6%
if -0.0400000000000000008 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < 3.9999999999999999e-171Initial program 18.3%
Taylor expanded in eps around 0 81.5%
Final simplification74.9%
(FPCore (x eps) :precision binary64 (fma (sin eps) (cos x) (* (sin x) (+ (cos eps) -1.0))))
double code(double x, double eps) {
return fma(sin(eps), cos(x), (sin(x) * (cos(eps) + -1.0)));
}
function code(x, eps) return fma(sin(eps), cos(x), Float64(sin(x) * Float64(cos(eps) + -1.0))) end
code[x_, eps_] := N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sin \varepsilon, \cos x, \sin x \cdot \left(\cos \varepsilon + -1\right)\right)
\end{array}
Initial program 39.6%
sin-sum64.4%
associate--l+64.4%
Applied egg-rr64.4%
associate-+r-64.4%
+-commutative64.4%
associate-+r-99.4%
*-commutative99.4%
sub-neg99.4%
fma-def99.4%
*-commutative99.4%
neg-mul-199.4%
distribute-rgt-out99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x eps) :precision binary64 (+ (* (sin eps) (cos x)) (* (sin x) (+ (cos eps) -1.0))))
double code(double x, double eps) {
return (sin(eps) * cos(x)) + (sin(x) * (cos(eps) + -1.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (sin(eps) * cos(x)) + (sin(x) * (cos(eps) + (-1.0d0)))
end function
public static double code(double x, double eps) {
return (Math.sin(eps) * Math.cos(x)) + (Math.sin(x) * (Math.cos(eps) + -1.0));
}
def code(x, eps): return (math.sin(eps) * math.cos(x)) + (math.sin(x) * (math.cos(eps) + -1.0))
function code(x, eps) return Float64(Float64(sin(eps) * cos(x)) + Float64(sin(x) * Float64(cos(eps) + -1.0))) end
function tmp = code(x, eps) tmp = (sin(eps) * cos(x)) + (sin(x) * (cos(eps) + -1.0)); end
code[x_, eps_] := N[(N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \cos x + \sin x \cdot \left(\cos \varepsilon + -1\right)
\end{array}
Initial program 39.6%
sin-sum64.4%
associate--l+64.4%
Applied egg-rr64.4%
associate-+r-64.4%
+-commutative64.4%
associate-+r-99.4%
*-commutative99.4%
sub-neg99.4%
fma-def99.4%
*-commutative99.4%
neg-mul-199.4%
distribute-rgt-out99.5%
Simplified99.5%
fma-udef99.5%
*-commutative99.5%
distribute-lft-in99.4%
+-commutative99.4%
*-commutative99.4%
mul-1-neg99.4%
associate-+l+64.4%
sub-neg64.4%
associate-+l-99.4%
*-commutative99.4%
*-un-lft-identity99.4%
metadata-eval99.4%
*-commutative99.4%
distribute-rgt-out--99.5%
metadata-eval99.5%
Applied egg-rr99.5%
Final simplification99.5%
(FPCore (x eps) :precision binary64 (fma (sin eps) (cos x) (* (sin x) 0.0)))
double code(double x, double eps) {
return fma(sin(eps), cos(x), (sin(x) * 0.0));
}
function code(x, eps) return fma(sin(eps), cos(x), Float64(sin(x) * 0.0)) end
code[x_, eps_] := N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * 0.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sin \varepsilon, \cos x, \sin x \cdot 0\right)
\end{array}
Initial program 39.6%
sin-sum64.4%
associate--l+64.4%
Applied egg-rr64.4%
associate-+r-64.4%
+-commutative64.4%
associate-+r-99.4%
*-commutative99.4%
sub-neg99.4%
fma-def99.4%
*-commutative99.4%
neg-mul-199.4%
distribute-rgt-out99.5%
Simplified99.5%
Taylor expanded in eps around 0 76.7%
Final simplification76.7%
(FPCore (x eps) :precision binary64 (* (sin (* eps 0.5)) (* 2.0 (cos (* 0.5 (+ eps (* x 2.0)))))))
double code(double x, double eps) {
return sin((eps * 0.5)) * (2.0 * cos((0.5 * (eps + (x * 2.0)))));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin((eps * 0.5d0)) * (2.0d0 * cos((0.5d0 * (eps + (x * 2.0d0)))))
end function
public static double code(double x, double eps) {
return Math.sin((eps * 0.5)) * (2.0 * Math.cos((0.5 * (eps + (x * 2.0)))));
}
def code(x, eps): return math.sin((eps * 0.5)) * (2.0 * math.cos((0.5 * (eps + (x * 2.0)))))
function code(x, eps) return Float64(sin(Float64(eps * 0.5)) * Float64(2.0 * cos(Float64(0.5 * Float64(eps + Float64(x * 2.0)))))) end
function tmp = code(x, eps) tmp = sin((eps * 0.5)) * (2.0 * cos((0.5 * (eps + (x * 2.0))))); end
code[x_, eps_] := N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[(2.0 * N[Cos[N[(0.5 * N[(eps + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \left(\varepsilon \cdot 0.5\right) \cdot \left(2 \cdot \cos \left(0.5 \cdot \left(\varepsilon + x \cdot 2\right)\right)\right)
\end{array}
Initial program 39.6%
diff-sin39.0%
div-inv39.0%
+-commutative39.0%
associate--l+75.3%
*-un-lft-identity75.3%
*-un-lft-identity75.3%
distribute-rgt-out--75.3%
metadata-eval75.3%
metadata-eval75.3%
div-inv75.3%
+-commutative75.3%
associate-+l+75.2%
count-275.2%
*-commutative75.2%
metadata-eval75.2%
Applied egg-rr75.2%
*-commutative75.2%
associate-*l*75.2%
mul0-rgt75.2%
*-commutative75.2%
Simplified75.2%
Final simplification75.2%
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.00032) (not (<= eps 0.0035))) (sin eps) (* eps (cos x))))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.00032) || !(eps <= 0.0035)) {
tmp = sin(eps);
} else {
tmp = eps * cos(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.00032d0)) .or. (.not. (eps <= 0.0035d0))) then
tmp = sin(eps)
else
tmp = eps * cos(x)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((eps <= -0.00032) || !(eps <= 0.0035)) {
tmp = Math.sin(eps);
} else {
tmp = eps * Math.cos(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.00032) or not (eps <= 0.0035): tmp = math.sin(eps) else: tmp = eps * math.cos(x) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.00032) || !(eps <= 0.0035)) tmp = sin(eps); else tmp = Float64(eps * cos(x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -0.00032) || ~((eps <= 0.0035))) tmp = sin(eps); else tmp = eps * cos(x); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.00032], N[Not[LessEqual[eps, 0.0035]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.00032 \lor \neg \left(\varepsilon \leq 0.0035\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\end{array}
\end{array}
if eps < -3.20000000000000026e-4 or 0.00350000000000000007 < eps Initial program 50.1%
Taylor expanded in x around 0 50.1%
if -3.20000000000000026e-4 < eps < 0.00350000000000000007Initial program 29.2%
Taylor expanded in eps around 0 99.5%
Final simplification74.8%
(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 39.6%
Taylor expanded in x around 0 53.3%
Final simplification53.3%
(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 39.6%
Taylor expanded in eps around 0 51.5%
Taylor expanded in x around 0 29.9%
Final simplification29.9%
(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 2023336
(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)))