
(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 (+ (* (cos x) (sin eps)) (* (sin x) (+ (cos eps) -1.0))))
double code(double x, double eps) {
return (cos(x) * sin(eps)) + (sin(x) * (cos(eps) + -1.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (cos(x) * sin(eps)) + (sin(x) * (cos(eps) + (-1.0d0)))
end function
public static double code(double x, double eps) {
return (Math.cos(x) * Math.sin(eps)) + (Math.sin(x) * (Math.cos(eps) + -1.0));
}
def code(x, eps): return (math.cos(x) * math.sin(eps)) + (math.sin(x) * (math.cos(eps) + -1.0))
function code(x, eps) return Float64(Float64(cos(x) * sin(eps)) + Float64(sin(x) * Float64(cos(eps) + -1.0))) end
function tmp = code(x, eps) tmp = (cos(x) * sin(eps)) + (sin(x) * (cos(eps) + -1.0)); end
code[x_, eps_] := N[(N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos x \cdot \sin \varepsilon + \sin x \cdot \left(\cos \varepsilon + -1\right)
\end{array}
Initial program 40.9%
sin-sum65.8%
associate--l+65.8%
Applied egg-rr65.8%
Taylor expanded in x around inf 65.8%
associate--l+99.4%
fma-def99.4%
*-commutative99.4%
*-rgt-identity99.4%
distribute-lft-out--99.4%
sub-neg99.4%
metadata-eval99.4%
+-commutative99.4%
Simplified99.4%
Taylor expanded in x around inf 99.4%
Final simplification99.4%
(FPCore (x eps)
:precision binary64
(let* ((t_0 (- (sin (+ x eps)) (sin x))))
(if (or (<= t_0 -0.002) (not (<= t_0 1e-13)))
t_0
(* (sin (* eps 0.5)) (* (cos x) 2.0)))))
double code(double x, double eps) {
double t_0 = sin((x + eps)) - sin(x);
double tmp;
if ((t_0 <= -0.002) || !(t_0 <= 1e-13)) {
tmp = t_0;
} else {
tmp = sin((eps * 0.5)) * (cos(x) * 2.0);
}
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.002d0)) .or. (.not. (t_0 <= 1d-13))) then
tmp = t_0
else
tmp = sin((eps * 0.5d0)) * (cos(x) * 2.0d0)
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.002) || !(t_0 <= 1e-13)) {
tmp = t_0;
} else {
tmp = Math.sin((eps * 0.5)) * (Math.cos(x) * 2.0);
}
return tmp;
}
def code(x, eps): t_0 = math.sin((x + eps)) - math.sin(x) tmp = 0 if (t_0 <= -0.002) or not (t_0 <= 1e-13): tmp = t_0 else: tmp = math.sin((eps * 0.5)) * (math.cos(x) * 2.0) return tmp
function code(x, eps) t_0 = Float64(sin(Float64(x + eps)) - sin(x)) tmp = 0.0 if ((t_0 <= -0.002) || !(t_0 <= 1e-13)) tmp = t_0; else tmp = Float64(sin(Float64(eps * 0.5)) * Float64(cos(x) * 2.0)); end return tmp end
function tmp_2 = code(x, eps) t_0 = sin((x + eps)) - sin(x); tmp = 0.0; if ((t_0 <= -0.002) || ~((t_0 <= 1e-13))) tmp = t_0; else tmp = sin((eps * 0.5)) * (cos(x) * 2.0); 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[Or[LessEqual[t$95$0, -0.002], N[Not[LessEqual[t$95$0, 1e-13]], $MachinePrecision]], t$95$0, N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[(N[Cos[x], $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(x + \varepsilon\right) - \sin x\\
\mathbf{if}\;t_0 \leq -0.002 \lor \neg \left(t_0 \leq 10^{-13}\right):\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\sin \left(\varepsilon \cdot 0.5\right) \cdot \left(\cos x \cdot 2\right)\\
\end{array}
\end{array}
if (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < -2e-3 or 1e-13 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) Initial program 65.0%
if -2e-3 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < 1e-13Initial program 25.2%
diff-sin25.2%
div-inv25.2%
metadata-eval25.2%
div-inv25.2%
+-commutative25.2%
metadata-eval25.2%
Applied egg-rr25.2%
associate-*r*25.2%
*-commutative25.2%
associate-*l*25.2%
+-commutative25.2%
associate--l+83.6%
+-inverses83.6%
*-commutative83.6%
associate-+r+83.6%
+-commutative83.6%
Simplified83.6%
Taylor expanded in eps around 0 83.5%
Final simplification76.2%
(FPCore (x eps) :precision binary64 (* 2.0 (* (sin (* eps 0.5)) (cos (* 0.5 (+ eps (* x 2.0)))))))
double code(double x, double eps) {
return 2.0 * (sin((eps * 0.5)) * cos((0.5 * (eps + (x * 2.0)))));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 2.0d0 * (sin((eps * 0.5d0)) * cos((0.5d0 * (eps + (x * 2.0d0)))))
end function
public static double code(double x, double eps) {
return 2.0 * (Math.sin((eps * 0.5)) * Math.cos((0.5 * (eps + (x * 2.0)))));
}
def code(x, eps): return 2.0 * (math.sin((eps * 0.5)) * math.cos((0.5 * (eps + (x * 2.0)))))
function code(x, eps) return Float64(2.0 * Float64(sin(Float64(eps * 0.5)) * cos(Float64(0.5 * Float64(eps + Float64(x * 2.0)))))) end
function tmp = code(x, eps) tmp = 2.0 * (sin((eps * 0.5)) * cos((0.5 * (eps + (x * 2.0))))); end
code[x_, eps_] := N[(2.0 * N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(0.5 * N[(eps + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(\sin \left(\varepsilon \cdot 0.5\right) \cdot \cos \left(0.5 \cdot \left(\varepsilon + x \cdot 2\right)\right)\right)
\end{array}
Initial program 40.9%
diff-sin40.5%
div-inv40.5%
metadata-eval40.5%
div-inv40.5%
+-commutative40.5%
metadata-eval40.5%
Applied egg-rr40.5%
associate-*r*40.5%
*-commutative40.5%
associate-*l*40.5%
+-commutative40.5%
associate--l+75.7%
+-inverses75.7%
*-commutative75.7%
associate-+r+75.7%
+-commutative75.7%
Simplified75.7%
Taylor expanded in eps around inf 75.7%
Final simplification75.7%
(FPCore (x eps) :precision binary64 (* 2.0 (* (sin (* eps 0.5)) (cos (* 0.5 (+ x (+ x eps)))))))
double code(double x, double eps) {
return 2.0 * (sin((eps * 0.5)) * cos((0.5 * (x + (x + eps)))));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 2.0d0 * (sin((eps * 0.5d0)) * cos((0.5d0 * (x + (x + eps)))))
end function
public static double code(double x, double eps) {
return 2.0 * (Math.sin((eps * 0.5)) * Math.cos((0.5 * (x + (x + eps)))));
}
def code(x, eps): return 2.0 * (math.sin((eps * 0.5)) * math.cos((0.5 * (x + (x + eps)))))
function code(x, eps) return Float64(2.0 * Float64(sin(Float64(eps * 0.5)) * cos(Float64(0.5 * Float64(x + Float64(x + eps)))))) end
function tmp = code(x, eps) tmp = 2.0 * (sin((eps * 0.5)) * cos((0.5 * (x + (x + eps))))); end
code[x_, eps_] := N[(2.0 * N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(0.5 * N[(x + N[(x + eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(\sin \left(\varepsilon \cdot 0.5\right) \cdot \cos \left(0.5 \cdot \left(x + \left(x + \varepsilon\right)\right)\right)\right)
\end{array}
Initial program 40.9%
diff-sin40.5%
div-inv40.5%
metadata-eval40.5%
div-inv40.5%
+-commutative40.5%
metadata-eval40.5%
Applied egg-rr40.5%
associate-*r*40.5%
*-commutative40.5%
associate-*l*40.5%
+-commutative40.5%
associate--l+75.7%
+-inverses75.7%
*-commutative75.7%
associate-+r+75.7%
+-commutative75.7%
Simplified75.7%
Taylor expanded in eps around inf 75.7%
count-275.7%
count-275.7%
metadata-eval75.7%
cancel-sign-sub-inv75.7%
*-commutative75.7%
cancel-sign-sub-inv75.7%
metadata-eval75.7%
+-commutative75.7%
*-lft-identity75.7%
metadata-eval75.7%
cancel-sign-sub-inv75.7%
Simplified75.7%
Final simplification75.7%
(FPCore (x eps) :precision binary64 (if (<= eps -1.7e-5) (sin eps) (if (<= eps 0.00105) (* (cos x) eps) (sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -1.7e-5) {
tmp = sin(eps);
} else if (eps <= 0.00105) {
tmp = cos(x) * eps;
} else {
tmp = sin(eps);
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (eps <= (-1.7d-5)) then
tmp = sin(eps)
else if (eps <= 0.00105d0) then
tmp = cos(x) * eps
else
tmp = sin(eps)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= -1.7e-5) {
tmp = Math.sin(eps);
} else if (eps <= 0.00105) {
tmp = Math.cos(x) * eps;
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -1.7e-5: tmp = math.sin(eps) elif eps <= 0.00105: tmp = math.cos(x) * eps else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -1.7e-5) tmp = sin(eps); elseif (eps <= 0.00105) tmp = Float64(cos(x) * eps); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -1.7e-5) tmp = sin(eps); elseif (eps <= 0.00105) tmp = cos(x) * eps; else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -1.7e-5], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 0.00105], N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -1.7 \cdot 10^{-5}:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 0.00105:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -1.7e-5 or 0.00104999999999999994 < eps Initial program 50.4%
Taylor expanded in x around 0 51.6%
if -1.7e-5 < eps < 0.00104999999999999994Initial program 32.0%
Taylor expanded in eps around 0 98.5%
Final simplification75.8%
(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 40.9%
Taylor expanded in x around 0 53.1%
Final simplification53.1%
(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 40.9%
Taylor expanded in eps around 0 52.3%
Taylor expanded in x around 0 29.5%
Final simplification29.5%
(FPCore (x eps) :precision binary64 (* 2.0 (* (cos (+ x (/ eps 2.0))) (sin (/ eps 2.0)))))
double code(double x, double eps) {
return 2.0 * (cos((x + (eps / 2.0))) * sin((eps / 2.0)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 2.0d0 * (cos((x + (eps / 2.0d0))) * sin((eps / 2.0d0)))
end function
public static double code(double x, double eps) {
return 2.0 * (Math.cos((x + (eps / 2.0))) * Math.sin((eps / 2.0)));
}
def code(x, eps): return 2.0 * (math.cos((x + (eps / 2.0))) * math.sin((eps / 2.0)))
function code(x, eps) return Float64(2.0 * Float64(cos(Float64(x + Float64(eps / 2.0))) * sin(Float64(eps / 2.0)))) end
function tmp = code(x, eps) tmp = 2.0 * (cos((x + (eps / 2.0))) * sin((eps / 2.0))); end
code[x_, eps_] := N[(2.0 * N[(N[Cos[N[(x + N[(eps / 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(eps / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(\cos \left(x + \frac{\varepsilon}{2}\right) \cdot \sin \left(\frac{\varepsilon}{2}\right)\right)
\end{array}
herbie shell --seed 2023279
(FPCore (x eps)
:name "2sin (example 3.3)"
:precision binary64
:herbie-target
(* 2.0 (* (cos (+ x (/ eps 2.0))) (sin (/ eps 2.0))))
(- (sin (+ x eps)) (sin x)))