
(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 (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 40.5%
sin-sum61.8%
associate--l+61.8%
Applied egg-rr61.8%
+-commutative61.8%
sub-neg61.8%
associate-+l+99.6%
*-commutative99.6%
neg-mul-199.6%
*-commutative99.6%
distribute-rgt-out99.6%
+-commutative99.6%
Simplified99.6%
fma-def99.6%
*-commutative99.6%
Applied egg-rr99.6%
Final simplification99.6%
(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 40.5%
sin-sum61.8%
associate--l+61.8%
Applied egg-rr61.8%
+-commutative61.8%
sub-neg61.8%
associate-+l+99.6%
*-commutative99.6%
neg-mul-199.6%
*-commutative99.6%
distribute-rgt-out99.6%
+-commutative99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.00045) (not (<= eps 0.0038))) (- (sin eps) (sin x)) (+ (* (* eps -0.5) (* eps (sin x))) (* eps (cos x)))))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.00045) || !(eps <= 0.0038)) {
tmp = sin(eps) - sin(x);
} else {
tmp = ((eps * -0.5) * (eps * sin(x))) + (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.00045d0)) .or. (.not. (eps <= 0.0038d0))) then
tmp = sin(eps) - sin(x)
else
tmp = ((eps * (-0.5d0)) * (eps * sin(x))) + (eps * cos(x))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((eps <= -0.00045) || !(eps <= 0.0038)) {
tmp = Math.sin(eps) - Math.sin(x);
} else {
tmp = ((eps * -0.5) * (eps * Math.sin(x))) + (eps * Math.cos(x));
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.00045) or not (eps <= 0.0038): tmp = math.sin(eps) - math.sin(x) else: tmp = ((eps * -0.5) * (eps * math.sin(x))) + (eps * math.cos(x)) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.00045) || !(eps <= 0.0038)) tmp = Float64(sin(eps) - sin(x)); else tmp = Float64(Float64(Float64(eps * -0.5) * Float64(eps * sin(x))) + Float64(eps * cos(x))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -0.00045) || ~((eps <= 0.0038))) tmp = sin(eps) - sin(x); else tmp = ((eps * -0.5) * (eps * sin(x))) + (eps * cos(x)); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.00045], N[Not[LessEqual[eps, 0.0038]], $MachinePrecision]], N[(N[Sin[eps], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision], N[(N[(N[(eps * -0.5), $MachinePrecision] * N[(eps * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.00045 \lor \neg \left(\varepsilon \leq 0.0038\right):\\
\;\;\;\;\sin \varepsilon - \sin x\\
\mathbf{else}:\\
\;\;\;\;\left(\varepsilon \cdot -0.5\right) \cdot \left(\varepsilon \cdot \sin x\right) + \varepsilon \cdot \cos x\\
\end{array}
\end{array}
if eps < -4.4999999999999999e-4 or 0.00379999999999999999 < eps Initial program 56.0%
add-cube-cbrt55.5%
pow355.4%
Applied egg-rr55.4%
Taylor expanded in x around 0 26.5%
unpow1/357.1%
Simplified57.1%
Taylor expanded in eps around inf 58.1%
pow-base-158.1%
*-lft-identity58.1%
Simplified58.1%
if -4.4999999999999999e-4 < eps < 0.00379999999999999999Initial program 26.1%
Taylor expanded in eps around 0 99.4%
+-commutative99.4%
fma-def99.4%
unpow299.4%
associate-*l*99.4%
*-commutative99.4%
Simplified99.4%
fma-udef99.4%
associate-*r*99.4%
Applied egg-rr99.4%
Final simplification79.6%
(FPCore (x eps) :precision binary64 (* 2.0 (* (sin (* eps 0.5)) (cos (* 0.5 (+ eps (+ x x)))))))
double code(double x, double eps) {
return 2.0 * (sin((eps * 0.5)) * cos((0.5 * (eps + (x + x)))));
}
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 + x)))))
end function
public static double code(double x, double eps) {
return 2.0 * (Math.sin((eps * 0.5)) * Math.cos((0.5 * (eps + (x + x)))));
}
def code(x, eps): return 2.0 * (math.sin((eps * 0.5)) * math.cos((0.5 * (eps + (x + x)))))
function code(x, eps) return Float64(2.0 * Float64(sin(Float64(eps * 0.5)) * cos(Float64(0.5 * Float64(eps + Float64(x + x)))))) end
function tmp = code(x, eps) tmp = 2.0 * (sin((eps * 0.5)) * cos((0.5 * (eps + (x + x))))); 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 + x), $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 + \left(x + x\right)\right)\right)\right)
\end{array}
Initial program 40.5%
diff-sin39.6%
div-inv39.6%
metadata-eval39.6%
div-inv39.6%
+-commutative39.6%
metadata-eval39.6%
Applied egg-rr39.6%
*-commutative39.6%
+-commutative39.6%
associate--l+78.0%
+-inverses78.0%
distribute-lft-in78.0%
metadata-eval78.0%
*-commutative78.0%
associate-+r+78.0%
+-commutative78.0%
Simplified78.0%
Final simplification78.0%
(FPCore (x eps) :precision binary64 (if (or (<= eps -15500.0) (not (<= eps 0.0037))) (- (sin eps) (sin x)) (* eps (cos x))))
double code(double x, double eps) {
double tmp;
if ((eps <= -15500.0) || !(eps <= 0.0037)) {
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 <= (-15500.0d0)) .or. (.not. (eps <= 0.0037d0))) 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 <= -15500.0) || !(eps <= 0.0037)) {
tmp = Math.sin(eps) - Math.sin(x);
} else {
tmp = eps * Math.cos(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -15500.0) or not (eps <= 0.0037): tmp = math.sin(eps) - math.sin(x) else: tmp = eps * math.cos(x) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -15500.0) || !(eps <= 0.0037)) 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 <= -15500.0) || ~((eps <= 0.0037))) tmp = sin(eps) - sin(x); else tmp = eps * cos(x); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -15500.0], N[Not[LessEqual[eps, 0.0037]], $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 -15500 \lor \neg \left(\varepsilon \leq 0.0037\right):\\
\;\;\;\;\sin \varepsilon - \sin x\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\end{array}
\end{array}
if eps < -15500 or 0.0037000000000000002 < eps Initial program 56.5%
add-cube-cbrt55.9%
pow355.9%
Applied egg-rr55.9%
Taylor expanded in x around 0 26.7%
unpow1/357.5%
Simplified57.5%
Taylor expanded in eps around inf 58.6%
pow-base-158.6%
*-lft-identity58.6%
Simplified58.6%
if -15500 < eps < 0.0037000000000000002Initial program 25.9%
Taylor expanded in eps around 0 98.6%
Final simplification79.5%
(FPCore (x eps) :precision binary64 (if (<= eps -15500.0) (sin eps) (if (<= eps 0.012) (* eps (cos x)) (sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -15500.0) {
tmp = sin(eps);
} else if (eps <= 0.012) {
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 <= (-15500.0d0)) then
tmp = sin(eps)
else if (eps <= 0.012d0) 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 <= -15500.0) {
tmp = Math.sin(eps);
} else if (eps <= 0.012) {
tmp = eps * Math.cos(x);
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -15500.0: tmp = math.sin(eps) elif eps <= 0.012: tmp = eps * math.cos(x) else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -15500.0) tmp = sin(eps); elseif (eps <= 0.012) tmp = Float64(eps * cos(x)); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -15500.0) tmp = sin(eps); elseif (eps <= 0.012) tmp = eps * cos(x); else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -15500.0], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 0.012], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -15500:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 0.012:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -15500 or 0.012 < eps Initial program 56.5%
Taylor expanded in x around 0 56.5%
if -15500 < eps < 0.012Initial program 25.9%
Taylor expanded in eps around 0 98.6%
Final simplification78.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 40.5%
Taylor expanded in x around 0 52.4%
Final simplification52.4%
(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 40.5%
add-cube-cbrt39.4%
pow339.3%
Applied egg-rr39.3%
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 40.5%
Taylor expanded in eps around 0 53.0%
Taylor expanded in x around 0 26.9%
Final simplification26.9%
(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 2023256
(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)))