
(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 (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.4%
sin-sum70.9%
associate--l+70.8%
Applied egg-rr70.8%
+-commutative70.8%
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.5%
distribute-rgt-neg-in99.5%
sub-neg99.5%
+-commutative99.5%
distribute-neg-in99.5%
remove-double-neg99.5%
metadata-eval99.5%
Simplified99.5%
Final simplification99.5%
(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.4%
sin-sum70.9%
associate--l+70.8%
Applied egg-rr70.8%
+-commutative70.8%
associate-+l-99.4%
*-commutative99.4%
*-rgt-identity99.4%
distribute-lft-out--99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (x eps)
:precision binary64
(if (<= eps -8.5)
(sin eps)
(if (<= eps 0.000185)
(* (cos x) eps)
(* 2.0 (* (sin (* 0.5 (+ x (+ x eps)))) (cos (* eps 0.5)))))))
double code(double x, double eps) {
double tmp;
if (eps <= -8.5) {
tmp = sin(eps);
} else if (eps <= 0.000185) {
tmp = cos(x) * eps;
} else {
tmp = 2.0 * (sin((0.5 * (x + (x + eps)))) * cos((eps * 0.5)));
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (eps <= (-8.5d0)) then
tmp = sin(eps)
else if (eps <= 0.000185d0) then
tmp = cos(x) * eps
else
tmp = 2.0d0 * (sin((0.5d0 * (x + (x + eps)))) * cos((eps * 0.5d0)))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= -8.5) {
tmp = Math.sin(eps);
} else if (eps <= 0.000185) {
tmp = Math.cos(x) * eps;
} else {
tmp = 2.0 * (Math.sin((0.5 * (x + (x + eps)))) * Math.cos((eps * 0.5)));
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -8.5: tmp = math.sin(eps) elif eps <= 0.000185: tmp = math.cos(x) * eps else: tmp = 2.0 * (math.sin((0.5 * (x + (x + eps)))) * math.cos((eps * 0.5))) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -8.5) tmp = sin(eps); elseif (eps <= 0.000185) tmp = Float64(cos(x) * eps); else tmp = Float64(2.0 * Float64(sin(Float64(0.5 * Float64(x + Float64(x + eps)))) * cos(Float64(eps * 0.5)))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -8.5) tmp = sin(eps); elseif (eps <= 0.000185) tmp = cos(x) * eps; else tmp = 2.0 * (sin((0.5 * (x + (x + eps)))) * cos((eps * 0.5))); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -8.5], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 0.000185], N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision], N[(2.0 * N[(N[Sin[N[(0.5 * N[(x + N[(x + eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -8.5:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 0.000185:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \left(\sin \left(0.5 \cdot \left(x + \left(x + \varepsilon\right)\right)\right) \cdot \cos \left(\varepsilon \cdot 0.5\right)\right)\\
\end{array}
\end{array}
if eps < -8.5Initial program 49.3%
Taylor expanded in x around 0 53.0%
if -8.5 < eps < 1.85e-4Initial program 34.8%
Taylor expanded in eps around 0 98.1%
if 1.85e-4 < eps Initial program 48.4%
sin-sum99.4%
associate--l+99.2%
Applied egg-rr99.2%
+-commutative99.2%
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.5%
distribute-rgt-neg-in99.5%
sub-neg99.5%
+-commutative99.5%
distribute-neg-in99.5%
remove-double-neg99.5%
metadata-eval99.5%
Simplified99.5%
distribute-rgt-in99.5%
neg-mul-199.5%
Applied egg-rr99.5%
fma-udef99.4%
associate-+r+99.4%
*-commutative99.4%
sin-sum48.4%
+-commutative48.4%
add-sqr-sqrt26.5%
sqrt-unprod47.7%
sqr-neg47.7%
sqrt-unprod21.3%
add-sqr-sqrt46.6%
sum-sin47.9%
div-inv47.9%
associate-+l+47.9%
metadata-eval47.9%
div-inv47.9%
Applied egg-rr49.9%
*-commutative49.9%
+-inverses49.9%
+-rgt-identity49.9%
Simplified49.9%
Final simplification72.7%
(FPCore (x eps) :precision binary64 (* (sin (* eps 0.5)) (* (cos (* 0.5 (+ x (+ x eps)))) 2.0)))
double code(double x, double eps) {
return sin((eps * 0.5)) * (cos((0.5 * (x + (x + eps)))) * 2.0);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin((eps * 0.5d0)) * (cos((0.5d0 * (x + (x + eps)))) * 2.0d0)
end function
public static double code(double x, double eps) {
return Math.sin((eps * 0.5)) * (Math.cos((0.5 * (x + (x + eps)))) * 2.0);
}
def code(x, eps): return math.sin((eps * 0.5)) * (math.cos((0.5 * (x + (x + eps)))) * 2.0)
function code(x, eps) return Float64(sin(Float64(eps * 0.5)) * Float64(cos(Float64(0.5 * Float64(x + Float64(x + eps)))) * 2.0)) end
function tmp = code(x, eps) tmp = sin((eps * 0.5)) * (cos((0.5 * (x + (x + eps)))) * 2.0); end
code[x_, eps_] := N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[(N[Cos[N[(0.5 * N[(x + N[(x + eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \left(\varepsilon \cdot 0.5\right) \cdot \left(\cos \left(0.5 \cdot \left(x + \left(x + \varepsilon\right)\right)\right) \cdot 2\right)
\end{array}
Initial program 42.4%
sin-sum70.9%
associate--l+70.8%
Applied egg-rr70.8%
+-commutative70.8%
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.5%
distribute-rgt-neg-in99.5%
sub-neg99.5%
+-commutative99.5%
distribute-neg-in99.5%
remove-double-neg99.5%
metadata-eval99.5%
Simplified99.5%
distribute-rgt-in99.5%
neg-mul-199.5%
Applied egg-rr99.5%
fma-udef99.4%
associate-+r+70.9%
*-commutative70.9%
sin-sum42.4%
+-commutative42.4%
sub-neg42.4%
diff-sin42.0%
div-inv42.0%
+-commutative42.0%
associate--l+72.1%
metadata-eval72.1%
div-inv72.1%
associate-+l+72.1%
metadata-eval72.1%
Applied egg-rr72.1%
*-commutative72.1%
associate-*l*72.1%
+-inverses72.1%
+-rgt-identity72.1%
*-commutative72.1%
Simplified72.1%
Final simplification72.1%
(FPCore (x eps) :precision binary64 (if (<= eps -8.5) (sin eps) (if (<= eps 4000.0) (* (cos x) eps) (- (sin (+ x eps)) (sin x)))))
double code(double x, double eps) {
double tmp;
if (eps <= -8.5) {
tmp = sin(eps);
} else if (eps <= 4000.0) {
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 <= (-8.5d0)) then
tmp = sin(eps)
else if (eps <= 4000.0d0) 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 <= -8.5) {
tmp = Math.sin(eps);
} else if (eps <= 4000.0) {
tmp = Math.cos(x) * eps;
} else {
tmp = Math.sin((x + eps)) - Math.sin(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -8.5: tmp = math.sin(eps) elif eps <= 4000.0: 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 <= -8.5) tmp = sin(eps); elseif (eps <= 4000.0) 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 <= -8.5) tmp = sin(eps); elseif (eps <= 4000.0) tmp = cos(x) * eps; else tmp = sin((x + eps)) - sin(x); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -8.5], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 4000.0], 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 -8.5:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 4000:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\sin \left(x + \varepsilon\right) - \sin x\\
\end{array}
\end{array}
if eps < -8.5Initial program 49.3%
Taylor expanded in x around 0 53.0%
if -8.5 < eps < 4e3Initial program 34.0%
Taylor expanded in eps around 0 95.7%
if 4e3 < eps Initial program 50.3%
Final simplification72.3%
(FPCore (x eps) :precision binary64 (if (or (<= eps -8.5) (not (<= eps 4000.0))) (sin eps) (* (cos x) eps)))
double code(double x, double eps) {
double tmp;
if ((eps <= -8.5) || !(eps <= 4000.0)) {
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 <= (-8.5d0)) .or. (.not. (eps <= 4000.0d0))) 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 <= -8.5) || !(eps <= 4000.0)) {
tmp = Math.sin(eps);
} else {
tmp = Math.cos(x) * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -8.5) or not (eps <= 4000.0): tmp = math.sin(eps) else: tmp = math.cos(x) * eps return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -8.5) || !(eps <= 4000.0)) tmp = sin(eps); else tmp = Float64(cos(x) * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -8.5) || ~((eps <= 4000.0))) tmp = sin(eps); else tmp = cos(x) * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -8.5], N[Not[LessEqual[eps, 4000.0]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -8.5 \lor \neg \left(\varepsilon \leq 4000\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\end{array}
\end{array}
if eps < -8.5 or 4e3 < eps Initial program 49.8%
Taylor expanded in x around 0 51.5%
if -8.5 < eps < 4e3Initial program 34.0%
Taylor expanded in eps around 0 95.7%
Final simplification72.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 42.4%
Taylor expanded in x around 0 54.7%
Final simplification54.7%
(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.4%
Taylor expanded in eps around 0 46.8%
Taylor expanded in x around 0 29.2%
Final simplification29.2%
(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 2023320
(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)))