
(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 (cos x) (sin eps) (* (sin x) (log (exp (+ -1.0 (cos eps)))))))
double code(double x, double eps) {
return fma(cos(x), sin(eps), (sin(x) * log(exp((-1.0 + cos(eps))))));
}
function code(x, eps) return fma(cos(x), sin(eps), Float64(sin(x) * log(exp(Float64(-1.0 + cos(eps)))))) end
code[x_, eps_] := N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * N[Log[N[Exp[N[(-1.0 + N[Cos[eps], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\cos x, \sin \varepsilon, \sin x \cdot \log \left(e^{-1 + \cos \varepsilon}\right)\right)
\end{array}
Initial program 38.5%
sin-sum60.7%
associate--l+60.7%
fma-def60.7%
Applied egg-rr60.7%
Taylor expanded in x around inf 60.7%
associate--l+99.5%
fma-def99.5%
*-commutative99.5%
*-rgt-identity99.5%
distribute-lft-out--99.5%
sub-neg99.5%
metadata-eval99.5%
+-commutative99.5%
Simplified99.5%
add-log-exp99.5%
Applied egg-rr99.5%
Final simplification99.5%
(FPCore (x eps) :precision binary64 (fma (cos x) (sin eps) (* (sin x) (+ -1.0 (cos eps)))))
double code(double x, double eps) {
return fma(cos(x), sin(eps), (sin(x) * (-1.0 + cos(eps))));
}
function code(x, eps) return fma(cos(x), sin(eps), Float64(sin(x) * Float64(-1.0 + cos(eps)))) end
code[x_, eps_] := N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * N[(-1.0 + N[Cos[eps], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\cos x, \sin \varepsilon, \sin x \cdot \left(-1 + \cos \varepsilon\right)\right)
\end{array}
Initial program 38.5%
sin-sum60.7%
associate--l+60.7%
fma-def60.7%
Applied egg-rr60.7%
Taylor expanded in x around inf 60.7%
associate--l+99.5%
fma-def99.5%
*-commutative99.5%
*-rgt-identity99.5%
distribute-lft-out--99.5%
sub-neg99.5%
metadata-eval99.5%
+-commutative99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x eps) :precision binary64 (+ (* (cos x) (sin eps)) (* (sin x) (+ -1.0 (cos eps)))))
double code(double x, double eps) {
return (cos(x) * sin(eps)) + (sin(x) * (-1.0 + cos(eps)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (cos(x) * sin(eps)) + (sin(x) * ((-1.0d0) + cos(eps)))
end function
public static double code(double x, double eps) {
return (Math.cos(x) * Math.sin(eps)) + (Math.sin(x) * (-1.0 + Math.cos(eps)));
}
def code(x, eps): return (math.cos(x) * math.sin(eps)) + (math.sin(x) * (-1.0 + math.cos(eps)))
function code(x, eps) return Float64(Float64(cos(x) * sin(eps)) + Float64(sin(x) * Float64(-1.0 + cos(eps)))) end
function tmp = code(x, eps) tmp = (cos(x) * sin(eps)) + (sin(x) * (-1.0 + cos(eps))); end
code[x_, eps_] := N[(N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * N[(-1.0 + N[Cos[eps], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos x \cdot \sin \varepsilon + \sin x \cdot \left(-1 + \cos \varepsilon\right)
\end{array}
Initial program 38.5%
sin-sum60.7%
associate--l+60.7%
fma-def60.7%
Applied egg-rr60.7%
Taylor expanded in x around inf 60.7%
associate--l+99.5%
fma-def99.5%
*-commutative99.5%
*-rgt-identity99.5%
distribute-lft-out--99.5%
sub-neg99.5%
metadata-eval99.5%
+-commutative99.5%
Simplified99.5%
fma-udef99.5%
*-commutative99.5%
Applied egg-rr99.5%
Final simplification99.5%
(FPCore (x eps)
:precision binary64
(if (<= eps -0.022)
(sin eps)
(if (<= eps 9e-5)
(+ (* (cos x) eps) (* (* (sin x) (* eps eps)) -0.5))
(sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -0.022) {
tmp = sin(eps);
} else if (eps <= 9e-5) {
tmp = (cos(x) * eps) + ((sin(x) * (eps * eps)) * -0.5);
} 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 <= (-0.022d0)) then
tmp = sin(eps)
else if (eps <= 9d-5) then
tmp = (cos(x) * eps) + ((sin(x) * (eps * eps)) * (-0.5d0))
else
tmp = sin(eps)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= -0.022) {
tmp = Math.sin(eps);
} else if (eps <= 9e-5) {
tmp = (Math.cos(x) * eps) + ((Math.sin(x) * (eps * eps)) * -0.5);
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -0.022: tmp = math.sin(eps) elif eps <= 9e-5: tmp = (math.cos(x) * eps) + ((math.sin(x) * (eps * eps)) * -0.5) else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -0.022) tmp = sin(eps); elseif (eps <= 9e-5) tmp = Float64(Float64(cos(x) * eps) + Float64(Float64(sin(x) * Float64(eps * eps)) * -0.5)); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -0.022) tmp = sin(eps); elseif (eps <= 9e-5) tmp = (cos(x) * eps) + ((sin(x) * (eps * eps)) * -0.5); else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -0.022], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 9e-5], N[(N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision] + N[(N[(N[Sin[x], $MachinePrecision] * N[(eps * eps), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.022:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 9 \cdot 10^{-5}:\\
\;\;\;\;\cos x \cdot \varepsilon + \left(\sin x \cdot \left(\varepsilon \cdot \varepsilon\right)\right) \cdot -0.5\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -0.021999999999999999 or 9.00000000000000057e-5 < eps Initial program 53.8%
Taylor expanded in x around 0 55.5%
if -0.021999999999999999 < eps < 9.00000000000000057e-5Initial program 24.1%
Taylor expanded in eps around 0 99.5%
fma-def99.5%
*-commutative99.5%
unpow299.5%
Simplified99.5%
fma-udef99.5%
*-commutative99.5%
Applied egg-rr99.5%
Final simplification78.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 38.5%
diff-sin38.1%
div-inv38.1%
metadata-eval38.1%
div-inv38.1%
+-commutative38.1%
metadata-eval38.1%
Applied egg-rr38.1%
associate-*r*38.1%
*-commutative38.1%
associate-*l*38.1%
+-commutative38.1%
associate--l+77.6%
+-inverses77.6%
*-commutative77.6%
associate-+r+77.7%
+-commutative77.7%
Simplified77.7%
Taylor expanded in eps around inf 77.7%
*-commutative77.7%
+-commutative77.7%
remove-double-neg77.7%
mul-1-neg77.7%
sub-neg77.7%
*-commutative77.7%
*-commutative77.7%
cancel-sign-sub-inv77.7%
metadata-eval77.7%
*-lft-identity77.7%
+-commutative77.7%
metadata-eval77.7%
cancel-sign-sub-inv77.7%
*-commutative77.7%
Simplified77.7%
Final simplification77.7%
(FPCore (x eps) :precision binary64 (if (<= eps -220.0) (sin eps) (if (<= eps 2.7e-5) (* (sin (* eps 0.5)) (* (cos x) 2.0)) (sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -220.0) {
tmp = sin(eps);
} else if (eps <= 2.7e-5) {
tmp = sin((eps * 0.5)) * (cos(x) * 2.0);
} 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 <= (-220.0d0)) then
tmp = sin(eps)
else if (eps <= 2.7d-5) then
tmp = sin((eps * 0.5d0)) * (cos(x) * 2.0d0)
else
tmp = sin(eps)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= -220.0) {
tmp = Math.sin(eps);
} else if (eps <= 2.7e-5) {
tmp = Math.sin((eps * 0.5)) * (Math.cos(x) * 2.0);
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -220.0: tmp = math.sin(eps) elif eps <= 2.7e-5: tmp = math.sin((eps * 0.5)) * (math.cos(x) * 2.0) else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -220.0) tmp = sin(eps); elseif (eps <= 2.7e-5) tmp = Float64(sin(Float64(eps * 0.5)) * Float64(cos(x) * 2.0)); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -220.0) tmp = sin(eps); elseif (eps <= 2.7e-5) tmp = sin((eps * 0.5)) * (cos(x) * 2.0); else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -220.0], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 2.7e-5], N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[(N[Cos[x], $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -220:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 2.7 \cdot 10^{-5}:\\
\;\;\;\;\sin \left(\varepsilon \cdot 0.5\right) \cdot \left(\cos x \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -220 or 2.6999999999999999e-5 < eps Initial program 54.2%
Taylor expanded in x around 0 55.9%
if -220 < eps < 2.6999999999999999e-5Initial program 23.9%
diff-sin23.9%
div-inv23.9%
metadata-eval23.9%
div-inv23.9%
+-commutative23.9%
metadata-eval23.9%
Applied egg-rr23.9%
associate-*r*23.9%
*-commutative23.9%
associate-*l*23.9%
+-commutative23.9%
associate--l+99.0%
+-inverses99.0%
*-commutative99.0%
associate-+r+99.0%
+-commutative99.0%
Simplified99.0%
Taylor expanded in eps around 0 98.6%
Final simplification78.0%
(FPCore (x eps) :precision binary64 (if (<= eps -0.000135) (sin eps) (if (<= eps 4.4e-5) (* (cos x) eps) (sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -0.000135) {
tmp = sin(eps);
} else if (eps <= 4.4e-5) {
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 <= (-0.000135d0)) then
tmp = sin(eps)
else if (eps <= 4.4d-5) 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 <= -0.000135) {
tmp = Math.sin(eps);
} else if (eps <= 4.4e-5) {
tmp = Math.cos(x) * eps;
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -0.000135: tmp = math.sin(eps) elif eps <= 4.4e-5: tmp = math.cos(x) * eps else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -0.000135) tmp = sin(eps); elseif (eps <= 4.4e-5) tmp = Float64(cos(x) * eps); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -0.000135) tmp = sin(eps); elseif (eps <= 4.4e-5) tmp = cos(x) * eps; else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -0.000135], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 4.4e-5], N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.000135:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 4.4 \cdot 10^{-5}:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -1.35000000000000002e-4 or 4.3999999999999999e-5 < eps Initial program 53.8%
Taylor expanded in x around 0 55.5%
if -1.35000000000000002e-4 < eps < 4.3999999999999999e-5Initial program 24.1%
Taylor expanded in eps around 0 99.2%
Final simplification78.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 38.5%
Taylor expanded in x around 0 52.1%
Final simplification52.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 38.5%
Taylor expanded in eps around 0 53.1%
fma-def53.1%
*-commutative53.1%
unpow253.1%
Simplified53.1%
Taylor expanded in x around 0 27.1%
Final simplification27.1%
(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 2023201
(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)))