
(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 (* (sin eps) (- (cos x) (* (tan (* eps 0.5)) (sin x)))))
double code(double x, double eps) {
return sin(eps) * (cos(x) - (tan((eps * 0.5)) * sin(x)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin(eps) * (cos(x) - (tan((eps * 0.5d0)) * sin(x)))
end function
public static double code(double x, double eps) {
return Math.sin(eps) * (Math.cos(x) - (Math.tan((eps * 0.5)) * Math.sin(x)));
}
def code(x, eps): return math.sin(eps) * (math.cos(x) - (math.tan((eps * 0.5)) * math.sin(x)))
function code(x, eps) return Float64(sin(eps) * Float64(cos(x) - Float64(tan(Float64(eps * 0.5)) * sin(x)))) end
function tmp = code(x, eps) tmp = sin(eps) * (cos(x) - (tan((eps * 0.5)) * sin(x))); end
code[x_, eps_] := N[(N[Sin[eps], $MachinePrecision] * N[(N[Cos[x], $MachinePrecision] - N[(N[Tan[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \left(\cos x - \tan \left(\varepsilon \cdot 0.5\right) \cdot \sin x\right)
\end{array}
Initial program 44.8%
sin-sum71.0%
associate--l+71.0%
Applied egg-rr71.0%
+-commutative71.0%
associate-+l-99.3%
*-commutative99.3%
*-rgt-identity99.3%
distribute-lft-out--99.4%
Simplified99.4%
flip--99.3%
div-inv99.3%
metadata-eval99.3%
1-sub-cos99.6%
pow299.6%
Applied egg-rr99.6%
associate-*r/99.6%
*-rgt-identity99.6%
Simplified99.6%
Taylor expanded in x around inf 99.6%
*-commutative99.6%
*-lft-identity99.6%
times-frac99.6%
/-rgt-identity99.6%
unpow299.6%
associate-*r/99.6%
hang-0p-tan99.7%
Simplified99.7%
sub-neg99.7%
*-commutative99.7%
distribute-rgt-neg-in99.7%
div-inv99.7%
metadata-eval99.7%
Applied egg-rr99.7%
associate-*l*99.7%
distribute-lft-out99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (x eps)
:precision binary64
(let* ((t_0 (sin (+ eps x))) (t_1 (- t_0 (sin x))))
(if (<= t_1 -0.005)
(sin eps)
(if (<= t_1 2e-19) (* (cos x) (* 2.0 (sin (* eps 0.5)))) (fabs t_0)))))
double code(double x, double eps) {
double t_0 = sin((eps + x));
double t_1 = t_0 - sin(x);
double tmp;
if (t_1 <= -0.005) {
tmp = sin(eps);
} else if (t_1 <= 2e-19) {
tmp = cos(x) * (2.0 * sin((eps * 0.5)));
} else {
tmp = fabs(t_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) :: t_1
real(8) :: tmp
t_0 = sin((eps + x))
t_1 = t_0 - sin(x)
if (t_1 <= (-0.005d0)) then
tmp = sin(eps)
else if (t_1 <= 2d-19) then
tmp = cos(x) * (2.0d0 * sin((eps * 0.5d0)))
else
tmp = abs(t_0)
end if
code = tmp
end function
public static double code(double x, double eps) {
double t_0 = Math.sin((eps + x));
double t_1 = t_0 - Math.sin(x);
double tmp;
if (t_1 <= -0.005) {
tmp = Math.sin(eps);
} else if (t_1 <= 2e-19) {
tmp = Math.cos(x) * (2.0 * Math.sin((eps * 0.5)));
} else {
tmp = Math.abs(t_0);
}
return tmp;
}
def code(x, eps): t_0 = math.sin((eps + x)) t_1 = t_0 - math.sin(x) tmp = 0 if t_1 <= -0.005: tmp = math.sin(eps) elif t_1 <= 2e-19: tmp = math.cos(x) * (2.0 * math.sin((eps * 0.5))) else: tmp = math.fabs(t_0) return tmp
function code(x, eps) t_0 = sin(Float64(eps + x)) t_1 = Float64(t_0 - sin(x)) tmp = 0.0 if (t_1 <= -0.005) tmp = sin(eps); elseif (t_1 <= 2e-19) tmp = Float64(cos(x) * Float64(2.0 * sin(Float64(eps * 0.5)))); else tmp = abs(t_0); end return tmp end
function tmp_2 = code(x, eps) t_0 = sin((eps + x)); t_1 = t_0 - sin(x); tmp = 0.0; if (t_1 <= -0.005) tmp = sin(eps); elseif (t_1 <= 2e-19) tmp = cos(x) * (2.0 * sin((eps * 0.5))); else tmp = abs(t_0); end tmp_2 = tmp; end
code[x_, eps_] := Block[{t$95$0 = N[Sin[N[(eps + x), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 - N[Sin[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -0.005], N[Sin[eps], $MachinePrecision], If[LessEqual[t$95$1, 2e-19], N[(N[Cos[x], $MachinePrecision] * N[(2.0 * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Abs[t$95$0], $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(\varepsilon + x\right)\\
t_1 := t_0 - \sin x\\
\mathbf{if}\;t_1 \leq -0.005:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;t_1 \leq 2 \cdot 10^{-19}:\\
\;\;\;\;\cos x \cdot \left(2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\left|t_0\right|\\
\end{array}
\end{array}
if (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < -0.0050000000000000001Initial program 63.1%
Taylor expanded in x around 0 63.7%
if -0.0050000000000000001 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < 2e-19Initial program 29.6%
diff-sin29.6%
div-inv29.6%
associate--l+29.6%
metadata-eval29.6%
div-inv29.6%
+-commutative29.6%
associate-+l+29.6%
metadata-eval29.6%
Applied egg-rr29.6%
associate-*r*29.6%
*-commutative29.6%
*-commutative29.6%
+-commutative29.6%
count-229.6%
fma-def29.6%
sub-neg29.6%
mul-1-neg29.6%
+-commutative29.6%
associate-+r+81.2%
mul-1-neg81.2%
sub-neg81.2%
+-inverses81.2%
remove-double-neg81.2%
mul-1-neg81.2%
sub-neg81.2%
neg-sub081.2%
mul-1-neg81.2%
remove-double-neg81.2%
Simplified81.2%
Taylor expanded in eps around 0 81.2%
if 2e-19 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) Initial program 67.7%
add-cube-cbrt66.2%
pow366.2%
Applied egg-rr66.2%
rem-cube-cbrt67.7%
add-sqr-sqrt65.1%
sqrt-unprod67.8%
pow267.8%
+-commutative67.8%
Applied egg-rr67.8%
unpow267.8%
rem-sqrt-square67.8%
Simplified67.8%
Taylor expanded in x around 0 67.8%
Final simplification74.6%
(FPCore (x eps) :precision binary64 (- (* (sin eps) (cos x)) (* (sin x) (- 1.0 (cos eps)))))
double code(double x, double eps) {
return (sin(eps) * cos(x)) - (sin(x) * (1.0 - cos(eps)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (sin(eps) * cos(x)) - (sin(x) * (1.0d0 - cos(eps)))
end function
public static double code(double x, double eps) {
return (Math.sin(eps) * Math.cos(x)) - (Math.sin(x) * (1.0 - Math.cos(eps)));
}
def code(x, eps): return (math.sin(eps) * math.cos(x)) - (math.sin(x) * (1.0 - math.cos(eps)))
function code(x, eps) return Float64(Float64(sin(eps) * cos(x)) - Float64(sin(x) * Float64(1.0 - cos(eps)))) end
function tmp = code(x, eps) tmp = (sin(eps) * cos(x)) - (sin(x) * (1.0 - cos(eps))); end
code[x_, eps_] := N[(N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision] - N[(N[Sin[x], $MachinePrecision] * N[(1.0 - N[Cos[eps], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \cos x - \sin x \cdot \left(1 - \cos \varepsilon\right)
\end{array}
Initial program 44.8%
sin-sum71.0%
associate--l+71.0%
Applied egg-rr71.0%
+-commutative71.0%
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 (* 0.5 (+ eps (- x x)))) (cos (* 0.5 (+ x (+ eps x)))))))
double code(double x, double eps) {
return 2.0 * (sin((0.5 * (eps + (x - x)))) * cos((0.5 * (x + (eps + x)))));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 2.0d0 * (sin((0.5d0 * (eps + (x - x)))) * cos((0.5d0 * (x + (eps + x)))))
end function
public static double code(double x, double eps) {
return 2.0 * (Math.sin((0.5 * (eps + (x - x)))) * Math.cos((0.5 * (x + (eps + x)))));
}
def code(x, eps): return 2.0 * (math.sin((0.5 * (eps + (x - x)))) * math.cos((0.5 * (x + (eps + x)))))
function code(x, eps) return Float64(2.0 * Float64(sin(Float64(0.5 * Float64(eps + Float64(x - x)))) * cos(Float64(0.5 * Float64(x + Float64(eps + x)))))) end
function tmp = code(x, eps) tmp = 2.0 * (sin((0.5 * (eps + (x - x)))) * cos((0.5 * (x + (eps + x))))); end
code[x_, eps_] := N[(2.0 * N[(N[Sin[N[(0.5 * N[(eps + N[(x - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(0.5 * N[(x + N[(eps + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(\sin \left(0.5 \cdot \left(\varepsilon + \left(x - x\right)\right)\right) \cdot \cos \left(0.5 \cdot \left(x + \left(\varepsilon + x\right)\right)\right)\right)
\end{array}
Initial program 44.8%
add-sqr-sqrt22.2%
sqrt-unprod22.0%
pow222.0%
Applied egg-rr22.0%
sqrt-pow144.8%
metadata-eval44.8%
pow144.8%
diff-sin44.2%
div-inv44.2%
+-commutative44.2%
metadata-eval44.2%
div-inv44.2%
+-commutative44.2%
metadata-eval44.2%
Applied egg-rr44.2%
*-commutative44.2%
associate--l+74.2%
*-commutative74.2%
+-commutative74.2%
Simplified74.2%
Final simplification74.2%
(FPCore (x eps) :precision binary64 (* (* 2.0 (sin (/ eps 2.0))) (cos (/ (+ eps (* x 2.0)) 2.0))))
double code(double x, double eps) {
return (2.0 * sin((eps / 2.0))) * cos(((eps + (x * 2.0)) / 2.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (2.0d0 * sin((eps / 2.0d0))) * cos(((eps + (x * 2.0d0)) / 2.0d0))
end function
public static double code(double x, double eps) {
return (2.0 * Math.sin((eps / 2.0))) * Math.cos(((eps + (x * 2.0)) / 2.0));
}
def code(x, eps): return (2.0 * math.sin((eps / 2.0))) * math.cos(((eps + (x * 2.0)) / 2.0))
function code(x, eps) return Float64(Float64(2.0 * sin(Float64(eps / 2.0))) * cos(Float64(Float64(eps + Float64(x * 2.0)) / 2.0))) end
function tmp = code(x, eps) tmp = (2.0 * sin((eps / 2.0))) * cos(((eps + (x * 2.0)) / 2.0)); end
code[x_, eps_] := N[(N[(2.0 * N[Sin[N[(eps / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(N[(eps + N[(x * 2.0), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(2 \cdot \sin \left(\frac{\varepsilon}{2}\right)\right) \cdot \cos \left(\frac{\varepsilon + x \cdot 2}{2}\right)
\end{array}
Initial program 44.8%
add-cube-cbrt43.6%
pow343.5%
Applied egg-rr43.5%
rem-cube-cbrt44.8%
diff-sin44.2%
+-commutative44.2%
+-commutative44.2%
Applied egg-rr44.2%
associate-*r*44.2%
associate--l+74.2%
+-inverses74.2%
associate-+l+74.1%
count-274.1%
Simplified74.1%
Final simplification74.1%
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.76) (not (<= eps 4.5e-6))) (sin eps) (* eps (cos x))))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.76) || !(eps <= 4.5e-6)) {
tmp = sin(eps);
} 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 <= (-0.76d0)) .or. (.not. (eps <= 4.5d-6))) then
tmp = sin(eps)
else
tmp = eps * cos(x)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((eps <= -0.76) || !(eps <= 4.5e-6)) {
tmp = Math.sin(eps);
} else {
tmp = eps * Math.cos(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.76) or not (eps <= 4.5e-6): tmp = math.sin(eps) else: tmp = eps * math.cos(x) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.76) || !(eps <= 4.5e-6)) tmp = sin(eps); else tmp = Float64(eps * cos(x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -0.76) || ~((eps <= 4.5e-6))) tmp = sin(eps); else tmp = eps * cos(x); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.76], N[Not[LessEqual[eps, 4.5e-6]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.76 \lor \neg \left(\varepsilon \leq 4.5 \cdot 10^{-6}\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\end{array}
\end{array}
if eps < -0.76000000000000001 or 4.50000000000000011e-6 < eps Initial program 52.3%
Taylor expanded in x around 0 53.2%
if -0.76000000000000001 < eps < 4.50000000000000011e-6Initial program 36.0%
Taylor expanded in eps around 0 98.5%
Final simplification74.1%
(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 44.8%
Taylor expanded in x around 0 55.6%
Final simplification55.6%
(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 44.8%
add-cube-cbrt43.6%
pow343.5%
Applied egg-rr43.5%
Taylor expanded in eps around 0 4.1%
pow-base-14.1%
*-lft-identity4.1%
+-inverses4.1%
Simplified4.1%
Final simplification4.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 44.8%
Taylor expanded in eps around 0 47.5%
Taylor expanded in x around 0 29.0%
Final simplification29.0%
(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 2023301
(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)))