
(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 10 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 eps) -1.0) (sin x) (* (sin eps) (cos x))))
double code(double x, double eps) {
return fma((cos(eps) + -1.0), sin(x), (sin(eps) * cos(x)));
}
function code(x, eps) return fma(Float64(cos(eps) + -1.0), sin(x), Float64(sin(eps) * cos(x))) end
code[x_, eps_] := N[(N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision] * N[Sin[x], $MachinePrecision] + N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\cos \varepsilon + -1, \sin x, \sin \varepsilon \cdot \cos x\right)
\end{array}
Initial program 40.5%
add-exp-log20.6%
Applied egg-rr20.6%
rem-exp-log40.5%
+-commutative40.5%
sin-sum65.7%
*-commutative65.7%
associate--l+99.3%
Applied egg-rr99.3%
Taylor expanded in x around inf 99.3%
sub-neg99.3%
neg-mul-199.3%
distribute-rgt-in99.3%
metadata-eval99.3%
sub-neg99.3%
*-commutative99.3%
sub-neg99.3%
metadata-eval99.3%
Simplified99.3%
+-commutative99.3%
fma-def99.3%
Applied egg-rr99.3%
Final simplification99.3%
(FPCore (x eps)
:precision binary64
(let* ((t_0 (- (sin (+ eps x)) (sin x))))
(if (<= t_0 -0.05)
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.05) {
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.05d0)) 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.05) {
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.05: 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.05) tmp = t_0; elseif (t_0 <= 0.0) tmp = Float64(Float64(cos(x) * 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.05) 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.05], t$95$0, If[LessEqual[t$95$0, 0.0], N[(N[(N[Cos[x], $MachinePrecision] * 2.0), $MachinePrecision] * N[Sin[N[(eps * 0.5), $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.05:\\
\;\;\;\;t_0\\
\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;\left(\cos x \cdot 2\right) \cdot \sin \left(\varepsilon \cdot 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < -0.050000000000000003Initial program 67.4%
if -0.050000000000000003 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < 0.0Initial program 18.1%
diff-sin18.1%
div-inv18.1%
associate--l+18.1%
metadata-eval18.1%
div-inv18.1%
+-commutative18.1%
associate-+l+18.1%
metadata-eval18.1%
Applied egg-rr18.1%
associate-*r*18.1%
*-commutative18.1%
*-commutative18.1%
+-commutative18.1%
count-218.1%
fma-def18.1%
associate-+r-18.1%
+-commutative18.1%
associate--l+81.2%
+-inverses81.2%
Simplified81.2%
add-cbrt-cube80.9%
pow380.9%
Applied egg-rr80.9%
Taylor expanded in eps around 0 81.0%
Taylor expanded in x around inf 81.3%
associate-*r*81.3%
*-commutative81.3%
Simplified81.3%
if 0.0 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) Initial program 69.2%
Taylor expanded in x around 0 69.5%
Final simplification75.7%
(FPCore (x eps) :precision binary64 (let* ((t_0 (- (sin (+ eps x)) (sin x)))) (if (<= t_0 -0.05) t_0 (if (<= t_0 0.0) (* eps (cos x)) (sin eps)))))
double code(double x, double eps) {
double t_0 = sin((eps + x)) - sin(x);
double tmp;
if (t_0 <= -0.05) {
tmp = t_0;
} else if (t_0 <= 0.0) {
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) :: t_0
real(8) :: tmp
t_0 = sin((eps + x)) - sin(x)
if (t_0 <= (-0.05d0)) then
tmp = t_0
else if (t_0 <= 0.0d0) then
tmp = eps * cos(x)
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.05) {
tmp = t_0;
} else if (t_0 <= 0.0) {
tmp = eps * Math.cos(x);
} 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.05: tmp = t_0 elif t_0 <= 0.0: tmp = eps * math.cos(x) 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.05) tmp = t_0; elseif (t_0 <= 0.0) tmp = Float64(eps * cos(x)); 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.05) tmp = t_0; elseif (t_0 <= 0.0) tmp = eps * cos(x); 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.05], t$95$0, If[LessEqual[t$95$0, 0.0], N[(eps * N[Cos[x], $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.05:\\
\;\;\;\;t_0\\
\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < -0.050000000000000003Initial program 67.4%
if -0.050000000000000003 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < 0.0Initial program 18.1%
Taylor expanded in eps around 0 80.0%
if 0.0 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) Initial program 69.2%
Taylor expanded in x around 0 69.5%
Final simplification74.9%
(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%
add-exp-log20.6%
Applied egg-rr20.6%
rem-exp-log40.5%
+-commutative40.5%
sin-sum65.7%
*-commutative65.7%
associate--l+99.3%
Applied egg-rr99.3%
Taylor expanded in x around inf 99.3%
sub-neg99.3%
neg-mul-199.3%
distribute-rgt-in99.3%
metadata-eval99.3%
sub-neg99.3%
*-commutative99.3%
sub-neg99.3%
metadata-eval99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (x eps) :precision binary64 (+ (* (sin eps) (cos x)) (- (sin x) (sin x))))
double code(double x, double eps) {
return (sin(eps) * cos(x)) + (sin(x) - sin(x));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (sin(eps) * cos(x)) + (sin(x) - sin(x))
end function
public static double code(double x, double eps) {
return (Math.sin(eps) * Math.cos(x)) + (Math.sin(x) - Math.sin(x));
}
def code(x, eps): return (math.sin(eps) * math.cos(x)) + (math.sin(x) - math.sin(x))
function code(x, eps) return Float64(Float64(sin(eps) * cos(x)) + Float64(sin(x) - sin(x))) end
function tmp = code(x, eps) tmp = (sin(eps) * cos(x)) + (sin(x) - sin(x)); end
code[x_, eps_] := N[(N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \cos x + \left(\sin x - \sin x\right)
\end{array}
Initial program 40.5%
add-exp-log20.6%
Applied egg-rr20.6%
rem-exp-log40.5%
+-commutative40.5%
sin-sum65.7%
*-commutative65.7%
associate--l+99.3%
Applied egg-rr99.3%
Taylor expanded in eps around 0 75.5%
Final simplification75.5%
(FPCore (x eps) :precision binary64 (* (* 2.0 (sin (* eps 0.5))) (cos (* 0.5 (fma 2.0 x eps)))))
double code(double x, double eps) {
return (2.0 * sin((eps * 0.5))) * cos((0.5 * fma(2.0, x, eps)));
}
function code(x, eps) return Float64(Float64(2.0 * sin(Float64(eps * 0.5))) * cos(Float64(0.5 * fma(2.0, x, eps)))) end
code[x_, eps_] := N[(N[(2.0 * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(0.5 * N[(2.0 * x + eps), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right) \cdot \cos \left(0.5 \cdot \mathsf{fma}\left(2, x, \varepsilon\right)\right)
\end{array}
Initial program 40.5%
diff-sin40.2%
div-inv40.2%
associate--l+40.2%
metadata-eval40.2%
div-inv40.2%
+-commutative40.2%
associate-+l+40.4%
metadata-eval40.4%
Applied egg-rr40.4%
associate-*r*40.4%
*-commutative40.4%
*-commutative40.4%
+-commutative40.4%
count-240.4%
fma-def40.4%
associate-+r-40.4%
+-commutative40.4%
associate--l+75.3%
+-inverses75.3%
Simplified75.3%
Taylor expanded in x around inf 75.3%
*-commutative75.3%
associate-*r*75.3%
*-commutative75.3%
Simplified75.3%
Final simplification75.3%
(FPCore (x eps) :precision binary64 (* 2.0 (* (sin (* 0.5 (+ eps (- x x)))) (cos (* 0.5 (+ x (+ eps x)))))))
double code(double x, double eps) {
return 2.0 * (sin((0.5 * (eps + (x - x)))) * cos((0.5 * (x + (eps + x)))));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 2.0d0 * (sin((0.5d0 * (eps + (x - x)))) * cos((0.5d0 * (x + (eps + x)))))
end function
public static double code(double x, double eps) {
return 2.0 * (Math.sin((0.5 * (eps + (x - x)))) * Math.cos((0.5 * (x + (eps + x)))));
}
def code(x, eps): return 2.0 * (math.sin((0.5 * (eps + (x - x)))) * math.cos((0.5 * (x + (eps + x)))))
function code(x, eps) return Float64(2.0 * Float64(sin(Float64(0.5 * Float64(eps + Float64(x - x)))) * cos(Float64(0.5 * Float64(x + Float64(eps + x)))))) end
function tmp = code(x, eps) tmp = 2.0 * (sin((0.5 * (eps + (x - x)))) * cos((0.5 * (x + (eps + x))))); end
code[x_, eps_] := N[(2.0 * N[(N[Sin[N[(0.5 * N[(eps + N[(x - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(0.5 * N[(x + N[(eps + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(\sin \left(0.5 \cdot \left(\varepsilon + \left(x - x\right)\right)\right) \cdot \cos \left(0.5 \cdot \left(x + \left(\varepsilon + x\right)\right)\right)\right)
\end{array}
Initial program 40.5%
add-exp-log20.6%
Applied egg-rr20.6%
rem-exp-log40.5%
+-commutative40.5%
sin-sum65.7%
*-commutative65.7%
associate--l+99.3%
Applied egg-rr99.3%
associate-+r-65.7%
*-commutative65.7%
sin-sum40.5%
+-commutative40.5%
diff-sin40.2%
*-commutative40.2%
div-inv40.2%
+-commutative40.2%
associate--l+75.3%
metadata-eval75.3%
div-inv75.3%
+-commutative75.3%
metadata-eval75.3%
Applied egg-rr75.3%
Final simplification75.3%
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.0004) (not (<= eps 0.000105))) (sin eps) (* eps (cos x))))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.0004) || !(eps <= 0.000105)) {
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.0004d0)) .or. (.not. (eps <= 0.000105d0))) 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.0004) || !(eps <= 0.000105)) {
tmp = Math.sin(eps);
} else {
tmp = eps * Math.cos(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.0004) or not (eps <= 0.000105): tmp = math.sin(eps) else: tmp = eps * math.cos(x) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.0004) || !(eps <= 0.000105)) 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.0004) || ~((eps <= 0.000105))) tmp = sin(eps); else tmp = eps * cos(x); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.0004], N[Not[LessEqual[eps, 0.000105]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.0004 \lor \neg \left(\varepsilon \leq 0.000105\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\end{array}
\end{array}
if eps < -4.00000000000000019e-4 or 1.05e-4 < eps Initial program 51.5%
Taylor expanded in x around 0 51.9%
if -4.00000000000000019e-4 < eps < 1.05e-4Initial program 29.2%
Taylor expanded in eps around 0 98.6%
Final simplification74.9%
(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.7%
Final simplification52.7%
(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 50.2%
Taylor expanded in x around 0 28.1%
Final simplification28.1%
(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 2023319
(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)))