
(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 x) (sin eps) (log1p (expm1 (* (sin x) (+ (cos eps) -1.0))))))
double code(double x, double eps) {
return fma(cos(x), sin(eps), log1p(expm1((sin(x) * (cos(eps) + -1.0)))));
}
function code(x, eps) return fma(cos(x), sin(eps), log1p(expm1(Float64(sin(x) * Float64(cos(eps) + -1.0))))) end
code[x_, eps_] := N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision] + N[Log[1 + N[(Exp[N[(N[Sin[x], $MachinePrecision] * N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\cos x, \sin \varepsilon, \mathsf{log1p}\left(\mathsf{expm1}\left(\sin x \cdot \left(\cos \varepsilon + -1\right)\right)\right)\right)
\end{array}
Initial program 42.8%
sin-sum69.3%
associate--l+69.3%
Applied egg-rr69.3%
+-commutative69.3%
associate-+l-99.4%
*-commutative99.4%
*-rgt-identity99.4%
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%
log1p-expm1-u99.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 -0.01)
t_0
(if (<= t_0 0.0)
(* (cos x) (* 2.0 (sin (* eps 0.5))))
(fabs (sin eps))))))
double code(double x, double eps) {
double t_0 = sin((x + eps)) - sin(x);
double tmp;
if (t_0 <= -0.01) {
tmp = t_0;
} else if (t_0 <= 0.0) {
tmp = cos(x) * (2.0 * sin((eps * 0.5)));
} else {
tmp = fabs(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 <= (-0.01d0)) then
tmp = t_0
else if (t_0 <= 0.0d0) then
tmp = cos(x) * (2.0d0 * sin((eps * 0.5d0)))
else
tmp = abs(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 <= -0.01) {
tmp = t_0;
} else if (t_0 <= 0.0) {
tmp = Math.cos(x) * (2.0 * Math.sin((eps * 0.5)));
} else {
tmp = Math.abs(Math.sin(eps));
}
return tmp;
}
def code(x, eps): t_0 = math.sin((x + eps)) - math.sin(x) tmp = 0 if t_0 <= -0.01: tmp = t_0 elif t_0 <= 0.0: tmp = math.cos(x) * (2.0 * math.sin((eps * 0.5))) else: tmp = math.fabs(math.sin(eps)) return tmp
function code(x, eps) t_0 = Float64(sin(Float64(x + eps)) - sin(x)) tmp = 0.0 if (t_0 <= -0.01) tmp = t_0; elseif (t_0 <= 0.0) tmp = Float64(cos(x) * Float64(2.0 * sin(Float64(eps * 0.5)))); else tmp = abs(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 <= -0.01) tmp = t_0; elseif (t_0 <= 0.0) tmp = cos(x) * (2.0 * sin((eps * 0.5))); else tmp = abs(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, -0.01], 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[Abs[N[Sin[eps], $MachinePrecision]], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(x + \varepsilon\right) - \sin x\\
\mathbf{if}\;t_0 \leq -0.01:\\
\;\;\;\;t_0\\
\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;\cos x \cdot \left(2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\left|\sin \varepsilon\right|\\
\end{array}
\end{array}
if (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < -0.0100000000000000002Initial program 64.6%
if -0.0100000000000000002 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < 0.0Initial program 19.3%
diff-sin19.3%
div-inv19.3%
associate--l+19.3%
metadata-eval19.3%
div-inv19.3%
+-commutative19.3%
associate-+l+19.3%
metadata-eval19.3%
Applied egg-rr19.3%
associate-*r*19.3%
*-commutative19.3%
*-commutative19.3%
+-commutative19.3%
count-219.3%
fma-def19.3%
sub-neg19.3%
mul-1-neg19.3%
+-commutative19.3%
associate-+r+78.4%
mul-1-neg78.4%
sub-neg78.4%
+-inverses78.4%
remove-double-neg78.4%
mul-1-neg78.4%
sub-neg78.4%
neg-sub078.4%
mul-1-neg78.4%
remove-double-neg78.4%
Simplified78.4%
Taylor expanded in eps around 0 78.4%
if 0.0 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) Initial program 71.6%
Taylor expanded in x around 0 73.4%
add-sqr-sqrt72.2%
sqrt-unprod67.6%
pow267.6%
Applied egg-rr67.6%
unpow267.6%
rem-sqrt-square74.6%
Simplified74.6%
Final simplification74.3%
(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 42.8%
sin-sum69.3%
associate--l+69.3%
Applied egg-rr69.3%
+-commutative69.3%
associate-+l-99.4%
*-commutative99.4%
*-rgt-identity99.4%
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)) (* (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 42.8%
sin-sum69.3%
associate--l+69.3%
Applied egg-rr69.3%
+-commutative69.3%
associate-+l-99.4%
*-commutative99.4%
*-rgt-identity99.4%
distribute-lft-out--99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (x eps) :precision binary64 (* (cos (* 0.5 (+ eps (+ x x)))) (* 2.0 (sin (* eps 0.5)))))
double code(double x, double eps) {
return cos((0.5 * (eps + (x + x)))) * (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 + x)))) * (2.0d0 * sin((eps * 0.5d0)))
end function
public static double code(double x, double eps) {
return Math.cos((0.5 * (eps + (x + x)))) * (2.0 * Math.sin((eps * 0.5)));
}
def code(x, eps): return math.cos((0.5 * (eps + (x + x)))) * (2.0 * math.sin((eps * 0.5)))
function code(x, eps) return Float64(cos(Float64(0.5 * Float64(eps + Float64(x + x)))) * Float64(2.0 * sin(Float64(eps * 0.5)))) end
function tmp = code(x, eps) tmp = cos((0.5 * (eps + (x + x)))) * (2.0 * sin((eps * 0.5))); end
code[x_, eps_] := N[(N[Cos[N[(0.5 * N[(eps + N[(x + x), $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 + \left(x + x\right)\right)\right) \cdot \left(2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)
\end{array}
Initial program 42.8%
add-sqr-sqrt20.5%
sqrt-unprod20.4%
pow220.4%
Applied egg-rr20.4%
sqrt-pow142.8%
metadata-eval42.8%
pow142.8%
diff-sin42.0%
div-inv42.0%
+-commutative42.0%
associate--l+73.5%
metadata-eval73.5%
div-inv73.5%
associate-+l+73.5%
metadata-eval73.5%
Applied egg-rr73.5%
associate-*r*73.5%
+-inverses73.5%
+-rgt-identity73.5%
*-commutative73.5%
*-commutative73.5%
+-commutative73.5%
associate-+l+73.5%
Simplified73.5%
Final simplification73.5%
(FPCore (x eps) :precision binary64 (if (<= eps -0.0044) (sin eps) (if (<= eps 1.8e-5) (* (cos x) eps) (- (sin (+ x eps)) (sin x)))))
double code(double x, double eps) {
double tmp;
if (eps <= -0.0044) {
tmp = sin(eps);
} else if (eps <= 1.8e-5) {
tmp = cos(x) * eps;
} else {
tmp = sin((x + eps)) - sin(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.0044d0)) then
tmp = sin(eps)
else if (eps <= 1.8d-5) then
tmp = cos(x) * eps
else
tmp = sin((x + eps)) - sin(x)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= -0.0044) {
tmp = Math.sin(eps);
} else if (eps <= 1.8e-5) {
tmp = Math.cos(x) * eps;
} else {
tmp = Math.sin((x + eps)) - Math.sin(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -0.0044: tmp = math.sin(eps) elif eps <= 1.8e-5: tmp = math.cos(x) * eps else: tmp = math.sin((x + eps)) - math.sin(x) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -0.0044) tmp = sin(eps); elseif (eps <= 1.8e-5) tmp = Float64(cos(x) * eps); else tmp = Float64(sin(Float64(x + eps)) - sin(x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -0.0044) tmp = sin(eps); elseif (eps <= 1.8e-5) tmp = cos(x) * eps; else tmp = sin((x + eps)) - sin(x); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -0.0044], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 1.8e-5], N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision], N[(N[Sin[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.0044:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 1.8 \cdot 10^{-5}:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\sin \left(x + \varepsilon\right) - \sin x\\
\end{array}
\end{array}
if eps < -0.00440000000000000027Initial program 45.9%
Taylor expanded in x around 0 47.6%
if -0.00440000000000000027 < eps < 1.80000000000000005e-5Initial program 33.9%
Taylor expanded in eps around 0 98.8%
if 1.80000000000000005e-5 < eps Initial program 55.4%
Final simplification73.7%
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.0044) (not (<= eps 1.9e-5))) (sin eps) (* (cos x) eps)))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.0044) || !(eps <= 1.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 <= (-0.0044d0)) .or. (.not. (eps <= 1.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 <= -0.0044) || !(eps <= 1.9e-5)) {
tmp = Math.sin(eps);
} else {
tmp = Math.cos(x) * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.0044) or not (eps <= 1.9e-5): tmp = math.sin(eps) else: tmp = math.cos(x) * eps return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.0044) || !(eps <= 1.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 <= -0.0044) || ~((eps <= 1.9e-5))) tmp = sin(eps); else tmp = cos(x) * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.0044], N[Not[LessEqual[eps, 1.9e-5]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.0044 \lor \neg \left(\varepsilon \leq 1.9 \cdot 10^{-5}\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\end{array}
\end{array}
if eps < -0.00440000000000000027 or 1.9000000000000001e-5 < eps Initial program 50.7%
Taylor expanded in x around 0 51.5%
if -0.00440000000000000027 < eps < 1.9000000000000001e-5Initial program 33.9%
Taylor expanded in eps around 0 98.8%
Final simplification73.6%
(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.8%
Taylor expanded in x around 0 54.2%
Final simplification54.2%
(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.8%
Taylor expanded in x around 0 54.2%
Taylor expanded in eps around 0 29.1%
Final simplification29.1%
(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 2023322
(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)))