
(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 (+ (cos eps) -1.0) (sin x) (* (sin eps) (cos x))))
double code(double x, double eps) {
return fma((cos(eps) + -1.0), sin(x), (sin(eps) * cos(x)));
}
function code(x, eps) return fma(Float64(cos(eps) + -1.0), sin(x), Float64(sin(eps) * cos(x))) end
code[x_, eps_] := N[(N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision] * N[Sin[x], $MachinePrecision] + N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\cos \varepsilon + -1, \sin x, \sin \varepsilon \cdot \cos x\right)
\end{array}
Initial program 41.8%
sin-sum67.3%
associate--l+67.3%
Applied egg-rr67.3%
+-commutative67.3%
associate-+l-99.6%
*-commutative99.6%
*-rgt-identity99.6%
distribute-lft-out--99.5%
Simplified99.5%
Taylor expanded in eps around inf 99.5%
fma-neg99.5%
distribute-rgt-neg-in99.5%
sub-neg99.5%
distribute-neg-in99.5%
metadata-eval99.5%
remove-double-neg99.5%
+-commutative99.5%
Simplified99.5%
Taylor expanded in x around inf 99.5%
+-commutative99.5%
*-commutative99.5%
fma-def99.6%
sub-neg99.6%
metadata-eval99.6%
*-commutative99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x eps) :precision binary64 (fma (cos x) (sin eps) (* (+ (cos eps) -1.0) (sin x))))
double code(double x, double eps) {
return fma(cos(x), sin(eps), ((cos(eps) + -1.0) * sin(x)));
}
function code(x, eps) return fma(cos(x), sin(eps), Float64(Float64(cos(eps) + -1.0) * sin(x))) end
code[x_, eps_] := N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision] + N[(N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\cos x, \sin \varepsilon, \left(\cos \varepsilon + -1\right) \cdot \sin x\right)
\end{array}
Initial program 41.8%
sin-sum67.3%
associate--l+67.3%
Applied egg-rr67.3%
+-commutative67.3%
associate-+l-99.6%
*-commutative99.6%
*-rgt-identity99.6%
distribute-lft-out--99.5%
Simplified99.5%
Taylor expanded in eps around inf 99.5%
fma-neg99.5%
distribute-rgt-neg-in99.5%
sub-neg99.5%
distribute-neg-in99.5%
metadata-eval99.5%
remove-double-neg99.5%
+-commutative99.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 41.8%
sin-sum67.3%
associate--l+67.3%
Applied egg-rr67.3%
+-commutative67.3%
associate-+l-99.6%
*-commutative99.6%
*-rgt-identity99.6%
distribute-lft-out--99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x eps) :precision binary64 (fma (cos x) (sin eps) (* (sin x) 0.0)))
double code(double x, double eps) {
return fma(cos(x), sin(eps), (sin(x) * 0.0));
}
function code(x, eps) return fma(cos(x), sin(eps), Float64(sin(x) * 0.0)) end
code[x_, eps_] := N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * 0.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\cos x, \sin \varepsilon, \sin x \cdot 0\right)
\end{array}
Initial program 41.8%
sin-sum67.3%
associate--l+67.3%
Applied egg-rr67.3%
+-commutative67.3%
associate-+l-99.6%
*-commutative99.6%
*-rgt-identity99.6%
distribute-lft-out--99.5%
Simplified99.5%
Taylor expanded in eps around inf 99.5%
fma-neg99.5%
distribute-rgt-neg-in99.5%
sub-neg99.5%
distribute-neg-in99.5%
metadata-eval99.5%
remove-double-neg99.5%
+-commutative99.5%
Simplified99.5%
Taylor expanded in eps around 0 75.3%
Final simplification75.3%
(FPCore (x eps) :precision binary64 (* (cos (* 0.5 (+ eps (* x 2.0)))) (* 2.0 (sin (* eps 0.5)))))
double code(double x, double eps) {
return cos((0.5 * (eps + (x * 2.0)))) * (2.0 * sin((eps * 0.5)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = cos((0.5d0 * (eps + (x * 2.0d0)))) * (2.0d0 * sin((eps * 0.5d0)))
end function
public static double code(double x, double eps) {
return Math.cos((0.5 * (eps + (x * 2.0)))) * (2.0 * Math.sin((eps * 0.5)));
}
def code(x, eps): return math.cos((0.5 * (eps + (x * 2.0)))) * (2.0 * math.sin((eps * 0.5)))
function code(x, eps) return Float64(cos(Float64(0.5 * Float64(eps + Float64(x * 2.0)))) * Float64(2.0 * sin(Float64(eps * 0.5)))) end
function tmp = code(x, eps) tmp = cos((0.5 * (eps + (x * 2.0)))) * (2.0 * sin((eps * 0.5))); end
code[x_, eps_] := N[(N[Cos[N[(0.5 * N[(eps + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[(2.0 * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos \left(0.5 \cdot \left(\varepsilon + x \cdot 2\right)\right) \cdot \left(2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)
\end{array}
Initial program 41.8%
add-cbrt-cube36.6%
pow336.6%
Applied egg-rr36.6%
rem-cbrt-cube41.8%
diff-sin41.2%
div-inv41.2%
+-commutative41.2%
associate--l+74.0%
metadata-eval74.0%
div-inv74.0%
+-commutative74.0%
associate-+l+73.9%
metadata-eval73.9%
Applied egg-rr73.9%
associate-*r*73.9%
*-commutative73.9%
*-commutative73.9%
count-273.9%
*-commutative73.9%
+-inverses73.9%
+-rgt-identity73.9%
Simplified73.9%
Final simplification73.9%
(FPCore (x eps) :precision binary64 (if (<= eps -520000000000.0) (- (sin (+ eps x)) (sin x)) (if (<= eps 6e-6) (* eps (cos x)) (sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -520000000000.0) {
tmp = sin((eps + x)) - sin(x);
} else if (eps <= 6e-6) {
tmp = eps * cos(x);
} else {
tmp = sin(eps);
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (eps <= (-520000000000.0d0)) then
tmp = sin((eps + x)) - sin(x)
else if (eps <= 6d-6) then
tmp = eps * cos(x)
else
tmp = sin(eps)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= -520000000000.0) {
tmp = Math.sin((eps + x)) - Math.sin(x);
} else if (eps <= 6e-6) {
tmp = eps * Math.cos(x);
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -520000000000.0: tmp = math.sin((eps + x)) - math.sin(x) elif eps <= 6e-6: tmp = eps * math.cos(x) else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -520000000000.0) tmp = Float64(sin(Float64(eps + x)) - sin(x)); elseif (eps <= 6e-6) tmp = Float64(eps * cos(x)); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -520000000000.0) tmp = sin((eps + x)) - sin(x); elseif (eps <= 6e-6) tmp = eps * cos(x); else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -520000000000.0], N[(N[Sin[N[(eps + x), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision], If[LessEqual[eps, 6e-6], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -520000000000:\\
\;\;\;\;\sin \left(\varepsilon + x\right) - \sin x\\
\mathbf{elif}\;\varepsilon \leq 6 \cdot 10^{-6}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -5.2e11Initial program 53.4%
if -5.2e11 < eps < 6.0000000000000002e-6Initial program 25.6%
Taylor expanded in eps around 0 98.1%
if 6.0000000000000002e-6 < eps Initial program 56.6%
Taylor expanded in x around 0 57.7%
Final simplification74.6%
(FPCore (x eps) :precision binary64 (if (or (<= eps -520000000000.0) (not (<= eps 1.55e-6))) (sin eps) (* eps (cos x))))
double code(double x, double eps) {
double tmp;
if ((eps <= -520000000000.0) || !(eps <= 1.55e-6)) {
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 <= (-520000000000.0d0)) .or. (.not. (eps <= 1.55d-6))) 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 <= -520000000000.0) || !(eps <= 1.55e-6)) {
tmp = Math.sin(eps);
} else {
tmp = eps * Math.cos(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -520000000000.0) or not (eps <= 1.55e-6): tmp = math.sin(eps) else: tmp = eps * math.cos(x) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -520000000000.0) || !(eps <= 1.55e-6)) tmp = sin(eps); else tmp = Float64(eps * cos(x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -520000000000.0) || ~((eps <= 1.55e-6))) tmp = sin(eps); else tmp = eps * cos(x); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -520000000000.0], N[Not[LessEqual[eps, 1.55e-6]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -520000000000 \lor \neg \left(\varepsilon \leq 1.55 \cdot 10^{-6}\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\end{array}
\end{array}
if eps < -5.2e11 or 1.55e-6 < eps Initial program 54.9%
Taylor expanded in x around 0 55.3%
if -5.2e11 < eps < 1.55e-6Initial program 25.6%
Taylor expanded in eps around 0 98.1%
Final simplification74.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 41.8%
Taylor expanded in x around 0 53.8%
Final simplification53.8%
(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 41.8%
Taylor expanded in eps around 0 45.6%
Taylor expanded in x around 0 24.9%
Final simplification24.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 2023310
(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)))