
(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 8 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) (* (sin x) (tan (* eps 0.5))))))
double code(double x, double eps) {
return sin(eps) * (cos(x) - (sin(x) * tan((eps * 0.5))));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin(eps) * (cos(x) - (sin(x) * tan((eps * 0.5d0))))
end function
public static double code(double x, double eps) {
return Math.sin(eps) * (Math.cos(x) - (Math.sin(x) * Math.tan((eps * 0.5))));
}
def code(x, eps): return math.sin(eps) * (math.cos(x) - (math.sin(x) * math.tan((eps * 0.5))))
function code(x, eps) return Float64(sin(eps) * Float64(cos(x) - Float64(sin(x) * tan(Float64(eps * 0.5))))) end
function tmp = code(x, eps) tmp = sin(eps) * (cos(x) - (sin(x) * tan((eps * 0.5)))); end
code[x_, eps_] := N[(N[Sin[eps], $MachinePrecision] * N[(N[Cos[x], $MachinePrecision] - N[(N[Sin[x], $MachinePrecision] * N[Tan[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \left(\cos x - \sin x \cdot \tan \left(\varepsilon \cdot 0.5\right)\right)
\end{array}
Initial program 39.8%
sin-sum59.6%
associate--l+59.6%
Applied egg-rr59.6%
+-commutative59.6%
associate-+l-99.1%
*-commutative99.1%
*-rgt-identity99.1%
distribute-lft-out--99.2%
Simplified99.2%
flip--99.1%
associate-*r/99.0%
metadata-eval99.0%
1-sub-cos99.5%
pow299.5%
Applied egg-rr99.5%
*-commutative99.5%
associate-/l*99.5%
Simplified99.5%
Taylor expanded in eps around inf 99.5%
associate-/l*99.5%
unpow299.5%
*-rgt-identity99.5%
associate-*r/99.5%
times-frac99.5%
hang-0p-tan99.7%
associate-/r/99.7%
/-rgt-identity99.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%
neg-mul-199.7%
associate-*r*99.7%
*-commutative99.7%
neg-mul-199.7%
Simplified99.7%
Final simplification99.7%
(FPCore (x eps) :precision binary64 (+ (* (sin eps) (cos x)) (* (sin x) (+ (cos eps) -1.0))))
double code(double x, double eps) {
return (sin(eps) * cos(x)) + (sin(x) * (cos(eps) + -1.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (sin(eps) * cos(x)) + (sin(x) * (cos(eps) + (-1.0d0)))
end function
public static double code(double x, double eps) {
return (Math.sin(eps) * Math.cos(x)) + (Math.sin(x) * (Math.cos(eps) + -1.0));
}
def code(x, eps): return (math.sin(eps) * math.cos(x)) + (math.sin(x) * (math.cos(eps) + -1.0))
function code(x, eps) return Float64(Float64(sin(eps) * cos(x)) + Float64(sin(x) * Float64(cos(eps) + -1.0))) end
function tmp = code(x, eps) tmp = (sin(eps) * cos(x)) + (sin(x) * (cos(eps) + -1.0)); end
code[x_, eps_] := N[(N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \cos x + \sin x \cdot \left(\cos \varepsilon + -1\right)
\end{array}
Initial program 39.8%
sin-sum59.6%
associate--l+59.6%
Applied egg-rr59.6%
+-commutative59.6%
associate-+l-99.1%
*-commutative99.1%
*-rgt-identity99.1%
distribute-lft-out--99.2%
Simplified99.2%
Final simplification99.2%
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.0013) (not (<= eps 0.0007))) (- (sin (- eps x)) (sin x)) (* eps (cos x))))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.0013) || !(eps <= 0.0007)) {
tmp = sin((eps - x)) - 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 <= (-0.0013d0)) .or. (.not. (eps <= 0.0007d0))) then
tmp = sin((eps - x)) - 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 <= -0.0013) || !(eps <= 0.0007)) {
tmp = Math.sin((eps - x)) - Math.sin(x);
} else {
tmp = eps * Math.cos(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.0013) or not (eps <= 0.0007): tmp = math.sin((eps - x)) - math.sin(x) else: tmp = eps * math.cos(x) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.0013) || !(eps <= 0.0007)) tmp = Float64(sin(Float64(eps - x)) - sin(x)); else tmp = Float64(eps * cos(x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -0.0013) || ~((eps <= 0.0007))) tmp = sin((eps - x)) - sin(x); else tmp = eps * cos(x); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.0013], N[Not[LessEqual[eps, 0.0007]], $MachinePrecision]], N[(N[Sin[N[(eps - x), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.0013 \lor \neg \left(\varepsilon \leq 0.0007\right):\\
\;\;\;\;\sin \left(\varepsilon - x\right) - \sin x\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\end{array}
\end{array}
if eps < -0.0012999999999999999 or 6.99999999999999993e-4 < eps Initial program 57.1%
sin-sum99.5%
associate--l+99.5%
Applied egg-rr99.5%
+-commutative99.5%
associate-+l-99.5%
*-commutative99.5%
*-rgt-identity99.5%
distribute-lft-out--99.6%
Simplified99.6%
sub-neg99.6%
distribute-lft-in99.5%
*-commutative99.5%
*-un-lft-identity99.5%
Applied egg-rr99.5%
*-un-lft-identity99.5%
+-commutative99.5%
associate--r+99.5%
*-commutative99.5%
add-sqr-sqrt45.3%
sqrt-unprod83.5%
sqr-neg83.5%
sqrt-unprod38.2%
add-sqr-sqrt60.9%
sin-diff60.6%
Applied egg-rr60.6%
*-lft-identity60.6%
Simplified60.6%
if -0.0012999999999999999 < eps < 6.99999999999999993e-4Initial program 26.3%
Taylor expanded in eps around 0 98.3%
Final simplification81.8%
(FPCore (x eps) :precision binary64 (if (<= eps -7.4e-6) (- (sin (+ eps x)) (sin x)) (if (<= eps 1.8e-5) (* eps (cos x)) (sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -7.4e-6) {
tmp = sin((eps + x)) - sin(x);
} else if (eps <= 1.8e-5) {
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 <= (-7.4d-6)) then
tmp = sin((eps + x)) - sin(x)
else if (eps <= 1.8d-5) 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 <= -7.4e-6) {
tmp = Math.sin((eps + x)) - Math.sin(x);
} else if (eps <= 1.8e-5) {
tmp = eps * Math.cos(x);
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -7.4e-6: tmp = math.sin((eps + x)) - math.sin(x) elif eps <= 1.8e-5: tmp = eps * math.cos(x) else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -7.4e-6) tmp = Float64(sin(Float64(eps + x)) - sin(x)); elseif (eps <= 1.8e-5) tmp = Float64(eps * cos(x)); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -7.4e-6) tmp = sin((eps + x)) - sin(x); elseif (eps <= 1.8e-5) tmp = eps * cos(x); else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -7.4e-6], N[(N[Sin[N[(eps + x), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision], If[LessEqual[eps, 1.8e-5], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -7.4 \cdot 10^{-6}:\\
\;\;\;\;\sin \left(\varepsilon + x\right) - \sin x\\
\mathbf{elif}\;\varepsilon \leq 1.8 \cdot 10^{-5}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -7.4000000000000003e-6Initial program 64.1%
if -7.4000000000000003e-6 < eps < 1.80000000000000005e-5Initial program 26.1%
Taylor expanded in eps around 0 98.8%
if 1.80000000000000005e-5 < eps Initial program 47.8%
Taylor expanded in x around 0 51.3%
Final simplification81.0%
(FPCore (x eps) :precision binary64 (* (sin eps) (cos x)))
double code(double x, double eps) {
return sin(eps) * cos(x);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin(eps) * cos(x)
end function
public static double code(double x, double eps) {
return Math.sin(eps) * Math.cos(x);
}
def code(x, eps): return math.sin(eps) * math.cos(x)
function code(x, eps) return Float64(sin(eps) * cos(x)) end
function tmp = code(x, eps) tmp = sin(eps) * cos(x); end
code[x_, eps_] := N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \cos x
\end{array}
Initial program 39.8%
sin-sum59.6%
associate--l+59.6%
Applied egg-rr59.6%
+-commutative59.6%
associate-+l-99.1%
*-commutative99.1%
*-rgt-identity99.1%
distribute-lft-out--99.2%
Simplified99.2%
sub-neg99.2%
distribute-lft-in99.1%
*-commutative99.1%
*-un-lft-identity99.1%
Applied egg-rr99.1%
Taylor expanded in eps around 0 82.0%
distribute-rgt1-in82.0%
metadata-eval82.0%
mul0-lft82.0%
Simplified82.0%
Final simplification82.0%
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.00125) (not (<= eps 9.6e-5))) (sin eps) (* eps (cos x))))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.00125) || !(eps <= 9.6e-5)) {
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.00125d0)) .or. (.not. (eps <= 9.6d-5))) 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.00125) || !(eps <= 9.6e-5)) {
tmp = Math.sin(eps);
} else {
tmp = eps * Math.cos(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.00125) or not (eps <= 9.6e-5): tmp = math.sin(eps) else: tmp = eps * math.cos(x) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.00125) || !(eps <= 9.6e-5)) 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.00125) || ~((eps <= 9.6e-5))) tmp = sin(eps); else tmp = eps * cos(x); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.00125], N[Not[LessEqual[eps, 9.6e-5]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.00125 \lor \neg \left(\varepsilon \leq 9.6 \cdot 10^{-5}\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\end{array}
\end{array}
if eps < -0.00125000000000000003 or 9.6000000000000002e-5 < eps Initial program 57.1%
Taylor expanded in x around 0 58.8%
if -0.00125000000000000003 < eps < 9.6000000000000002e-5Initial program 26.3%
Taylor expanded in eps around 0 98.3%
Final simplification81.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 39.8%
Taylor expanded in x around 0 55.5%
Final simplification55.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 39.8%
Taylor expanded in x around 0 55.5%
Taylor expanded in eps around 0 31.3%
Final simplification31.3%
(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 2024013
(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)))