
(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 (- (* (cos x) (sin eps)) (* (tan (/ eps 2.0)) (* (sin eps) (sin x)))))
double code(double x, double eps) {
return (cos(x) * sin(eps)) - (tan((eps / 2.0)) * (sin(eps) * sin(x)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (cos(x) * sin(eps)) - (tan((eps / 2.0d0)) * (sin(eps) * sin(x)))
end function
public static double code(double x, double eps) {
return (Math.cos(x) * Math.sin(eps)) - (Math.tan((eps / 2.0)) * (Math.sin(eps) * Math.sin(x)));
}
def code(x, eps): return (math.cos(x) * math.sin(eps)) - (math.tan((eps / 2.0)) * (math.sin(eps) * math.sin(x)))
function code(x, eps) return Float64(Float64(cos(x) * sin(eps)) - Float64(tan(Float64(eps / 2.0)) * Float64(sin(eps) * sin(x)))) end
function tmp = code(x, eps) tmp = (cos(x) * sin(eps)) - (tan((eps / 2.0)) * (sin(eps) * sin(x))); end
code[x_, eps_] := N[(N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision]), $MachinePrecision] - N[(N[Tan[N[(eps / 2.0), $MachinePrecision]], $MachinePrecision] * N[(N[Sin[eps], $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos x \cdot \sin \varepsilon - \tan \left(\frac{\varepsilon}{2}\right) \cdot \left(\sin \varepsilon \cdot \sin x\right)
\end{array}
Initial program 45.1%
sin-sum67.3%
associate--l+67.3%
Applied egg-rr67.3%
Taylor expanded in x around inf 67.3%
associate--l+67.3%
*-commutative67.3%
+-commutative67.3%
associate-+l-99.5%
*-rgt-identity99.5%
distribute-lft-out--99.5%
Simplified99.5%
flip--99.3%
associate-*r/99.4%
metadata-eval99.4%
1-sub-cos99.6%
pow299.6%
Applied egg-rr99.6%
*-commutative99.6%
associate-/l*99.6%
Simplified99.6%
Taylor expanded in eps around inf 99.6%
unpow299.6%
associate-*r*99.6%
/-rgt-identity99.6%
associate-/r/99.6%
associate-*l/99.6%
associate-/r/99.6%
/-rgt-identity99.6%
hang-0p-tan99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (x eps)
:precision binary64
(let* ((t_0 (- (sin (+ x eps)) (sin x))))
(if (or (<= t_0 -0.1) (not (<= t_0 0.04)))
t_0
(* (sin (* eps 0.5)) (* (cos x) 2.0)))))
double code(double x, double eps) {
double t_0 = sin((x + eps)) - sin(x);
double tmp;
if ((t_0 <= -0.1) || !(t_0 <= 0.04)) {
tmp = t_0;
} else {
tmp = sin((eps * 0.5)) * (cos(x) * 2.0);
}
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.1d0)) .or. (.not. (t_0 <= 0.04d0))) then
tmp = t_0
else
tmp = sin((eps * 0.5d0)) * (cos(x) * 2.0d0)
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.1) || !(t_0 <= 0.04)) {
tmp = t_0;
} else {
tmp = Math.sin((eps * 0.5)) * (Math.cos(x) * 2.0);
}
return tmp;
}
def code(x, eps): t_0 = math.sin((x + eps)) - math.sin(x) tmp = 0 if (t_0 <= -0.1) or not (t_0 <= 0.04): tmp = t_0 else: tmp = math.sin((eps * 0.5)) * (math.cos(x) * 2.0) return tmp
function code(x, eps) t_0 = Float64(sin(Float64(x + eps)) - sin(x)) tmp = 0.0 if ((t_0 <= -0.1) || !(t_0 <= 0.04)) tmp = t_0; else tmp = Float64(sin(Float64(eps * 0.5)) * Float64(cos(x) * 2.0)); end return tmp end
function tmp_2 = code(x, eps) t_0 = sin((x + eps)) - sin(x); tmp = 0.0; if ((t_0 <= -0.1) || ~((t_0 <= 0.04))) tmp = t_0; else tmp = sin((eps * 0.5)) * (cos(x) * 2.0); 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.1], N[Not[LessEqual[t$95$0, 0.04]], $MachinePrecision]], t$95$0, N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[(N[Cos[x], $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sin \left(x + \varepsilon\right) - \sin x\\
\mathbf{if}\;t_0 \leq -0.1 \lor \neg \left(t_0 \leq 0.04\right):\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\sin \left(\varepsilon \cdot 0.5\right) \cdot \left(\cos x \cdot 2\right)\\
\end{array}
\end{array}
if (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < -0.10000000000000001 or 0.0400000000000000008 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) Initial program 81.2%
if -0.10000000000000001 < (-.f64 (sin.f64 (+.f64 x eps)) (sin.f64 x)) < 0.0400000000000000008Initial program 24.6%
diff-sin24.4%
div-inv24.4%
metadata-eval24.4%
div-inv24.4%
+-commutative24.4%
metadata-eval24.4%
Applied egg-rr24.4%
associate-*r*24.4%
*-commutative24.4%
associate-*l*24.4%
+-commutative24.4%
associate--l+76.7%
+-inverses76.7%
*-commutative76.7%
associate-+r+76.7%
+-commutative76.7%
Simplified76.7%
Taylor expanded in eps around 0 77.3%
Final simplification78.7%
(FPCore (x eps) :precision binary64 (+ (* (cos x) (sin eps)) (- (* (sin x) (cos eps)) (sin x))))
double code(double x, double eps) {
return (cos(x) * sin(eps)) + ((sin(x) * cos(eps)) - sin(x));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (cos(x) * sin(eps)) + ((sin(x) * cos(eps)) - sin(x))
end function
public static double code(double x, double eps) {
return (Math.cos(x) * Math.sin(eps)) + ((Math.sin(x) * Math.cos(eps)) - Math.sin(x));
}
def code(x, eps): return (math.cos(x) * math.sin(eps)) + ((math.sin(x) * math.cos(eps)) - math.sin(x))
function code(x, eps) return Float64(Float64(cos(x) * sin(eps)) + Float64(Float64(sin(x) * cos(eps)) - sin(x))) end
function tmp = code(x, eps) tmp = (cos(x) * sin(eps)) + ((sin(x) * cos(eps)) - sin(x)); end
code[x_, eps_] := N[(N[(N[Cos[x], $MachinePrecision] * N[Sin[eps], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Sin[x], $MachinePrecision] * N[Cos[eps], $MachinePrecision]), $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos x \cdot \sin \varepsilon + \left(\sin x \cdot \cos \varepsilon - \sin x\right)
\end{array}
Initial program 45.1%
sin-sum67.3%
associate--l+67.3%
Applied egg-rr67.3%
Taylor expanded in x around inf 67.3%
associate--l+67.3%
*-commutative67.3%
+-commutative67.3%
associate-+l-99.5%
*-rgt-identity99.5%
distribute-lft-out--99.5%
Simplified99.5%
sub-neg99.5%
distribute-lft-in99.5%
*-commutative99.5%
*-un-lft-identity99.5%
Applied egg-rr99.5%
Final simplification99.5%
(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 45.1%
sin-sum67.3%
associate--l+67.3%
Applied egg-rr67.3%
Taylor expanded in x around inf 67.3%
associate--l+67.3%
*-commutative67.3%
+-commutative67.3%
associate-+l-99.5%
*-rgt-identity99.5%
distribute-lft-out--99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x eps) :precision binary64 (* (sin (* eps 0.5)) (* 2.0 (cos (* 0.5 (+ eps (+ x x)))))))
double code(double x, double eps) {
return sin((eps * 0.5)) * (2.0 * cos((0.5 * (eps + (x + x)))));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin((eps * 0.5d0)) * (2.0d0 * cos((0.5d0 * (eps + (x + x)))))
end function
public static double code(double x, double eps) {
return Math.sin((eps * 0.5)) * (2.0 * Math.cos((0.5 * (eps + (x + x)))));
}
def code(x, eps): return math.sin((eps * 0.5)) * (2.0 * math.cos((0.5 * (eps + (x + x)))))
function code(x, eps) return Float64(sin(Float64(eps * 0.5)) * Float64(2.0 * cos(Float64(0.5 * Float64(eps + Float64(x + x)))))) end
function tmp = code(x, eps) tmp = sin((eps * 0.5)) * (2.0 * cos((0.5 * (eps + (x + x))))); end
code[x_, eps_] := N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[(2.0 * N[Cos[N[(0.5 * N[(eps + N[(x + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \left(\varepsilon \cdot 0.5\right) \cdot \left(2 \cdot \cos \left(0.5 \cdot \left(\varepsilon + \left(x + x\right)\right)\right)\right)
\end{array}
Initial program 45.1%
diff-sin44.8%
div-inv44.8%
metadata-eval44.8%
div-inv44.8%
+-commutative44.8%
metadata-eval44.8%
Applied egg-rr44.8%
associate-*r*44.8%
*-commutative44.8%
associate-*l*44.8%
+-commutative44.8%
associate--l+78.1%
+-inverses78.1%
*-commutative78.1%
associate-+r+78.0%
+-commutative78.0%
Simplified78.0%
Final simplification78.0%
(FPCore (x eps) :precision binary64 (if (<= eps -7e-5) (sin eps) (if (<= eps 0.038) (* (cos x) eps) (sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -7e-5) {
tmp = sin(eps);
} else if (eps <= 0.038) {
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 <= (-7d-5)) then
tmp = sin(eps)
else if (eps <= 0.038d0) 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 <= -7e-5) {
tmp = Math.sin(eps);
} else if (eps <= 0.038) {
tmp = Math.cos(x) * eps;
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -7e-5: tmp = math.sin(eps) elif eps <= 0.038: tmp = math.cos(x) * eps else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -7e-5) tmp = sin(eps); elseif (eps <= 0.038) tmp = Float64(cos(x) * eps); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -7e-5) tmp = sin(eps); elseif (eps <= 0.038) tmp = cos(x) * eps; else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -7e-5], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 0.038], N[(N[Cos[x], $MachinePrecision] * eps), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -7 \cdot 10^{-5}:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 0.038:\\
\;\;\;\;\cos x \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -6.9999999999999994e-5 or 0.0379999999999999991 < eps Initial program 58.4%
Taylor expanded in x around 0 59.9%
if -6.9999999999999994e-5 < eps < 0.0379999999999999991Initial program 31.0%
Taylor expanded in eps around 0 98.4%
Final simplification78.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 45.1%
Taylor expanded in x around 0 60.2%
Final simplification60.2%
(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.1%
diff-sin44.8%
div-inv44.8%
metadata-eval44.8%
div-inv44.8%
+-commutative44.8%
metadata-eval44.8%
Applied egg-rr44.8%
associate-*r*44.8%
*-commutative44.8%
associate-*l*44.8%
+-commutative44.8%
associate--l+78.1%
+-inverses78.1%
*-commutative78.1%
associate-+r+78.0%
+-commutative78.0%
Simplified78.0%
Taylor expanded in eps around 0 49.5%
*-commutative49.5%
Simplified49.5%
Taylor expanded in x around 0 31.1%
Final simplification31.1%
(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 2023285
(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)))