
(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 (- (* (sin eps) (cos x)) (* (* (sin eps) (sin x)) (tan (* eps 0.5)))))
double code(double x, double eps) {
return (sin(eps) * cos(x)) - ((sin(eps) * 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(eps) * 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(eps) * Math.sin(x)) * Math.tan((eps * 0.5)));
}
def code(x, eps): return (math.sin(eps) * math.cos(x)) - ((math.sin(eps) * math.sin(x)) * math.tan((eps * 0.5)))
function code(x, eps) return Float64(Float64(sin(eps) * cos(x)) - Float64(Float64(sin(eps) * sin(x)) * tan(Float64(eps * 0.5)))) end
function tmp = code(x, eps) tmp = (sin(eps) * cos(x)) - ((sin(eps) * sin(x)) * tan((eps * 0.5))); end
code[x_, eps_] := N[(N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision] - N[(N[(N[Sin[eps], $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision] * N[Tan[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \cos x - \left(\sin \varepsilon \cdot \sin x\right) \cdot \tan \left(\varepsilon \cdot 0.5\right)
\end{array}
Initial program 38.3%
sin-sum61.7%
associate--l+61.7%
Applied egg-rr61.7%
+-commutative61.7%
sub-neg61.7%
associate-+l+99.2%
*-commutative99.2%
neg-mul-199.2%
*-commutative99.2%
distribute-rgt-out99.2%
+-commutative99.2%
Simplified99.2%
flip-+99.2%
frac-2neg99.2%
metadata-eval99.2%
sub-1-cos99.5%
pow299.5%
sub-neg99.5%
metadata-eval99.5%
Applied egg-rr99.5%
remove-double-neg99.5%
+-commutative99.5%
distribute-neg-in99.5%
metadata-eval99.5%
sub-neg99.5%
Simplified99.5%
Taylor expanded in eps around inf 99.5%
metadata-eval99.5%
+-commutative99.5%
times-frac99.5%
*-lft-identity99.5%
unpow299.5%
times-frac99.5%
+-commutative99.5%
hang-0p-tan99.7%
Simplified99.7%
expm1-log1p-u95.4%
expm1-udef94.9%
div-inv94.9%
metadata-eval94.9%
div-inv94.9%
metadata-eval94.9%
Applied egg-rr94.9%
expm1-def95.4%
expm1-log1p99.7%
associate-*l*99.7%
associate-*r*99.7%
*-commutative99.7%
mul-1-neg99.7%
*-commutative99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (x eps) :precision binary64 (fma (sin eps) (cos x) (* (sin x) (+ (cos eps) -1.0))))
double code(double x, double eps) {
return fma(sin(eps), cos(x), (sin(x) * (cos(eps) + -1.0)));
}
function code(x, eps) return fma(sin(eps), cos(x), Float64(sin(x) * Float64(cos(eps) + -1.0))) end
code[x_, eps_] := N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sin \varepsilon, \cos x, \sin x \cdot \left(\cos \varepsilon + -1\right)\right)
\end{array}
Initial program 38.3%
sin-sum61.7%
associate--l+61.7%
Applied egg-rr61.7%
+-commutative61.7%
sub-neg61.7%
associate-+l+99.2%
*-commutative99.2%
neg-mul-199.2%
*-commutative99.2%
distribute-rgt-out99.2%
+-commutative99.2%
Simplified99.2%
fma-def99.3%
*-commutative99.3%
Applied egg-rr99.3%
Final simplification99.3%
(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 38.3%
sin-sum61.7%
associate--l+61.7%
Applied egg-rr61.7%
+-commutative61.7%
sub-neg61.7%
associate-+l+99.2%
*-commutative99.2%
neg-mul-199.2%
*-commutative99.2%
distribute-rgt-out99.2%
+-commutative99.2%
Simplified99.2%
+-commutative99.2%
*-commutative99.2%
*-commutative99.2%
fma-def99.3%
*-commutative99.3%
Applied egg-rr99.3%
Final simplification99.3%
(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 38.3%
sin-sum61.7%
associate--l+61.7%
Applied egg-rr61.7%
+-commutative61.7%
sub-neg61.7%
associate-+l+99.2%
*-commutative99.2%
neg-mul-199.2%
*-commutative99.2%
distribute-rgt-out99.2%
+-commutative99.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(2.0 * Float64(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[(2.0 * N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(0.5 * N[(eps + N[(x + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(\sin \left(\varepsilon \cdot 0.5\right) \cdot \cos \left(0.5 \cdot \left(\varepsilon + \left(x + x\right)\right)\right)\right)
\end{array}
Initial program 38.3%
diff-sin37.6%
div-inv37.6%
metadata-eval37.6%
div-inv37.6%
+-commutative37.6%
metadata-eval37.6%
Applied egg-rr37.6%
*-commutative37.6%
+-commutative37.6%
associate--l+76.1%
+-inverses76.1%
distribute-lft-in76.1%
metadata-eval76.1%
*-commutative76.1%
associate-+r+76.2%
+-commutative76.2%
Simplified76.2%
Final simplification76.2%
(FPCore (x eps) :precision binary64 (if (<= eps -1.8e-6) (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 <= -1.8e-6) {
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 <= (-1.8d-6)) 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 <= -1.8e-6) {
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 <= -1.8e-6: 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 <= -1.8e-6) 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 <= -1.8e-6) 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, -1.8e-6], 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 -1.8 \cdot 10^{-6}:\\
\;\;\;\;\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 < -1.79999999999999992e-6Initial program 46.1%
Taylor expanded in x around 0 46.6%
if -1.79999999999999992e-6 < eps < 1.15e-5Initial program 29.3%
Taylor expanded in eps around 0 98.8%
if 1.15e-5 < eps Initial program 52.1%
Final simplification76.4%
(FPCore (x eps) :precision binary64 (if (<= eps -1.85e-5) (sin eps) (if (<= eps 0.000105) (* eps (cos x)) (sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -1.85e-5) {
tmp = sin(eps);
} else if (eps <= 0.000105) {
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 <= (-1.85d-5)) then
tmp = sin(eps)
else if (eps <= 0.000105d0) 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 <= -1.85e-5) {
tmp = Math.sin(eps);
} else if (eps <= 0.000105) {
tmp = eps * Math.cos(x);
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -1.85e-5: tmp = math.sin(eps) elif eps <= 0.000105: tmp = eps * math.cos(x) else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -1.85e-5) tmp = sin(eps); elseif (eps <= 0.000105) tmp = Float64(eps * cos(x)); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -1.85e-5) tmp = sin(eps); elseif (eps <= 0.000105) tmp = eps * cos(x); else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -1.85e-5], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 0.000105], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -1.85 \cdot 10^{-5}:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 0.000105:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -1.84999999999999991e-5 or 1.05e-4 < eps Initial program 49.1%
Taylor expanded in x around 0 49.2%
if -1.84999999999999991e-5 < eps < 1.05e-4Initial program 29.3%
Taylor expanded in eps around 0 98.8%
Final simplification76.3%
(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 38.3%
Taylor expanded in x around 0 52.8%
Final simplification52.8%
(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 38.3%
Taylor expanded in eps around 0 18.2%
Taylor expanded in x around 0 32.2%
Final simplification32.2%
(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 2023238
(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)))