
(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 8 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
(let* ((t_0 (+ (cos eps) -1.0)))
(+
(fma (cos x) (sin eps) (* (sin x) t_0))
(fma t_0 (sin x) (* (- 1.0 (cos eps)) (sin x))))))
double code(double x, double eps) {
double t_0 = cos(eps) + -1.0;
return fma(cos(x), sin(eps), (sin(x) * t_0)) + fma(t_0, sin(x), ((1.0 - cos(eps)) * sin(x)));
}
function code(x, eps) t_0 = Float64(cos(eps) + -1.0) return Float64(fma(cos(x), sin(eps), Float64(sin(x) * t_0)) + fma(t_0, sin(x), Float64(Float64(1.0 - cos(eps)) * sin(x)))) end
code[x_, eps_] := Block[{t$95$0 = N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]}, N[(N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision] + N[(t$95$0 * N[Sin[x], $MachinePrecision] + N[(N[(1.0 - N[Cos[eps], $MachinePrecision]), $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \varepsilon + -1\\
\mathsf{fma}\left(\cos x, \sin \varepsilon, \sin x \cdot t_0\right) + \mathsf{fma}\left(t_0, \sin x, \left(1 - \cos \varepsilon\right) \cdot \sin x\right)
\end{array}
\end{array}
Initial program 42.7%
sin-sum67.5%
associate--l+67.4%
Applied egg-rr67.4%
+-commutative67.4%
associate-+l-99.3%
*-commutative99.3%
*-rgt-identity99.3%
distribute-lft-out--99.4%
Simplified99.4%
log1p-expm1-u99.3%
Applied egg-rr99.3%
log1p-expm1-u99.4%
*-commutative99.4%
prod-diff99.4%
distribute-rgt-neg-in99.4%
*-commutative99.4%
Applied egg-rr99.4%
Final simplification99.4%
(FPCore (x eps)
:precision binary64
(let* ((t_0 (- (sin (+ x eps)) (sin x))))
(if (<= t_0 -5e-6)
t_0
(if (<= t_0 0.0) (* (cos x) (* 2.0 (sin (* eps 0.5)))) (sin eps)))))
double code(double x, double eps) {
double t_0 = sin((x + eps)) - sin(x);
double tmp;
if (t_0 <= -5e-6) {
tmp = t_0;
} else if (t_0 <= 0.0) {
tmp = cos(x) * (2.0 * sin((eps * 0.5)));
} else {
tmp = sin(eps);
}
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((x + eps)) - sin(x)
if (t_0 <= (-5d-6)) then
tmp = t_0
else if (t_0 <= 0.0d0) then
tmp = cos(x) * (2.0d0 * sin((eps * 0.5d0)))
else
tmp = sin(eps)
end if
code = tmp
end function
public static double code(double x, double eps) {
double t_0 = Math.sin((x + eps)) - Math.sin(x);
double tmp;
if (t_0 <= -5e-6) {
tmp = t_0;
} else if (t_0 <= 0.0) {
tmp = Math.cos(x) * (2.0 * Math.sin((eps * 0.5)));
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): t_0 = math.sin((x + eps)) - math.sin(x) tmp = 0 if t_0 <= -5e-6: tmp = t_0 elif t_0 <= 0.0: tmp = math.cos(x) * (2.0 * math.sin((eps * 0.5))) else: tmp = math.sin(eps) return tmp
function code(x, eps) t_0 = Float64(sin(Float64(x + eps)) - sin(x)) tmp = 0.0 if (t_0 <= -5e-6) tmp = t_0; elseif (t_0 <= 0.0) tmp = Float64(cos(x) * Float64(2.0 * sin(Float64(eps * 0.5)))); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) t_0 = sin((x + eps)) - sin(x); tmp = 0.0; if (t_0 <= -5e-6) tmp = t_0; elseif (t_0 <= 0.0) tmp = cos(x) * (2.0 * sin((eps * 0.5))); else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := Block[{t$95$0 = N[(N[Sin[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -5e-6], t$95$0, If[LessEqual[t$95$0, 0.0], N[(N[Cos[x], $MachinePrecision] * N[(2.0 * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(x + \varepsilon\right) - \sin x\\
\mathbf{if}\;t_0 \leq -5 \cdot 10^{-6}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;\cos x \cdot \left(2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < -5.00000000000000041e-6Initial program 69.8%
if -5.00000000000000041e-6 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < 0.0Initial program 20.0%
diff-sin20.0%
div-inv20.0%
associate--l+20.0%
metadata-eval20.0%
div-inv20.0%
+-commutative20.0%
associate-+l+20.0%
metadata-eval20.0%
Applied egg-rr20.0%
associate-*r*20.0%
*-commutative20.0%
*-commutative20.0%
+-commutative20.0%
count-220.0%
fma-def20.0%
sub-neg20.0%
mul-1-neg20.0%
+-commutative20.0%
associate-+r+79.6%
mul-1-neg79.6%
sub-neg79.6%
+-inverses79.6%
remove-double-neg79.6%
mul-1-neg79.6%
sub-neg79.6%
neg-sub079.6%
mul-1-neg79.6%
remove-double-neg79.6%
Simplified79.6%
Taylor expanded in eps around 0 79.6%
if 0.0 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) Initial program 72.0%
Taylor expanded in x around 0 73.9%
Final simplification76.3%
(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 42.7%
sin-sum67.5%
associate--l+67.4%
Applied egg-rr67.4%
+-commutative67.4%
associate-+l-99.3%
*-commutative99.3%
*-rgt-identity99.3%
distribute-lft-out--99.4%
Simplified99.4%
Taylor expanded in eps around inf 99.4%
fma-neg99.4%
distribute-rgt-neg-in99.4%
neg-sub099.4%
associate--r-99.4%
metadata-eval99.4%
+-commutative99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (x eps) :precision binary64 (- (* (cos x) (sin eps)) (* (- 1.0 (cos eps)) (sin x))))
double code(double x, double eps) {
return (cos(x) * sin(eps)) - ((1.0 - cos(eps)) * sin(x));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (cos(x) * sin(eps)) - ((1.0d0 - cos(eps)) * sin(x))
end function
public static double code(double x, double eps) {
return (Math.cos(x) * Math.sin(eps)) - ((1.0 - Math.cos(eps)) * Math.sin(x));
}
def code(x, eps): return (math.cos(x) * math.sin(eps)) - ((1.0 - math.cos(eps)) * math.sin(x))
function code(x, eps) return Float64(Float64(cos(x) * sin(eps)) - Float64(Float64(1.0 - cos(eps)) * sin(x))) end
function tmp = code(x, eps) tmp = (cos(x) * sin(eps)) - ((1.0 - cos(eps)) * sin(x)); end
code[x_, eps_] := N[(N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision]), $MachinePrecision] - N[(N[(1.0 - N[Cos[eps], $MachinePrecision]), $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos x \cdot \sin \varepsilon - \left(1 - \cos \varepsilon\right) \cdot \sin x
\end{array}
Initial program 42.7%
sin-sum67.5%
associate--l+67.4%
Applied egg-rr67.4%
+-commutative67.4%
associate-+l-99.3%
*-commutative99.3%
*-rgt-identity99.3%
distribute-lft-out--99.4%
Simplified99.4%
Final simplification99.4%
(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 42.7%
add-sqr-sqrt19.5%
sqrt-unprod21.4%
pow221.4%
Applied egg-rr21.4%
sqrt-pow142.7%
metadata-eval42.7%
pow142.7%
diff-sin42.5%
+-commutative42.5%
+-commutative42.5%
Applied egg-rr42.5%
associate--l+76.1%
+-commutative76.1%
Simplified76.1%
Final simplification76.1%
(FPCore (x eps) :precision binary64 (if (or (<= eps -2.8e-5) (not (<= eps 1.3e-22))) (sin eps) (* (cos x) eps)))
double code(double x, double eps) {
double tmp;
if ((eps <= -2.8e-5) || !(eps <= 1.3e-22)) {
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 <= (-2.8d-5)) .or. (.not. (eps <= 1.3d-22))) 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 <= -2.8e-5) || !(eps <= 1.3e-22)) {
tmp = Math.sin(eps);
} else {
tmp = Math.cos(x) * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -2.8e-5) or not (eps <= 1.3e-22): tmp = math.sin(eps) else: tmp = math.cos(x) * eps return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -2.8e-5) || !(eps <= 1.3e-22)) tmp = sin(eps); else tmp = Float64(cos(x) * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -2.8e-5) || ~((eps <= 1.3e-22))) tmp = sin(eps); else tmp = cos(x) * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -2.8e-5], N[Not[LessEqual[eps, 1.3e-22]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -2.8 \cdot 10^{-5} \lor \neg \left(\varepsilon \leq 1.3 \cdot 10^{-22}\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\end{array}
\end{array}
if eps < -2.79999999999999996e-5 or 1.3e-22 < eps Initial program 53.6%
Taylor expanded in x around 0 55.6%
if -2.79999999999999996e-5 < eps < 1.3e-22Initial program 30.4%
Taylor expanded in eps around 0 99.0%
Final simplification75.9%
(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 42.7%
Taylor expanded in x around 0 56.8%
Final simplification56.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 42.7%
Taylor expanded in eps around 0 49.8%
Taylor expanded in x around 0 30.7%
Final simplification30.7%
(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}
herbie shell --seed 2023311
(FPCore (x eps)
:name "2sin (example 3.3)"
:precision binary64
:herbie-target
(fma (cos x) (sin eps) (* (sin x) (- (cos eps) 1.0)))
(- (sin (+ x eps)) (sin x)))