
(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 (+ (* (sin eps) (cos x)) (- (* (sin x) (cos eps)) (sin x))))
double code(double x, double eps) {
return (sin(eps) * cos(x)) + ((sin(x) * cos(eps)) - 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) * cos(eps)) - sin(x))
end function
public static double code(double x, double eps) {
return (Math.sin(eps) * Math.cos(x)) + ((Math.sin(x) * Math.cos(eps)) - Math.sin(x));
}
def code(x, eps): return (math.sin(eps) * math.cos(x)) + ((math.sin(x) * math.cos(eps)) - math.sin(x))
function code(x, eps) return Float64(Float64(sin(eps) * cos(x)) + Float64(Float64(sin(x) * cos(eps)) - sin(x))) end
function tmp = code(x, eps) tmp = (sin(eps) * cos(x)) + ((sin(x) * cos(eps)) - sin(x)); end
code[x_, eps_] := N[(N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Sin[x], $MachinePrecision] * N[Cos[eps], $MachinePrecision]), $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \cos x + \left(\sin x \cdot \cos \varepsilon - \sin x\right)
\end{array}
Initial program 41.7%
sin-sum65.9%
associate--l+65.9%
Applied egg-rr65.9%
+-commutative65.9%
sub-neg65.9%
associate-+l+99.6%
*-commutative99.6%
neg-mul-199.6%
*-commutative99.6%
distribute-rgt-out99.6%
+-commutative99.6%
Simplified99.6%
distribute-rgt-in99.6%
neg-mul-199.6%
Applied egg-rr99.6%
Taylor expanded in eps around inf 99.6%
*-commutative99.6%
Simplified99.6%
Final simplification99.6%
(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 41.7%
sin-sum65.9%
associate--l+65.9%
Applied egg-rr65.9%
+-commutative65.9%
sub-neg65.9%
associate-+l+99.6%
*-commutative99.6%
neg-mul-199.6%
*-commutative99.6%
distribute-rgt-out99.6%
+-commutative99.6%
Simplified99.6%
fma-def99.6%
*-commutative99.6%
Applied egg-rr99.6%
Final simplification99.6%
(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 41.7%
sin-sum65.9%
associate--l+65.9%
Applied egg-rr65.9%
+-commutative65.9%
sub-neg65.9%
associate-+l+99.6%
*-commutative99.6%
neg-mul-199.6%
*-commutative99.6%
distribute-rgt-out99.6%
+-commutative99.6%
Simplified99.6%
Final simplification99.6%
(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 41.7%
sin-sum65.9%
associate--l+65.9%
Applied egg-rr65.9%
+-commutative65.9%
sub-neg65.9%
associate-+l+99.6%
*-commutative99.6%
neg-mul-199.6%
*-commutative99.6%
distribute-rgt-out99.6%
+-commutative99.6%
Simplified99.6%
distribute-rgt-in99.6%
neg-mul-199.6%
Applied egg-rr99.6%
Taylor expanded in eps around inf 99.6%
*-commutative99.6%
Simplified99.6%
Taylor expanded in eps around 0 77.2%
Final simplification77.2%
(FPCore (x eps)
:precision binary64
(if (<= eps -0.00012)
(sin eps)
(if (<= eps 3.7e-26)
(* eps (+ (cos x) (* eps (* (sin x) -0.5))))
(sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -0.00012) {
tmp = sin(eps);
} else if (eps <= 3.7e-26) {
tmp = eps * (cos(x) + (eps * (sin(x) * -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) :: tmp
if (eps <= (-0.00012d0)) then
tmp = sin(eps)
else if (eps <= 3.7d-26) then
tmp = eps * (cos(x) + (eps * (sin(x) * (-0.5d0))))
else
tmp = sin(eps)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= -0.00012) {
tmp = Math.sin(eps);
} else if (eps <= 3.7e-26) {
tmp = eps * (Math.cos(x) + (eps * (Math.sin(x) * -0.5)));
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -0.00012: tmp = math.sin(eps) elif eps <= 3.7e-26: tmp = eps * (math.cos(x) + (eps * (math.sin(x) * -0.5))) else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -0.00012) tmp = sin(eps); elseif (eps <= 3.7e-26) tmp = Float64(eps * Float64(cos(x) + Float64(eps * Float64(sin(x) * -0.5)))); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -0.00012) tmp = sin(eps); elseif (eps <= 3.7e-26) tmp = eps * (cos(x) + (eps * (sin(x) * -0.5))); else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -0.00012], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 3.7e-26], N[(eps * N[(N[Cos[x], $MachinePrecision] + N[(eps * N[(N[Sin[x], $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.00012:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 3.7 \cdot 10^{-26}:\\
\;\;\;\;\varepsilon \cdot \left(\cos x + \varepsilon \cdot \left(\sin x \cdot -0.5\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -1.20000000000000003e-4 or 3.6999999999999999e-26 < eps Initial program 52.2%
Taylor expanded in x around 0 53.4%
if -1.20000000000000003e-4 < eps < 3.6999999999999999e-26Initial program 30.7%
sin-sum30.7%
associate--l+30.7%
Applied egg-rr30.7%
+-commutative30.7%
sub-neg30.7%
associate-+l+99.8%
*-commutative99.8%
neg-mul-199.8%
*-commutative99.8%
distribute-rgt-out99.8%
+-commutative99.8%
Simplified99.8%
Taylor expanded in eps around 0 99.8%
+-commutative99.8%
*-commutative99.8%
*-commutative99.8%
associate-*l*99.8%
unpow299.8%
associate-*r*99.8%
distribute-rgt-out99.8%
*-commutative99.8%
Simplified99.8%
Final simplification76.0%
(FPCore (x eps) :precision binary64 (* (cos (* 0.5 (+ eps (+ x x)))) (* 2.0 (sin (* eps 0.5)))))
double code(double x, double eps) {
return cos((0.5 * (eps + (x + x)))) * (2.0 * sin((eps * 0.5)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = cos((0.5d0 * (eps + (x + x)))) * (2.0d0 * sin((eps * 0.5d0)))
end function
public static double code(double x, double eps) {
return Math.cos((0.5 * (eps + (x + x)))) * (2.0 * Math.sin((eps * 0.5)));
}
def code(x, eps): return math.cos((0.5 * (eps + (x + x)))) * (2.0 * math.sin((eps * 0.5)))
function code(x, eps) return Float64(cos(Float64(0.5 * Float64(eps + Float64(x + x)))) * Float64(2.0 * sin(Float64(eps * 0.5)))) end
function tmp = code(x, eps) tmp = cos((0.5 * (eps + (x + x)))) * (2.0 * sin((eps * 0.5))); end
code[x_, eps_] := N[(N[Cos[N[(0.5 * N[(eps + N[(x + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[(2.0 * N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos \left(0.5 \cdot \left(\varepsilon + \left(x + x\right)\right)\right) \cdot \left(2 \cdot \sin \left(\varepsilon \cdot 0.5\right)\right)
\end{array}
Initial program 41.7%
sin-sum65.9%
associate--l+65.9%
Applied egg-rr65.9%
+-commutative65.9%
sub-neg65.9%
associate-+l+99.6%
*-commutative99.6%
neg-mul-199.6%
*-commutative99.6%
distribute-rgt-out99.6%
+-commutative99.6%
Simplified99.6%
*-commutative99.6%
distribute-rgt-in99.6%
neg-mul-199.6%
associate-+r+65.9%
*-commutative65.9%
sin-sum41.7%
+-commutative41.7%
sub-neg41.7%
diff-sin41.2%
div-inv41.2%
+-commutative41.2%
associate--l+76.0%
metadata-eval76.0%
div-inv76.0%
+-commutative76.0%
associate-+l+76.0%
metadata-eval76.0%
Applied egg-rr76.0%
associate-*r*76.0%
*-commutative76.0%
*-commutative76.0%
+-inverses76.0%
+-rgt-identity76.0%
Simplified76.0%
Final simplification76.0%
(FPCore (x eps) :precision binary64 (if (<= eps -0.000135) (sin eps) (if (<= eps 3.7e-26) (* eps (cos x)) (sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -0.000135) {
tmp = sin(eps);
} else if (eps <= 3.7e-26) {
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 <= (-0.000135d0)) then
tmp = sin(eps)
else if (eps <= 3.7d-26) 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 <= -0.000135) {
tmp = Math.sin(eps);
} else if (eps <= 3.7e-26) {
tmp = eps * Math.cos(x);
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -0.000135: tmp = math.sin(eps) elif eps <= 3.7e-26: tmp = eps * math.cos(x) else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -0.000135) tmp = sin(eps); elseif (eps <= 3.7e-26) tmp = Float64(eps * cos(x)); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -0.000135) tmp = sin(eps); elseif (eps <= 3.7e-26) tmp = eps * cos(x); else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -0.000135], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 3.7e-26], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.000135:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 3.7 \cdot 10^{-26}:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -1.35000000000000002e-4 or 3.6999999999999999e-26 < eps Initial program 52.2%
Taylor expanded in x around 0 53.4%
if -1.35000000000000002e-4 < eps < 3.6999999999999999e-26Initial program 30.7%
Taylor expanded in eps around 0 99.8%
Final simplification76.0%
(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.7%
Taylor expanded in x around 0 54.8%
Final simplification54.8%
(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.7%
add-cube-cbrt40.6%
pow340.6%
Applied egg-rr40.6%
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.7%
Taylor expanded in eps around 0 52.0%
+-commutative52.0%
fma-def52.0%
*-commutative52.0%
unpow252.0%
associate-*r*52.0%
Simplified52.0%
Taylor expanded in x around 0 30.8%
Final simplification30.8%
(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 2023287
(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)))