
(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 6 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 (* (sin eps) (- (cos x) (* (sin x) (tan (* eps 0.5))))))
double code(double x, double eps) {
return sin(eps) * (cos(x) - (sin(x) * tan((eps * 0.5))));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin(eps) * (cos(x) - (sin(x) * tan((eps * 0.5d0))))
end function
public static double code(double x, double eps) {
return Math.sin(eps) * (Math.cos(x) - (Math.sin(x) * Math.tan((eps * 0.5))));
}
def code(x, eps): return math.sin(eps) * (math.cos(x) - (math.sin(x) * math.tan((eps * 0.5))))
function code(x, eps) return Float64(sin(eps) * Float64(cos(x) - Float64(sin(x) * tan(Float64(eps * 0.5))))) end
function tmp = code(x, eps) tmp = sin(eps) * (cos(x) - (sin(x) * tan((eps * 0.5)))); end
code[x_, eps_] := N[(N[Sin[eps], $MachinePrecision] * N[(N[Cos[x], $MachinePrecision] - N[(N[Sin[x], $MachinePrecision] * N[Tan[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \left(\cos x - \sin x \cdot \tan \left(\varepsilon \cdot 0.5\right)\right)
\end{array}
Initial program 44.8%
sin-sum65.6%
associate--l+65.6%
Applied egg-rr65.6%
+-commutative65.6%
associate-+l-99.4%
*-commutative99.4%
*-rgt-identity99.4%
distribute-lft-out--99.4%
Simplified99.4%
flip--99.2%
metadata-eval99.2%
1-sub-cos99.6%
pow299.6%
Applied egg-rr99.6%
Taylor expanded in x around inf 99.6%
*-lft-identity99.6%
*-commutative99.6%
times-frac99.6%
/-rgt-identity99.6%
unpow299.6%
associate-*r/99.6%
hang-0p-tan99.7%
Simplified99.7%
associate-*r*99.7%
cancel-sign-sub-inv99.7%
distribute-lft-neg-in99.7%
distribute-rgt-neg-in99.7%
div-inv99.7%
metadata-eval99.7%
Applied egg-rr99.7%
distribute-rgt-neg-out99.7%
unsub-neg99.7%
*-commutative99.7%
associate-*r*99.7%
*-commutative99.7%
distribute-lft-out--99.7%
*-commutative99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.023) (not (<= eps 1.05e-5))) (- (sin eps) (sin x)) (* eps (cos x))))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.023) || !(eps <= 1.05e-5)) {
tmp = sin(eps) - sin(x);
} 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.023d0)) .or. (.not. (eps <= 1.05d-5))) then
tmp = sin(eps) - sin(x)
else
tmp = eps * cos(x)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((eps <= -0.023) || !(eps <= 1.05e-5)) {
tmp = Math.sin(eps) - Math.sin(x);
} else {
tmp = eps * Math.cos(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.023) or not (eps <= 1.05e-5): tmp = math.sin(eps) - math.sin(x) else: tmp = eps * math.cos(x) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.023) || !(eps <= 1.05e-5)) tmp = Float64(sin(eps) - sin(x)); else tmp = Float64(eps * cos(x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -0.023) || ~((eps <= 1.05e-5))) tmp = sin(eps) - sin(x); else tmp = eps * cos(x); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.023], N[Not[LessEqual[eps, 1.05e-5]], $MachinePrecision]], N[(N[Sin[eps], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.023 \lor \neg \left(\varepsilon \leq 1.05 \cdot 10^{-5}\right):\\
\;\;\;\;\sin \varepsilon - \sin x\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\end{array}
\end{array}
if eps < -0.023 or 1.04999999999999994e-5 < eps Initial program 61.0%
add-cbrt-cube60.9%
pow360.8%
Applied egg-rr60.8%
Taylor expanded in x around 0 61.8%
Taylor expanded in eps around inf 62.2%
if -0.023 < eps < 1.04999999999999994e-5Initial program 27.6%
Taylor expanded in eps around 0 98.6%
Final simplification79.9%
(FPCore (x eps) :precision binary64 (* (* 2.0 (sin (/ eps 2.0))) (cos (/ (+ eps (* x 2.0)) 2.0))))
double code(double x, double eps) {
return (2.0 * sin((eps / 2.0))) * cos(((eps + (x * 2.0)) / 2.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (2.0d0 * sin((eps / 2.0d0))) * cos(((eps + (x * 2.0d0)) / 2.0d0))
end function
public static double code(double x, double eps) {
return (2.0 * Math.sin((eps / 2.0))) * Math.cos(((eps + (x * 2.0)) / 2.0));
}
def code(x, eps): return (2.0 * math.sin((eps / 2.0))) * math.cos(((eps + (x * 2.0)) / 2.0))
function code(x, eps) return Float64(Float64(2.0 * sin(Float64(eps / 2.0))) * cos(Float64(Float64(eps + Float64(x * 2.0)) / 2.0))) end
function tmp = code(x, eps) tmp = (2.0 * sin((eps / 2.0))) * cos(((eps + (x * 2.0)) / 2.0)); end
code[x_, eps_] := N[(N[(2.0 * N[Sin[N[(eps / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(N[(eps + N[(x * 2.0), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(2 \cdot \sin \left(\frac{\varepsilon}{2}\right)\right) \cdot \cos \left(\frac{\varepsilon + x \cdot 2}{2}\right)
\end{array}
Initial program 44.8%
add-cbrt-cube39.5%
pow339.6%
Applied egg-rr39.6%
rem-cbrt-cube44.8%
diff-sin44.4%
+-commutative44.4%
+-commutative44.4%
Applied egg-rr44.4%
associate-*r*44.4%
associate--l+79.3%
+-inverses79.3%
associate-+l+79.3%
count-279.3%
Simplified79.3%
Final simplification79.3%
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.023) (not (<= eps 4.8e-5))) (sin eps) (* eps (cos x))))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.023) || !(eps <= 4.8e-5)) {
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.023d0)) .or. (.not. (eps <= 4.8d-5))) 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.023) || !(eps <= 4.8e-5)) {
tmp = Math.sin(eps);
} else {
tmp = eps * Math.cos(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.023) or not (eps <= 4.8e-5): tmp = math.sin(eps) else: tmp = eps * math.cos(x) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.023) || !(eps <= 4.8e-5)) 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.023) || ~((eps <= 4.8e-5))) tmp = sin(eps); else tmp = eps * cos(x); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.023], N[Not[LessEqual[eps, 4.8e-5]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.023 \lor \neg \left(\varepsilon \leq 4.8 \cdot 10^{-5}\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\end{array}
\end{array}
if eps < -0.023 or 4.8000000000000001e-5 < eps Initial program 61.0%
Taylor expanded in x around 0 61.0%
if -0.023 < eps < 4.8000000000000001e-5Initial program 27.6%
Taylor expanded in eps around 0 98.6%
Final simplification79.2%
(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 44.8%
Taylor expanded in x around 0 58.5%
Final simplification58.5%
(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 44.8%
Taylor expanded in eps around 0 49.6%
Taylor expanded in x around 0 28.8%
Final simplification28.8%
(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 2024020
(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)))