
(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) (+ (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 41.3%
sin-sum64.9%
associate--l+64.9%
Applied egg-rr64.9%
+-commutative64.9%
associate-+l-99.4%
*-commutative99.4%
*-rgt-identity99.4%
distribute-lft-out--99.5%
Simplified99.5%
Taylor expanded in eps around inf 99.5%
fma-neg99.5%
distribute-rgt-neg-in99.5%
sub-neg99.5%
+-commutative99.5%
distribute-neg-in99.5%
remove-double-neg99.5%
metadata-eval99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x eps)
:precision binary64
(let* ((t_0 (- (sin (+ x eps)) (sin x))))
(if (or (<= t_0 -0.042) (not (<= t_0 5e-105)))
t_0
(* (cos x) (* 2.0 (sin (* eps 0.5)))))))
double code(double x, double eps) {
double t_0 = sin((x + eps)) - sin(x);
double tmp;
if ((t_0 <= -0.042) || !(t_0 <= 5e-105)) {
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((x + eps)) - sin(x)
if ((t_0 <= (-0.042d0)) .or. (.not. (t_0 <= 5d-105))) 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((x + eps)) - Math.sin(x);
double tmp;
if ((t_0 <= -0.042) || !(t_0 <= 5e-105)) {
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((x + eps)) - math.sin(x) tmp = 0 if (t_0 <= -0.042) or not (t_0 <= 5e-105): 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(x + eps)) - sin(x)) tmp = 0.0 if ((t_0 <= -0.042) || !(t_0 <= 5e-105)) 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((x + eps)) - sin(x); tmp = 0.0; if ((t_0 <= -0.042) || ~((t_0 <= 5e-105))) 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[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -0.042], N[Not[LessEqual[t$95$0, 5e-105]], $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(x + \varepsilon\right) - \sin x\\
\mathbf{if}\;t_0 \leq -0.042 \lor \neg \left(t_0 \leq 5 \cdot 10^{-105}\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)) < -0.0420000000000000026 or 4.99999999999999963e-105 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) Initial program 71.1%
if -0.0420000000000000026 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < 4.99999999999999963e-105Initial program 19.8%
diff-sin19.8%
div-inv19.8%
associate--l+19.8%
metadata-eval19.8%
div-inv19.8%
+-commutative19.8%
associate-+l+19.8%
metadata-eval19.8%
Applied egg-rr19.8%
associate-*r*19.8%
*-commutative19.8%
*-commutative19.8%
+-commutative19.8%
count-219.8%
fma-def19.8%
sub-neg19.8%
mul-1-neg19.8%
+-commutative19.8%
associate-+r+81.4%
mul-1-neg81.4%
sub-neg81.4%
+-inverses81.4%
remove-double-neg81.4%
mul-1-neg81.4%
sub-neg81.4%
neg-sub081.4%
mul-1-neg81.4%
remove-double-neg81.4%
Simplified81.4%
Taylor expanded in eps around 0 81.5%
Final simplification77.2%
(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 41.3%
sin-sum64.9%
associate--l+64.9%
Applied egg-rr64.9%
+-commutative64.9%
associate-+l-99.4%
*-commutative99.4%
*-rgt-identity99.4%
distribute-lft-out--99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x eps) :precision binary64 (* (cos (/ (+ eps (* x 2.0)) 2.0)) (* 2.0 (sin (/ eps 2.0)))))
double code(double x, double eps) {
return cos(((eps + (x * 2.0)) / 2.0)) * (2.0 * sin((eps / 2.0)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = cos(((eps + (x * 2.0d0)) / 2.0d0)) * (2.0d0 * sin((eps / 2.0d0)))
end function
public static double code(double x, double eps) {
return Math.cos(((eps + (x * 2.0)) / 2.0)) * (2.0 * Math.sin((eps / 2.0)));
}
def code(x, eps): return math.cos(((eps + (x * 2.0)) / 2.0)) * (2.0 * math.sin((eps / 2.0)))
function code(x, eps) return Float64(cos(Float64(Float64(eps + Float64(x * 2.0)) / 2.0)) * Float64(2.0 * sin(Float64(eps / 2.0)))) end
function tmp = code(x, eps) tmp = cos(((eps + (x * 2.0)) / 2.0)) * (2.0 * sin((eps / 2.0))); end
code[x_, eps_] := N[(N[Cos[N[(N[(eps + N[(x * 2.0), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision] * N[(2.0 * N[Sin[N[(eps / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos \left(\frac{\varepsilon + x \cdot 2}{2}\right) \cdot \left(2 \cdot \sin \left(\frac{\varepsilon}{2}\right)\right)
\end{array}
Initial program 41.3%
sin-sum64.9%
associate--l+64.9%
Applied egg-rr64.9%
+-commutative64.9%
associate-+l-99.4%
*-commutative99.4%
*-rgt-identity99.4%
distribute-lft-out--99.5%
Simplified99.5%
Taylor expanded in eps around inf 99.5%
fma-neg99.5%
distribute-rgt-neg-in99.5%
sub-neg99.5%
+-commutative99.5%
distribute-neg-in99.5%
remove-double-neg99.5%
metadata-eval99.5%
Simplified99.5%
fma-udef99.5%
distribute-lft-in99.4%
associate-+r+64.9%
+-commutative64.9%
sin-sum41.3%
*-commutative41.3%
neg-mul-141.3%
sub-neg41.3%
diff-sin41.0%
+-commutative41.0%
+-commutative41.0%
Applied egg-rr41.0%
associate-*r*41.0%
*-commutative41.0%
associate-+l+40.9%
count-240.9%
associate--l+76.7%
+-inverses76.7%
Simplified76.7%
Final simplification76.7%
(FPCore (x eps) :precision binary64 (if (<= eps -0.000215) (- (sin (+ x eps)) (sin x)) (if (<= eps 8e-5) (* (cos x) eps) (sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -0.000215) {
tmp = sin((x + eps)) - sin(x);
} else if (eps <= 8e-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.000215d0)) then
tmp = sin((x + eps)) - sin(x)
else if (eps <= 8d-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.000215) {
tmp = Math.sin((x + eps)) - Math.sin(x);
} else if (eps <= 8e-5) {
tmp = Math.cos(x) * eps;
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -0.000215: tmp = math.sin((x + eps)) - math.sin(x) elif eps <= 8e-5: tmp = math.cos(x) * eps else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -0.000215) tmp = Float64(sin(Float64(x + eps)) - sin(x)); elseif (eps <= 8e-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.000215) tmp = sin((x + eps)) - sin(x); elseif (eps <= 8e-5) tmp = cos(x) * eps; else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -0.000215], N[(N[Sin[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision], If[LessEqual[eps, 8e-5], N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.000215:\\
\;\;\;\;\sin \left(x + \varepsilon\right) - \sin x\\
\mathbf{elif}\;\varepsilon \leq 8 \cdot 10^{-5}:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -2.14999999999999995e-4Initial program 47.9%
if -2.14999999999999995e-4 < eps < 8.00000000000000065e-5Initial program 27.5%
Taylor expanded in eps around 0 98.8%
if 8.00000000000000065e-5 < eps Initial program 60.4%
Taylor expanded in x around 0 61.1%
Final simplification76.3%
(FPCore (x eps) :precision binary64 (if (or (<= eps -0.00034) (not (<= eps 9.5e-5))) (sin eps) (* (cos x) eps)))
double code(double x, double eps) {
double tmp;
if ((eps <= -0.00034) || !(eps <= 9.5e-5)) {
tmp = sin(eps);
} else {
tmp = cos(x) * 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.00034d0)) .or. (.not. (eps <= 9.5d-5))) then
tmp = sin(eps)
else
tmp = cos(x) * eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((eps <= -0.00034) || !(eps <= 9.5e-5)) {
tmp = Math.sin(eps);
} else {
tmp = Math.cos(x) * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (eps <= -0.00034) or not (eps <= 9.5e-5): tmp = math.sin(eps) else: tmp = math.cos(x) * eps return tmp
function code(x, eps) tmp = 0.0 if ((eps <= -0.00034) || !(eps <= 9.5e-5)) tmp = sin(eps); else tmp = Float64(cos(x) * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((eps <= -0.00034) || ~((eps <= 9.5e-5))) tmp = sin(eps); else tmp = cos(x) * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[eps, -0.00034], N[Not[LessEqual[eps, 9.5e-5]], $MachinePrecision]], N[Sin[eps], $MachinePrecision], N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.00034 \lor \neg \left(\varepsilon \leq 9.5 \cdot 10^{-5}\right):\\
\;\;\;\;\sin \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\end{array}
\end{array}
if eps < -3.4e-4 or 9.5000000000000005e-5 < eps Initial program 54.4%
Taylor expanded in x around 0 54.7%
if -3.4e-4 < eps < 9.5000000000000005e-5Initial program 27.5%
Taylor expanded in eps around 0 98.8%
Final simplification76.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 41.3%
Taylor expanded in x around 0 52.5%
Final simplification52.5%
(FPCore (x eps) :precision binary64 0.0)
double code(double x, double eps) {
return 0.0;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 0.0d0
end function
public static double code(double x, double eps) {
return 0.0;
}
def code(x, eps): return 0.0
function code(x, eps) return 0.0 end
function tmp = code(x, eps) tmp = 0.0; end
code[x_, eps_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 41.3%
add-cube-cbrt40.3%
pow340.3%
Applied egg-rr40.3%
Taylor expanded in eps around 0 4.3%
pow-base-14.3%
*-lft-identity4.3%
+-inverses4.3%
Simplified4.3%
Final simplification4.3%
(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 41.3%
Taylor expanded in x around 0 52.5%
Taylor expanded in eps around 0 26.5%
Final simplification26.5%
(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 2023299
(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)))