
(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 42.3%
sin-sum66.8%
associate--l+66.8%
Applied egg-rr66.8%
+-commutative66.8%
sub-neg66.8%
associate-+l+99.5%
*-commutative99.5%
neg-mul-199.5%
*-commutative99.5%
distribute-rgt-out99.5%
+-commutative99.5%
Simplified99.5%
fma-def99.5%
*-commutative99.5%
Applied egg-rr99.5%
Final simplification99.5%
(FPCore (x eps)
:precision binary64
(let* ((t_0 (- (sin (+ eps x)) (sin x))))
(if (or (<= t_0 -1e-6) (not (<= t_0 2e-104)))
t_0
(* (cos x) (* 2.0 (sin (* eps 0.5)))))))
double code(double x, double eps) {
double t_0 = sin((eps + x)) - sin(x);
double tmp;
if ((t_0 <= -1e-6) || !(t_0 <= 2e-104)) {
tmp = t_0;
} else {
tmp = cos(x) * (2.0 * sin((eps * 0.5)));
}
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 <= (-1d-6)) .or. (.not. (t_0 <= 2d-104))) then
tmp = t_0
else
tmp = cos(x) * (2.0d0 * sin((eps * 0.5d0)))
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 <= -1e-6) || !(t_0 <= 2e-104)) {
tmp = t_0;
} else {
tmp = Math.cos(x) * (2.0 * Math.sin((eps * 0.5)));
}
return tmp;
}
def code(x, eps): t_0 = math.sin((eps + x)) - math.sin(x) tmp = 0 if (t_0 <= -1e-6) or not (t_0 <= 2e-104): tmp = t_0 else: tmp = math.cos(x) * (2.0 * math.sin((eps * 0.5))) return tmp
function code(x, eps) t_0 = Float64(sin(Float64(eps + x)) - sin(x)) tmp = 0.0 if ((t_0 <= -1e-6) || !(t_0 <= 2e-104)) tmp = t_0; else tmp = Float64(cos(x) * Float64(2.0 * sin(Float64(eps * 0.5)))); end return tmp end
function tmp_2 = code(x, eps) t_0 = sin((eps + x)) - sin(x); tmp = 0.0; if ((t_0 <= -1e-6) || ~((t_0 <= 2e-104))) tmp = t_0; else tmp = cos(x) * (2.0 * sin((eps * 0.5))); 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[Or[LessEqual[t$95$0, -1e-6], N[Not[LessEqual[t$95$0, 2e-104]], $MachinePrecision]], t$95$0, N[(N[Cos[x], $MachinePrecision] * N[(2.0 * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(\varepsilon + x\right) - \sin x\\
\mathbf{if}\;t_0 \leq -1 \cdot 10^{-6} \lor \neg \left(t_0 \leq 2 \cdot 10^{-104}\right):\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\cos x \cdot \left(2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)\\
\end{array}
\end{array}
if (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < -9.99999999999999955e-7 or 1.99999999999999985e-104 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) Initial program 70.0%
if -9.99999999999999955e-7 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < 1.99999999999999985e-104Initial program 21.8%
diff-sin21.8%
div-inv21.8%
associate--l+21.8%
metadata-eval21.8%
div-inv21.8%
+-commutative21.8%
associate-+l+21.8%
metadata-eval21.8%
Applied egg-rr21.8%
associate-*r*21.8%
*-commutative21.8%
*-commutative21.8%
+-commutative21.8%
count-221.8%
fma-def21.8%
sub-neg21.8%
mul-1-neg21.8%
+-commutative21.8%
associate-+r+80.5%
mul-1-neg80.5%
sub-neg80.5%
+-inverses80.5%
remove-double-neg80.5%
mul-1-neg80.5%
sub-neg80.5%
neg-sub080.5%
mul-1-neg80.5%
remove-double-neg80.5%
Simplified80.5%
Taylor expanded in eps around 0 80.5%
Final simplification76.0%
(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 42.3%
sin-sum66.8%
associate--l+66.8%
Applied egg-rr66.8%
+-commutative66.8%
sub-neg66.8%
associate-+l+99.5%
*-commutative99.5%
neg-mul-199.5%
*-commutative99.5%
distribute-rgt-out99.5%
+-commutative99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x eps) :precision binary64 (if (or (<= x -0.00065) (not (<= x 0.0004))) (* (cos x) (* 2.0 (sin (* eps 0.5)))) (+ (sin eps) (* x (+ (cos eps) -1.0)))))
double code(double x, double eps) {
double tmp;
if ((x <= -0.00065) || !(x <= 0.0004)) {
tmp = cos(x) * (2.0 * sin((eps * 0.5)));
} else {
tmp = sin(eps) + (x * (cos(eps) + -1.0));
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if ((x <= (-0.00065d0)) .or. (.not. (x <= 0.0004d0))) then
tmp = cos(x) * (2.0d0 * sin((eps * 0.5d0)))
else
tmp = sin(eps) + (x * (cos(eps) + (-1.0d0)))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((x <= -0.00065) || !(x <= 0.0004)) {
tmp = Math.cos(x) * (2.0 * Math.sin((eps * 0.5)));
} else {
tmp = Math.sin(eps) + (x * (Math.cos(eps) + -1.0));
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -0.00065) or not (x <= 0.0004): tmp = math.cos(x) * (2.0 * math.sin((eps * 0.5))) else: tmp = math.sin(eps) + (x * (math.cos(eps) + -1.0)) return tmp
function code(x, eps) tmp = 0.0 if ((x <= -0.00065) || !(x <= 0.0004)) tmp = Float64(cos(x) * Float64(2.0 * sin(Float64(eps * 0.5)))); else tmp = Float64(sin(eps) + Float64(x * Float64(cos(eps) + -1.0))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((x <= -0.00065) || ~((x <= 0.0004))) tmp = cos(x) * (2.0 * sin((eps * 0.5))); else tmp = sin(eps) + (x * (cos(eps) + -1.0)); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -0.00065], N[Not[LessEqual[x, 0.0004]], $MachinePrecision]], N[(N[Cos[x], $MachinePrecision] * N[(2.0 * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Sin[eps], $MachinePrecision] + N[(x * N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.00065 \lor \neg \left(x \leq 0.0004\right):\\
\;\;\;\;\cos x \cdot \left(2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon + x \cdot \left(\cos \varepsilon + -1\right)\\
\end{array}
\end{array}
if x < -6.4999999999999997e-4 or 4.00000000000000019e-4 < x Initial program 6.8%
diff-sin6.2%
div-inv6.2%
associate--l+6.2%
metadata-eval6.2%
div-inv6.2%
+-commutative6.2%
associate-+l+6.3%
metadata-eval6.3%
Applied egg-rr6.3%
associate-*r*6.3%
*-commutative6.3%
*-commutative6.3%
+-commutative6.3%
count-26.3%
fma-def6.3%
sub-neg6.3%
mul-1-neg6.3%
+-commutative6.3%
associate-+r+51.6%
mul-1-neg51.6%
sub-neg51.6%
+-inverses51.6%
remove-double-neg51.6%
mul-1-neg51.6%
sub-neg51.6%
neg-sub051.6%
mul-1-neg51.6%
remove-double-neg51.6%
Simplified51.6%
Taylor expanded in eps around 0 52.0%
if -6.4999999999999997e-4 < x < 4.00000000000000019e-4Initial program 76.7%
Taylor expanded in x around 0 100.0%
Final simplification76.4%
(FPCore (x eps) :precision binary64 (if (<= eps -40000000.0) (sin eps) (if (<= eps 1.15e-5) (* eps (cos x)) (- (sin (+ eps x)) (sin x)))))
double code(double x, double eps) {
double tmp;
if (eps <= -40000000.0) {
tmp = sin(eps);
} else if (eps <= 1.15e-5) {
tmp = eps * cos(x);
} else {
tmp = sin((eps + x)) - sin(x);
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (eps <= (-40000000.0d0)) then
tmp = sin(eps)
else if (eps <= 1.15d-5) then
tmp = eps * cos(x)
else
tmp = sin((eps + x)) - sin(x)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= -40000000.0) {
tmp = Math.sin(eps);
} else if (eps <= 1.15e-5) {
tmp = eps * Math.cos(x);
} else {
tmp = Math.sin((eps + x)) - Math.sin(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -40000000.0: tmp = math.sin(eps) elif eps <= 1.15e-5: tmp = eps * math.cos(x) else: tmp = math.sin((eps + x)) - math.sin(x) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -40000000.0) tmp = sin(eps); elseif (eps <= 1.15e-5) tmp = Float64(eps * cos(x)); else tmp = Float64(sin(Float64(eps + x)) - sin(x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -40000000.0) tmp = sin(eps); elseif (eps <= 1.15e-5) tmp = eps * cos(x); else tmp = sin((eps + x)) - sin(x); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -40000000.0], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 1.15e-5], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision], N[(N[Sin[N[(eps + x), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -40000000:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 1.15 \cdot 10^{-5}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\mathbf{else}:\\
\;\;\;\;\sin \left(\varepsilon + x\right) - \sin x\\
\end{array}
\end{array}
if eps < -4e7Initial program 50.7%
Taylor expanded in x around 0 51.3%
if -4e7 < eps < 1.15e-5Initial program 29.2%
Taylor expanded in eps around 0 97.4%
if 1.15e-5 < eps Initial program 58.3%
Final simplification75.5%
(FPCore (x eps) :precision binary64 (* 2.0 (* (cos (* 0.5 (- eps (* x -2.0)))) (sin (* eps 0.5)))))
double code(double x, double eps) {
return 2.0 * (cos((0.5 * (eps - (x * -2.0)))) * sin((eps * 0.5)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 2.0d0 * (cos((0.5d0 * (eps - (x * (-2.0d0))))) * sin((eps * 0.5d0)))
end function
public static double code(double x, double eps) {
return 2.0 * (Math.cos((0.5 * (eps - (x * -2.0)))) * Math.sin((eps * 0.5)));
}
def code(x, eps): return 2.0 * (math.cos((0.5 * (eps - (x * -2.0)))) * math.sin((eps * 0.5)))
function code(x, eps) return Float64(2.0 * Float64(cos(Float64(0.5 * Float64(eps - Float64(x * -2.0)))) * sin(Float64(eps * 0.5)))) end
function tmp = code(x, eps) tmp = 2.0 * (cos((0.5 * (eps - (x * -2.0)))) * sin((eps * 0.5))); end
code[x_, eps_] := N[(2.0 * N[(N[Cos[N[(0.5 * N[(eps - N[(x * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(\cos \left(0.5 \cdot \left(\varepsilon - x \cdot -2\right)\right) \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)
\end{array}
Initial program 42.3%
sin-sum66.8%
associate--l+66.8%
Applied egg-rr66.8%
+-commutative66.8%
sub-neg66.8%
associate-+l+99.5%
*-commutative99.5%
neg-mul-199.5%
*-commutative99.5%
distribute-rgt-out99.5%
+-commutative99.5%
Simplified99.5%
distribute-lft-in99.5%
associate-+r+66.8%
*-commutative66.8%
sin-sum42.3%
+-commutative42.3%
*-commutative42.3%
neg-mul-142.3%
sub-neg42.3%
diff-sin41.8%
+-commutative41.8%
+-commutative41.8%
Applied egg-rr41.8%
Taylor expanded in x around -inf 75.5%
Final simplification75.5%
(FPCore (x eps) :precision binary64 (if (or (<= eps -40000000.0) (not (<= eps 0.00038))) (sin eps) (* eps (cos x))))
double code(double x, double eps) {
double tmp;
if ((eps <= -40000000.0) || !(eps <= 0.00038)) {
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 <= (-40000000.0d0)) .or. (.not. (eps <= 0.00038d0))) 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 <= -40000000.0) || !(eps <= 0.00038)) {
tmp = Math.sin(eps);
} else {
tmp = eps * Math.cos(x);
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -40000000.0) or not (eps <= 0.00038): tmp = math.sin(eps) else: tmp = eps * math.cos(x) return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -40000000.0) || !(eps <= 0.00038)) tmp = sin(eps); else tmp = Float64(eps * cos(x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -40000000.0) || ~((eps <= 0.00038))) tmp = sin(eps); else tmp = eps * cos(x); end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -40000000.0], N[Not[LessEqual[eps, 0.00038]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -40000000 \lor \neg \left(\varepsilon \leq 0.00038\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\end{array}
\end{array}
if eps < -4e7 or 3.8000000000000002e-4 < eps Initial program 54.7%
Taylor expanded in x around 0 54.9%
if -4e7 < eps < 3.8000000000000002e-4Initial program 29.2%
Taylor expanded in eps around 0 97.4%
Final simplification75.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 42.3%
Taylor expanded in x around 0 55.0%
Final simplification55.0%
(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 42.3%
Taylor expanded in eps around 0 48.8%
Taylor expanded in x around 0 28.3%
Final simplification28.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 2024024
(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)))