
(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 6 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
(let* ((t_0 (* (sin eps) (sin x))) (t_1 (tan (* eps 0.5))))
(+
(fma 1.0 (* (sin eps) (cos x)) (* t_1 (* (sin x) (- (sin eps)))))
(fma (- t_1) t_0 (* t_1 t_0)))))
double code(double x, double eps) {
double t_0 = sin(eps) * sin(x);
double t_1 = tan((eps * 0.5));
return fma(1.0, (sin(eps) * cos(x)), (t_1 * (sin(x) * -sin(eps)))) + fma(-t_1, t_0, (t_1 * t_0));
}
function code(x, eps) t_0 = Float64(sin(eps) * sin(x)) t_1 = tan(Float64(eps * 0.5)) return Float64(fma(1.0, Float64(sin(eps) * cos(x)), Float64(t_1 * Float64(sin(x) * Float64(-sin(eps))))) + fma(Float64(-t_1), t_0, Float64(t_1 * t_0))) end
code[x_, eps_] := Block[{t$95$0 = N[(N[Sin[eps], $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Tan[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]}, N[(N[(1.0 * N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision] + N[(t$95$1 * N[(N[Sin[x], $MachinePrecision] * (-N[Sin[eps], $MachinePrecision])), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[((-t$95$1) * t$95$0 + N[(t$95$1 * t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \varepsilon \cdot \sin x\\
t_1 := \tan \left(\varepsilon \cdot 0.5\right)\\
\mathsf{fma}\left(1, \sin \varepsilon \cdot \cos x, t_1 \cdot \left(\sin x \cdot \left(-\sin \varepsilon\right)\right)\right) + \mathsf{fma}\left(-t_1, t_0, t_1 \cdot t_0\right)
\end{array}
\end{array}
Initial program 37.1%
sin-sum60.5%
associate--l+60.6%
Applied egg-rr60.6%
+-commutative60.6%
associate-+l-99.2%
*-commutative99.2%
*-rgt-identity99.2%
distribute-lft-out--99.2%
Simplified99.2%
flip--99.0%
div-inv99.0%
metadata-eval99.0%
1-sub-cos99.4%
pow299.4%
Applied egg-rr99.4%
associate-*r/99.4%
*-rgt-identity99.4%
Simplified99.4%
Taylor expanded in x around inf 99.4%
*-commutative99.4%
*-lft-identity99.4%
times-frac99.4%
/-rgt-identity99.4%
unpow299.4%
associate-*r/99.4%
hang-0p-tan99.7%
Simplified99.7%
*-un-lft-identity99.7%
associate-*r*99.7%
prod-diff99.7%
div-inv99.7%
metadata-eval99.7%
div-inv99.7%
metadata-eval99.7%
Applied egg-rr99.7%
Final simplification99.7%
(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 37.1%
sin-sum60.5%
associate--l+60.6%
Applied egg-rr60.6%
+-commutative60.6%
associate-+l-99.2%
*-commutative99.2%
*-rgt-identity99.2%
distribute-lft-out--99.2%
Simplified99.2%
flip--99.0%
div-inv99.0%
metadata-eval99.0%
1-sub-cos99.4%
pow299.4%
Applied egg-rr99.4%
associate-*r/99.4%
*-rgt-identity99.4%
Simplified99.4%
Taylor expanded in x around inf 99.4%
*-commutative99.4%
*-lft-identity99.4%
times-frac99.4%
/-rgt-identity99.4%
unpow299.4%
associate-*r/99.4%
hang-0p-tan99.7%
Simplified99.7%
sub-neg99.7%
div-inv99.7%
metadata-eval99.7%
Applied egg-rr99.7%
sub-neg99.7%
*-commutative99.7%
associate-*r*99.7%
*-commutative99.7%
associate-*r*99.7%
distribute-rgt-out--99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (x eps) :precision binary64 (* (cos (* 0.5 (+ eps (* x 2.0)))) (* 2.0 (sin (* eps 0.5)))))
double code(double x, double eps) {
return cos((0.5 * (eps + (x * 2.0)))) * (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 * 2.0d0)))) * (2.0d0 * sin((eps * 0.5d0)))
end function
public static double code(double x, double eps) {
return Math.cos((0.5 * (eps + (x * 2.0)))) * (2.0 * Math.sin((eps * 0.5)));
}
def code(x, eps): return math.cos((0.5 * (eps + (x * 2.0)))) * (2.0 * math.sin((eps * 0.5)))
function code(x, eps) return Float64(cos(Float64(0.5 * Float64(eps + Float64(x * 2.0)))) * Float64(2.0 * sin(Float64(eps * 0.5)))) end
function tmp = code(x, eps) tmp = cos((0.5 * (eps + (x * 2.0)))) * (2.0 * sin((eps * 0.5))); end
code[x_, eps_] := N[(N[Cos[N[(0.5 * N[(eps + N[(x * 2.0), $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 + x \cdot 2\right)\right) \cdot \left(2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)
\end{array}
Initial program 37.1%
add-cbrt-cube30.8%
pow330.8%
Applied egg-rr30.8%
rem-cbrt-cube37.1%
diff-sin36.6%
div-inv36.6%
+-commutative36.6%
associate--l+77.2%
metadata-eval77.2%
div-inv77.2%
+-commutative77.2%
associate-+l+77.0%
metadata-eval77.0%
Applied egg-rr77.0%
associate-*r*77.0%
*-commutative77.0%
*-commutative77.0%
count-277.0%
*-commutative77.0%
+-inverses77.0%
+-rgt-identity77.0%
Simplified77.0%
Final simplification77.0%
(FPCore (x eps) :precision binary64 (if (<= eps -0.106) (sin eps) (if (<= eps 0.0033) (* eps (cos x)) (sin (+ eps x)))))
double code(double x, double eps) {
double tmp;
if (eps <= -0.106) {
tmp = sin(eps);
} else if (eps <= 0.0033) {
tmp = eps * cos(x);
} else {
tmp = sin((eps + 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.106d0)) then
tmp = sin(eps)
else if (eps <= 0.0033d0) then
tmp = eps * cos(x)
else
tmp = sin((eps + x))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= -0.106) {
tmp = Math.sin(eps);
} else if (eps <= 0.0033) {
tmp = eps * Math.cos(x);
} else {
tmp = Math.sin((eps + x));
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -0.106: tmp = math.sin(eps) elif eps <= 0.0033: tmp = eps * math.cos(x) else: tmp = math.sin((eps + x)) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -0.106) tmp = sin(eps); elseif (eps <= 0.0033) tmp = Float64(eps * cos(x)); else tmp = sin(Float64(eps + x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -0.106) tmp = sin(eps); elseif (eps <= 0.0033) tmp = eps * cos(x); else tmp = sin((eps + x)); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -0.106], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 0.0033], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision], N[Sin[N[(eps + x), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.106:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 0.0033:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\mathbf{else}:\\
\;\;\;\;\sin \left(\varepsilon + x\right)\\
\end{array}
\end{array}
if eps < -0.105999999999999997Initial program 55.0%
Taylor expanded in x around 0 57.0%
if -0.105999999999999997 < eps < 0.0033Initial program 24.3%
Taylor expanded in eps around 0 97.6%
if 0.0033 < eps Initial program 48.6%
log1p-expm1-u48.6%
Applied egg-rr48.6%
log1p-expm1-u48.6%
add-sqr-sqrt21.5%
sqrt-unprod27.1%
pow227.1%
+-commutative27.1%
Applied egg-rr27.1%
unpow227.1%
rem-sqrt-square27.1%
Simplified27.1%
Taylor expanded in x around 0 26.0%
+-commutative26.0%
rem-square-sqrt21.3%
fabs-sqr21.3%
rem-square-sqrt49.4%
Simplified49.4%
Final simplification77.0%
(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 37.1%
Taylor expanded in x around 0 53.5%
Final simplification53.5%
(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 37.1%
sin-sum60.5%
associate--l+60.6%
Applied egg-rr60.6%
+-commutative60.6%
associate-+l-99.2%
*-commutative99.2%
*-rgt-identity99.2%
distribute-lft-out--99.2%
Simplified99.2%
Taylor expanded in x around 0 54.6%
Taylor expanded in eps around 0 30.7%
Final simplification30.7%
(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 2023318
(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)))