
(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) (* (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 41.9%
sin-sum66.4%
associate--l+66.4%
Applied egg-rr66.4%
+-commutative66.4%
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.6%
distribute-rgt-neg-in99.6%
sub-neg99.6%
distribute-neg-in99.6%
metadata-eval99.6%
remove-double-neg99.6%
+-commutative99.6%
Simplified99.6%
Final simplification99.6%
(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}
Initial program 41.9%
sin-sum66.4%
associate--l+66.4%
Applied egg-rr66.4%
+-commutative66.4%
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 2.0))) (cos (/ (+ eps (+ x x)) 2.0))))
double code(double x, double eps) {
return (2.0 * sin((eps / 2.0))) * cos(((eps + (x + x)) / 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 + x)) / 2.0d0))
end function
public static double code(double x, double eps) {
return (2.0 * Math.sin((eps / 2.0))) * Math.cos(((eps + (x + x)) / 2.0));
}
def code(x, eps): return (2.0 * math.sin((eps / 2.0))) * math.cos(((eps + (x + x)) / 2.0))
function code(x, eps) return Float64(Float64(2.0 * sin(Float64(eps / 2.0))) * cos(Float64(Float64(eps + Float64(x + x)) / 2.0))) end
function tmp = code(x, eps) tmp = (2.0 * sin((eps / 2.0))) * cos(((eps + (x + x)) / 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 + x), $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 + \left(x + x\right)}{2}\right)
\end{array}
Initial program 41.9%
add-cube-cbrt41.0%
pow341.0%
Applied egg-rr41.0%
rem-cube-cbrt41.9%
diff-sin41.5%
+-commutative41.5%
+-commutative41.5%
Applied egg-rr41.5%
associate-*r*41.5%
associate--l+75.8%
+-inverses75.8%
associate-+l+75.9%
Simplified75.9%
Final simplification75.9%
(FPCore (x eps) :precision binary64 (if (or (<= eps -4e-6) (not (<= eps 9e-5))) (sin eps) (* (cos x) eps)))
double code(double x, double eps) {
double tmp;
if ((eps <= -4e-6) || !(eps <= 9e-5)) {
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 <= (-4d-6)) .or. (.not. (eps <= 9d-5))) 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 <= -4e-6) || !(eps <= 9e-5)) {
tmp = Math.sin(eps);
} else {
tmp = Math.cos(x) * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -4e-6) or not (eps <= 9e-5): tmp = math.sin(eps) else: tmp = math.cos(x) * eps return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -4e-6) || !(eps <= 9e-5)) tmp = sin(eps); else tmp = Float64(cos(x) * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -4e-6) || ~((eps <= 9e-5))) tmp = sin(eps); else tmp = cos(x) * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -4e-6], N[Not[LessEqual[eps, 9e-5]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -4 \cdot 10^{-6} \lor \neg \left(\varepsilon \leq 9 \cdot 10^{-5}\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\end{array}
\end{array}
if eps < -3.99999999999999982e-6 or 9.00000000000000057e-5 < eps Initial program 49.2%
Taylor expanded in x around 0 49.7%
if -3.99999999999999982e-6 < eps < 9.00000000000000057e-5Initial program 35.0%
Taylor expanded in eps around 0 99.9%
Final simplification75.4%
(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.9%
Taylor expanded in x around 0 56.6%
Final simplification56.6%
(FPCore (x eps) :precision binary64 0.0)
double code(double x, double eps) {
return 0.0;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 0.0d0
end function
public static double code(double x, double eps) {
return 0.0;
}
def code(x, eps): return 0.0
function code(x, eps) return 0.0 end
function tmp = code(x, eps) tmp = 0.0; end
code[x_, eps_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 41.9%
add-cube-cbrt41.0%
pow341.0%
Applied egg-rr41.0%
Taylor expanded in eps around 0 4.3%
pow-base-14.3%
*-lft-identity4.3%
+-inverses4.3%
Simplified4.3%
Final simplification4.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 41.9%
Taylor expanded in eps around 0 53.2%
Taylor expanded in x around 0 34.4%
Final simplification34.4%
(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 2023306
(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)))