
(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 10 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 eps) -1.0) (sin x) (* (sin eps) (cos x))))
double code(double x, double eps) {
return fma((cos(eps) + -1.0), sin(x), (sin(eps) * cos(x)));
}
function code(x, eps) return fma(Float64(cos(eps) + -1.0), sin(x), Float64(sin(eps) * cos(x))) end
code[x_, eps_] := N[(N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision] * N[Sin[x], $MachinePrecision] + N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\cos \varepsilon + -1, \sin x, \sin \varepsilon \cdot \cos x\right)
\end{array}
Initial program 41.6%
sin-sum67.0%
associate--l+67.0%
Applied egg-rr67.0%
+-commutative67.0%
sub-neg67.0%
associate-+l+99.4%
*-commutative99.4%
neg-mul-199.4%
*-commutative99.4%
distribute-rgt-out99.4%
+-commutative99.4%
Simplified99.4%
+-commutative99.4%
*-commutative99.4%
*-commutative99.4%
fma-def99.4%
*-commutative99.4%
Applied egg-rr99.4%
Final simplification99.4%
(FPCore (x eps) :precision binary64 (fma (sin eps) (cos x) (* (+ (cos eps) -1.0) (sin x))))
double code(double x, double eps) {
return fma(sin(eps), cos(x), ((cos(eps) + -1.0) * sin(x)));
}
function code(x, eps) return fma(sin(eps), cos(x), Float64(Float64(cos(eps) + -1.0) * sin(x))) end
code[x_, eps_] := N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision] + N[(N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sin \varepsilon, \cos x, \left(\cos \varepsilon + -1\right) \cdot \sin x\right)
\end{array}
Initial program 41.6%
sin-sum67.0%
associate--l+67.0%
Applied egg-rr67.0%
+-commutative67.0%
sub-neg67.0%
associate-+l+99.4%
*-commutative99.4%
neg-mul-199.4%
*-commutative99.4%
distribute-rgt-out99.4%
+-commutative99.4%
Simplified99.4%
fma-def99.4%
*-commutative99.4%
Applied egg-rr99.4%
Final simplification99.4%
(FPCore (x eps) :precision binary64 (+ (* (sin eps) (cos x)) (* (+ (cos eps) -1.0) (sin x))))
double code(double x, double eps) {
return (sin(eps) * cos(x)) + ((cos(eps) + -1.0) * sin(x));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (sin(eps) * cos(x)) + ((cos(eps) + (-1.0d0)) * sin(x))
end function
public static double code(double x, double eps) {
return (Math.sin(eps) * Math.cos(x)) + ((Math.cos(eps) + -1.0) * Math.sin(x));
}
def code(x, eps): return (math.sin(eps) * math.cos(x)) + ((math.cos(eps) + -1.0) * math.sin(x))
function code(x, eps) return Float64(Float64(sin(eps) * cos(x)) + Float64(Float64(cos(eps) + -1.0) * sin(x))) end
function tmp = code(x, eps) tmp = (sin(eps) * cos(x)) + ((cos(eps) + -1.0) * sin(x)); end
code[x_, eps_] := N[(N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \cos x + \left(\cos \varepsilon + -1\right) \cdot \sin x
\end{array}
Initial program 41.6%
sin-sum67.0%
associate--l+67.0%
Applied egg-rr67.0%
+-commutative67.0%
sub-neg67.0%
associate-+l+99.4%
*-commutative99.4%
neg-mul-199.4%
*-commutative99.4%
distribute-rgt-out99.4%
+-commutative99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.0012) (not (<= eps 235000000000.0))) (- (sin eps) (sin x)) (* eps (+ (cos x) (* eps (* (sin x) -0.5))))))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.0012) || !(eps <= 235000000000.0)) {
tmp = sin(eps) - sin(x);
} else {
tmp = eps * (cos(x) + (eps * (sin(x) * -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 <= (-0.0012d0)) .or. (.not. (eps <= 235000000000.0d0))) then
tmp = sin(eps) - sin(x)
else
tmp = eps * (cos(x) + (eps * (sin(x) * (-0.5d0))))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((eps <= -0.0012) || !(eps <= 235000000000.0)) {
tmp = Math.sin(eps) - Math.sin(x);
} else {
tmp = eps * (Math.cos(x) + (eps * (Math.sin(x) * -0.5)));
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.0012) or not (eps <= 235000000000.0): tmp = math.sin(eps) - math.sin(x) else: tmp = eps * (math.cos(x) + (eps * (math.sin(x) * -0.5))) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.0012) || !(eps <= 235000000000.0)) tmp = Float64(sin(eps) - sin(x)); else tmp = Float64(eps * Float64(cos(x) + Float64(eps * Float64(sin(x) * -0.5)))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -0.0012) || ~((eps <= 235000000000.0))) tmp = sin(eps) - sin(x); else tmp = eps * (cos(x) + (eps * (sin(x) * -0.5))); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.0012], N[Not[LessEqual[eps, 235000000000.0]], $MachinePrecision]], N[(N[Sin[eps], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision], N[(eps * N[(N[Cos[x], $MachinePrecision] + N[(eps * N[(N[Sin[x], $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.0012 \lor \neg \left(\varepsilon \leq 235000000000\right):\\
\;\;\;\;\sin \varepsilon - \sin x\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \left(\cos x + \varepsilon \cdot \left(\sin x \cdot -0.5\right)\right)\\
\end{array}
\end{array}
if eps < -0.00119999999999999989 or 2.35e11 < eps Initial program 52.2%
add-cube-cbrt51.5%
pow351.5%
Applied egg-rr51.5%
Taylor expanded in x around 0 26.7%
Taylor expanded in eps around inf 54.8%
pow-base-154.8%
*-lft-identity54.8%
Simplified54.8%
if -0.00119999999999999989 < eps < 2.35e11Initial program 30.8%
sin-sum34.0%
associate--l+34.0%
Applied egg-rr34.0%
+-commutative34.0%
sub-neg34.0%
associate-+l+99.3%
*-commutative99.3%
neg-mul-199.3%
*-commutative99.3%
distribute-rgt-out99.3%
+-commutative99.3%
Simplified99.3%
Taylor expanded in eps around 0 98.5%
*-commutative98.5%
associate-*l*98.5%
unpow298.5%
associate-*r*98.5%
*-commutative98.5%
distribute-rgt-out98.5%
*-commutative98.5%
Simplified98.5%
Final simplification76.5%
(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 41.6%
diff-sin41.0%
div-inv41.0%
metadata-eval41.0%
div-inv41.0%
+-commutative41.0%
metadata-eval41.0%
Applied egg-rr41.0%
associate-*r*41.0%
*-commutative41.0%
*-commutative41.0%
associate-+r+41.0%
+-commutative41.0%
*-commutative41.0%
+-commutative41.0%
associate--l+75.6%
+-inverses75.6%
distribute-lft-in75.6%
metadata-eval75.6%
Simplified75.6%
Final simplification75.6%
(FPCore (x eps) :precision binary64 (if (or (<= eps -1.2e-5) (not (<= eps 0.18))) (- (sin eps) (sin x)) (* eps (cos x))))
double code(double x, double eps) {
double tmp;
if ((eps <= -1.2e-5) || !(eps <= 0.18)) {
tmp = sin(eps) - sin(x);
} else {
tmp = eps * cos(x);
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if ((eps <= (-1.2d-5)) .or. (.not. (eps <= 0.18d0))) then
tmp = sin(eps) - sin(x)
else
tmp = eps * cos(x)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((eps <= -1.2e-5) || !(eps <= 0.18)) {
tmp = Math.sin(eps) - Math.sin(x);
} else {
tmp = eps * Math.cos(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -1.2e-5) or not (eps <= 0.18): tmp = math.sin(eps) - math.sin(x) else: tmp = eps * math.cos(x) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -1.2e-5) || !(eps <= 0.18)) tmp = Float64(sin(eps) - sin(x)); else tmp = Float64(eps * cos(x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -1.2e-5) || ~((eps <= 0.18))) tmp = sin(eps) - sin(x); else tmp = eps * cos(x); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -1.2e-5], N[Not[LessEqual[eps, 0.18]], $MachinePrecision]], N[(N[Sin[eps], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -1.2 \cdot 10^{-5} \lor \neg \left(\varepsilon \leq 0.18\right):\\
\;\;\;\;\sin \varepsilon - \sin x\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\end{array}
\end{array}
if eps < -1.2e-5 or 0.17999999999999999 < eps Initial program 51.8%
add-cube-cbrt51.1%
pow351.1%
Applied egg-rr51.1%
Taylor expanded in x around 0 26.5%
Taylor expanded in eps around inf 54.4%
pow-base-154.4%
*-lft-identity54.4%
Simplified54.4%
if -1.2e-5 < eps < 0.17999999999999999Initial program 31.0%
Taylor expanded in eps around 0 98.5%
Final simplification76.1%
(FPCore (x eps) :precision binary64 (if (<= eps -9.5e-5) (sin eps) (if (<= eps 0.18) (* eps (cos x)) (sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -9.5e-5) {
tmp = sin(eps);
} else if (eps <= 0.18) {
tmp = eps * cos(x);
} 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 <= (-9.5d-5)) then
tmp = sin(eps)
else if (eps <= 0.18d0) then
tmp = eps * cos(x)
else
tmp = sin(eps)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= -9.5e-5) {
tmp = Math.sin(eps);
} else if (eps <= 0.18) {
tmp = eps * Math.cos(x);
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -9.5e-5: tmp = math.sin(eps) elif eps <= 0.18: tmp = eps * math.cos(x) else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -9.5e-5) tmp = sin(eps); elseif (eps <= 0.18) tmp = Float64(eps * cos(x)); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -9.5e-5) tmp = sin(eps); elseif (eps <= 0.18) tmp = eps * cos(x); else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -9.5e-5], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 0.18], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -9.5 \cdot 10^{-5}:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 0.18:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -9.5000000000000005e-5 or 0.17999999999999999 < eps Initial program 51.8%
Taylor expanded in x around 0 53.2%
if -9.5000000000000005e-5 < eps < 0.17999999999999999Initial program 31.0%
Taylor expanded in eps around 0 98.5%
Final simplification75.5%
(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.6%
Taylor expanded in x around 0 55.9%
Final simplification55.9%
(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.6%
add-cube-cbrt40.5%
pow340.5%
Applied egg-rr40.5%
Taylor expanded in eps around 0 4.2%
pow-base-14.2%
*-lft-identity4.2%
+-inverses4.2%
Simplified4.2%
Final simplification4.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 41.6%
Taylor expanded in eps around 0 50.6%
+-commutative50.6%
fma-def50.6%
*-commutative50.6%
unpow250.6%
associate-*r*50.7%
Simplified50.7%
Taylor expanded in x around 0 30.6%
Final simplification30.6%
(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 2023271
(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)))