
(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)) (/ (* (pow (sin eps) 2.0) (sin x)) (+ 1.0 (cos eps)))))
double code(double x, double eps) {
return (sin(eps) * cos(x)) - ((pow(sin(eps), 2.0) * 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(eps) ** 2.0d0) * sin(x)) / (1.0d0 + cos(eps)))
end function
public static double code(double x, double eps) {
return (Math.sin(eps) * Math.cos(x)) - ((Math.pow(Math.sin(eps), 2.0) * Math.sin(x)) / (1.0 + Math.cos(eps)));
}
def code(x, eps): return (math.sin(eps) * math.cos(x)) - ((math.pow(math.sin(eps), 2.0) * math.sin(x)) / (1.0 + math.cos(eps)))
function code(x, eps) return Float64(Float64(sin(eps) * cos(x)) - Float64(Float64((sin(eps) ^ 2.0) * sin(x)) / Float64(1.0 + cos(eps)))) end
function tmp = code(x, eps) tmp = (sin(eps) * cos(x)) - (((sin(eps) ^ 2.0) * sin(x)) / (1.0 + cos(eps))); end
code[x_, eps_] := N[(N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision] - N[(N[(N[Power[N[Sin[eps], $MachinePrecision], 2.0], $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision] / N[(1.0 + N[Cos[eps], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \cos x - \frac{{\sin \varepsilon}^{2} \cdot \sin x}{1 + \cos \varepsilon}
\end{array}
Initial program 45.4%
sin-sum68.7%
associate--l+68.7%
Applied egg-rr68.7%
+-commutative68.7%
associate-+l-99.1%
*-commutative99.1%
*-rgt-identity99.1%
distribute-lft-out--99.2%
Simplified99.2%
*-commutative99.2%
flip--98.9%
associate-*l/98.9%
metadata-eval98.9%
1-sub-cos99.4%
pow299.4%
Applied egg-rr99.4%
Final simplification99.4%
(FPCore (x eps)
:precision binary64
(let* ((t_0 (- (sin (+ eps x)) (sin x))))
(if (<= t_0 -0.001)
t_0
(if (<= t_0 0.0) (* (cos x) (* 2.0 (sin (* eps 0.5)))) (sin eps)))))
double code(double x, double eps) {
double t_0 = sin((eps + x)) - sin(x);
double tmp;
if (t_0 <= -0.001) {
tmp = t_0;
} else if (t_0 <= 0.0) {
tmp = cos(x) * (2.0 * sin((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) :: t_0
real(8) :: tmp
t_0 = sin((eps + x)) - sin(x)
if (t_0 <= (-0.001d0)) then
tmp = t_0
else if (t_0 <= 0.0d0) then
tmp = cos(x) * (2.0d0 * sin((eps * 0.5d0)))
else
tmp = sin(eps)
end if
code = tmp
end function
public static double code(double x, double eps) {
double t_0 = Math.sin((eps + x)) - Math.sin(x);
double tmp;
if (t_0 <= -0.001) {
tmp = t_0;
} else if (t_0 <= 0.0) {
tmp = Math.cos(x) * (2.0 * Math.sin((eps * 0.5)));
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): t_0 = math.sin((eps + x)) - math.sin(x) tmp = 0 if t_0 <= -0.001: tmp = t_0 elif t_0 <= 0.0: tmp = math.cos(x) * (2.0 * math.sin((eps * 0.5))) else: tmp = math.sin(eps) return tmp
function code(x, eps) t_0 = Float64(sin(Float64(eps + x)) - sin(x)) tmp = 0.0 if (t_0 <= -0.001) tmp = t_0; elseif (t_0 <= 0.0) tmp = Float64(cos(x) * Float64(2.0 * sin(Float64(eps * 0.5)))); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) t_0 = sin((eps + x)) - sin(x); tmp = 0.0; if (t_0 <= -0.001) tmp = t_0; elseif (t_0 <= 0.0) tmp = cos(x) * (2.0 * sin((eps * 0.5))); else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := Block[{t$95$0 = N[(N[Sin[N[(eps + x), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -0.001], t$95$0, If[LessEqual[t$95$0, 0.0], N[(N[Cos[x], $MachinePrecision] * N[(2.0 * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(\varepsilon + x\right) - \sin x\\
\mathbf{if}\;t_0 \leq -0.001:\\
\;\;\;\;t_0\\
\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;\cos x \cdot \left(2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < -1e-3Initial program 69.1%
if -1e-3 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < 0.0Initial program 19.4%
diff-sin19.4%
div-inv19.4%
associate--l+19.4%
metadata-eval19.4%
div-inv19.4%
+-commutative19.4%
associate-+l+19.4%
metadata-eval19.4%
Applied egg-rr19.4%
associate-*r*19.4%
*-commutative19.4%
*-commutative19.4%
+-commutative19.4%
count-219.4%
fma-def19.4%
sub-neg19.4%
mul-1-neg19.4%
+-commutative19.4%
associate-+r+79.6%
mul-1-neg79.6%
sub-neg79.6%
+-inverses79.6%
remove-double-neg79.6%
mul-1-neg79.6%
sub-neg79.6%
neg-sub079.6%
mul-1-neg79.6%
remove-double-neg79.6%
Simplified79.6%
Taylor expanded in eps around 0 79.6%
if 0.0 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) Initial program 80.1%
Taylor expanded in x around 0 81.4%
Final simplification77.9%
(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}
Initial program 45.4%
sin-sum68.7%
associate--l+68.7%
Applied egg-rr68.7%
+-commutative68.7%
associate-+l-99.1%
*-commutative99.1%
*-rgt-identity99.1%
distribute-lft-out--99.2%
Simplified99.2%
Taylor expanded in eps around inf 99.2%
fma-neg99.2%
distribute-rgt-neg-in99.2%
neg-sub099.2%
associate--r-99.2%
metadata-eval99.2%
+-commutative99.2%
Simplified99.2%
Final simplification99.2%
(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 45.4%
sin-sum68.7%
associate--l+68.7%
Applied egg-rr68.7%
+-commutative68.7%
associate-+l-99.1%
*-commutative99.1%
*-rgt-identity99.1%
distribute-lft-out--99.2%
Simplified99.2%
Final simplification99.2%
(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(Float64(2.0 * 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[(N[(2.0 * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(0.5 * N[(eps + N[(x + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right) \cdot \cos \left(0.5 \cdot \left(\varepsilon + \left(x + x\right)\right)\right)
\end{array}
Initial program 45.4%
add-sqr-sqrt23.0%
sqrt-unprod22.8%
pow222.8%
Applied egg-rr22.8%
sqrt-pow145.4%
metadata-eval45.4%
pow145.4%
diff-sin45.2%
div-inv45.2%
+-commutative45.2%
associate--l+77.4%
metadata-eval77.4%
div-inv77.4%
associate-+l+77.4%
metadata-eval77.4%
Applied egg-rr77.4%
associate-*r*77.4%
*-commutative77.4%
+-inverses77.4%
+-rgt-identity77.4%
*-commutative77.4%
+-commutative77.4%
associate-+l+77.6%
Simplified77.6%
Final simplification77.6%
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.042) (not (<= eps 0.000205))) (sin eps) (* eps (cos x))))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.042) || !(eps <= 0.000205)) {
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.042d0)) .or. (.not. (eps <= 0.000205d0))) 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.042) || !(eps <= 0.000205)) {
tmp = Math.sin(eps);
} else {
tmp = eps * Math.cos(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.042) or not (eps <= 0.000205): tmp = math.sin(eps) else: tmp = eps * math.cos(x) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.042) || !(eps <= 0.000205)) 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.042) || ~((eps <= 0.000205))) tmp = sin(eps); else tmp = eps * cos(x); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.042], N[Not[LessEqual[eps, 0.000205]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.042 \lor \neg \left(\varepsilon \leq 0.000205\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\end{array}
\end{array}
if eps < -0.0420000000000000026 or 2.05e-4 < eps Initial program 56.8%
Taylor expanded in x around 0 57.8%
if -0.0420000000000000026 < eps < 2.05e-4Initial program 33.3%
Taylor expanded in eps around 0 97.9%
Final simplification77.2%
(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 45.4%
Taylor expanded in x around 0 58.9%
Final simplification58.9%
(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 45.4%
Taylor expanded in eps around 0 49.2%
Taylor expanded in x around 0 30.9%
Final simplification30.9%
(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 2023331
(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)))