
(FPCore (x eps) :precision binary64 (- (cos (+ x eps)) (cos x)))
double code(double x, double eps) {
return cos((x + eps)) - cos(x);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = cos((x + eps)) - cos(x)
end function
public static double code(double x, double eps) {
return Math.cos((x + eps)) - Math.cos(x);
}
def code(x, eps): return math.cos((x + eps)) - math.cos(x)
function code(x, eps) return Float64(cos(Float64(x + eps)) - cos(x)) end
function tmp = code(x, eps) tmp = cos((x + eps)) - cos(x); end
code[x_, eps_] := N[(N[Cos[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos \left(x + \varepsilon\right) - \cos x
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 12 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x eps) :precision binary64 (- (cos (+ x eps)) (cos x)))
double code(double x, double eps) {
return cos((x + eps)) - cos(x);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = cos((x + eps)) - cos(x)
end function
public static double code(double x, double eps) {
return Math.cos((x + eps)) - Math.cos(x);
}
def code(x, eps): return math.cos((x + eps)) - math.cos(x)
function code(x, eps) return Float64(cos(Float64(x + eps)) - cos(x)) end
function tmp = code(x, eps) tmp = cos((x + eps)) - cos(x); end
code[x_, eps_] := N[(N[Cos[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\cos \left(x + \varepsilon\right) - \cos x
\end{array}
(FPCore (x eps) :precision binary64 (* -2.0 (* (sin (* eps 0.5)) (sin (fma eps 0.5 x)))))
double code(double x, double eps) {
return -2.0 * (sin((eps * 0.5)) * sin(fma(eps, 0.5, x)));
}
function code(x, eps) return Float64(-2.0 * Float64(sin(Float64(eps * 0.5)) * sin(fma(eps, 0.5, x)))) end
code[x_, eps_] := N[(-2.0 * N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(eps * 0.5 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-2 \cdot \left(\sin \left(\varepsilon \cdot 0.5\right) \cdot \sin \left(\mathsf{fma}\left(\varepsilon, 0.5, x\right)\right)\right)
\end{array}
Initial program 54.4%
lift--.f64N/A
lift-cos.f64N/A
lift-cos.f64N/A
diff-cosN/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.7%
Taylor expanded in eps around inf
*-commutativeN/A
metadata-evalN/A
cancel-sign-sub-invN/A
lower-*.f64N/A
lower-sin.f64N/A
cancel-sign-sub-invN/A
metadata-evalN/A
distribute-rgt-inN/A
*-commutativeN/A
associate-*l*N/A
metadata-evalN/A
*-rgt-identityN/A
lower-fma.f64N/A
lower-sin.f64N/A
*-commutativeN/A
lower-*.f6499.7
Applied rewrites99.7%
Final simplification99.7%
(FPCore (x eps) :precision binary64 (* (* (* (fma (* eps eps) -0.020833333333333332 0.5) eps) (sin (* (fma 2.0 x eps) 0.5))) -2.0))
double code(double x, double eps) {
return ((fma((eps * eps), -0.020833333333333332, 0.5) * eps) * sin((fma(2.0, x, eps) * 0.5))) * -2.0;
}
function code(x, eps) return Float64(Float64(Float64(fma(Float64(eps * eps), -0.020833333333333332, 0.5) * eps) * sin(Float64(fma(2.0, x, eps) * 0.5))) * -2.0) end
code[x_, eps_] := N[(N[(N[(N[(N[(eps * eps), $MachinePrecision] * -0.020833333333333332 + 0.5), $MachinePrecision] * eps), $MachinePrecision] * N[Sin[N[(N[(2.0 * x + eps), $MachinePrecision] * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * -2.0), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(\mathsf{fma}\left(\varepsilon \cdot \varepsilon, -0.020833333333333332, 0.5\right) \cdot \varepsilon\right) \cdot \sin \left(\mathsf{fma}\left(2, x, \varepsilon\right) \cdot 0.5\right)\right) \cdot -2
\end{array}
Initial program 54.4%
lift--.f64N/A
lift-cos.f64N/A
lift-cos.f64N/A
diff-cosN/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.7%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6499.4
Applied rewrites99.4%
Final simplification99.4%
(FPCore (x eps) :precision binary64 (* (* (* eps 0.5) (sin (* (fma 2.0 x eps) 0.5))) -2.0))
double code(double x, double eps) {
return ((eps * 0.5) * sin((fma(2.0, x, eps) * 0.5))) * -2.0;
}
function code(x, eps) return Float64(Float64(Float64(eps * 0.5) * sin(Float64(fma(2.0, x, eps) * 0.5))) * -2.0) end
code[x_, eps_] := N[(N[(N[(eps * 0.5), $MachinePrecision] * N[Sin[N[(N[(2.0 * x + eps), $MachinePrecision] * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * -2.0), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(\varepsilon \cdot 0.5\right) \cdot \sin \left(\mathsf{fma}\left(2, x, \varepsilon\right) \cdot 0.5\right)\right) \cdot -2
\end{array}
Initial program 54.4%
lift--.f64N/A
lift-cos.f64N/A
lift-cos.f64N/A
diff-cosN/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.7%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f6499.3
Applied rewrites99.3%
Final simplification99.3%
(FPCore (x eps) :precision binary64 (fma (- (sin x)) eps (* (* -0.5 eps) eps)))
double code(double x, double eps) {
return fma(-sin(x), eps, ((-0.5 * eps) * eps));
}
function code(x, eps) return fma(Float64(-sin(x)), eps, Float64(Float64(-0.5 * eps) * eps)) end
code[x_, eps_] := N[((-N[Sin[x], $MachinePrecision]) * eps + N[(N[(-0.5 * eps), $MachinePrecision] * eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-\sin x, \varepsilon, \left(-0.5 \cdot \varepsilon\right) \cdot \varepsilon\right)
\end{array}
Initial program 54.4%
Taylor expanded in x around 0
lower--.f64N/A
lower-cos.f6453.3
Applied rewrites53.3%
Taylor expanded in eps around 0
*-commutativeN/A
sub-negN/A
mul-1-negN/A
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f64N/A
associate-*r*N/A
*-commutativeN/A
+-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f64N/A
lower-cos.f64N/A
mul-1-negN/A
lower-neg.f64N/A
lower-sin.f6499.4
Applied rewrites99.4%
Applied rewrites99.5%
Taylor expanded in x around 0
Applied rewrites99.0%
(FPCore (x eps) :precision binary64 (* (fma (* -0.5 eps) 1.0 (- (sin x))) eps))
double code(double x, double eps) {
return fma((-0.5 * eps), 1.0, -sin(x)) * eps;
}
function code(x, eps) return Float64(fma(Float64(-0.5 * eps), 1.0, Float64(-sin(x))) * eps) end
code[x_, eps_] := N[(N[(N[(-0.5 * eps), $MachinePrecision] * 1.0 + (-N[Sin[x], $MachinePrecision])), $MachinePrecision] * eps), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-0.5 \cdot \varepsilon, 1, -\sin x\right) \cdot \varepsilon
\end{array}
Initial program 54.4%
Taylor expanded in x around 0
lower--.f64N/A
lower-cos.f6453.3
Applied rewrites53.3%
Taylor expanded in eps around 0
*-commutativeN/A
sub-negN/A
mul-1-negN/A
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f64N/A
associate-*r*N/A
*-commutativeN/A
+-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f64N/A
lower-cos.f64N/A
mul-1-negN/A
lower-neg.f64N/A
lower-sin.f6499.4
Applied rewrites99.4%
Taylor expanded in x around 0
Applied rewrites98.9%
(FPCore (x eps) :precision binary64 (fma (fma (* (fma 0.25 eps (* 0.16666666666666666 x)) eps) x (- eps)) x (* -0.5 (* eps eps))))
double code(double x, double eps) {
return fma(fma((fma(0.25, eps, (0.16666666666666666 * x)) * eps), x, -eps), x, (-0.5 * (eps * eps)));
}
function code(x, eps) return fma(fma(Float64(fma(0.25, eps, Float64(0.16666666666666666 * x)) * eps), x, Float64(-eps)), x, Float64(-0.5 * Float64(eps * eps))) end
code[x_, eps_] := N[(N[(N[(N[(0.25 * eps + N[(0.16666666666666666 * x), $MachinePrecision]), $MachinePrecision] * eps), $MachinePrecision] * x + (-eps)), $MachinePrecision] * x + N[(-0.5 * N[(eps * eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, \varepsilon, 0.16666666666666666 \cdot x\right) \cdot \varepsilon, x, -\varepsilon\right), x, -0.5 \cdot \left(\varepsilon \cdot \varepsilon\right)\right)
\end{array}
Initial program 54.4%
Taylor expanded in x around 0
lower--.f64N/A
lower-cos.f6453.3
Applied rewrites53.3%
Taylor expanded in eps around 0
*-commutativeN/A
sub-negN/A
mul-1-negN/A
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f64N/A
associate-*r*N/A
*-commutativeN/A
+-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f64N/A
lower-cos.f64N/A
mul-1-negN/A
lower-neg.f64N/A
lower-sin.f6499.4
Applied rewrites99.4%
Applied rewrites99.5%
Taylor expanded in x around 0
Applied rewrites98.3%
Final simplification98.3%
(FPCore (x eps) :precision binary64 (* (fma (fma (fma 0.16666666666666666 x (* 0.25 eps)) x -1.0) x (* -0.5 eps)) eps))
double code(double x, double eps) {
return fma(fma(fma(0.16666666666666666, x, (0.25 * eps)), x, -1.0), x, (-0.5 * eps)) * eps;
}
function code(x, eps) return Float64(fma(fma(fma(0.16666666666666666, x, Float64(0.25 * eps)), x, -1.0), x, Float64(-0.5 * eps)) * eps) end
code[x_, eps_] := N[(N[(N[(N[(0.16666666666666666 * x + N[(0.25 * eps), $MachinePrecision]), $MachinePrecision] * x + -1.0), $MachinePrecision] * x + N[(-0.5 * eps), $MachinePrecision]), $MachinePrecision] * eps), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.16666666666666666, x, 0.25 \cdot \varepsilon\right), x, -1\right), x, -0.5 \cdot \varepsilon\right) \cdot \varepsilon
\end{array}
Initial program 54.4%
Taylor expanded in x around 0
lower--.f64N/A
lower-cos.f6453.3
Applied rewrites53.3%
Taylor expanded in eps around 0
*-commutativeN/A
sub-negN/A
mul-1-negN/A
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f64N/A
associate-*r*N/A
*-commutativeN/A
+-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f64N/A
lower-cos.f64N/A
mul-1-negN/A
lower-neg.f64N/A
lower-sin.f6499.4
Applied rewrites99.4%
Taylor expanded in x around 0
Applied rewrites98.2%
(FPCore (x eps) :precision binary64 (fma (* (fma (* eps x) 0.25 -1.0) eps) x (* -0.5 (* eps eps))))
double code(double x, double eps) {
return fma((fma((eps * x), 0.25, -1.0) * eps), x, (-0.5 * (eps * eps)));
}
function code(x, eps) return fma(Float64(fma(Float64(eps * x), 0.25, -1.0) * eps), x, Float64(-0.5 * Float64(eps * eps))) end
code[x_, eps_] := N[(N[(N[(N[(eps * x), $MachinePrecision] * 0.25 + -1.0), $MachinePrecision] * eps), $MachinePrecision] * x + N[(-0.5 * N[(eps * eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\mathsf{fma}\left(\varepsilon \cdot x, 0.25, -1\right) \cdot \varepsilon, x, -0.5 \cdot \left(\varepsilon \cdot \varepsilon\right)\right)
\end{array}
Initial program 54.4%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.4%
Taylor expanded in x around 0
Applied rewrites97.9%
Taylor expanded in eps around 0
Applied rewrites97.9%
Final simplification97.9%
(FPCore (x eps) :precision binary64 (fma (- eps) x (* -0.5 (* eps eps))))
double code(double x, double eps) {
return fma(-eps, x, (-0.5 * (eps * eps)));
}
function code(x, eps) return fma(Float64(-eps), x, Float64(-0.5 * Float64(eps * eps))) end
code[x_, eps_] := N[((-eps) * x + N[(-0.5 * N[(eps * eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-\varepsilon, x, -0.5 \cdot \left(\varepsilon \cdot \varepsilon\right)\right)
\end{array}
Initial program 54.4%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.4%
Taylor expanded in x around 0
Applied rewrites97.9%
Taylor expanded in eps around 0
Applied rewrites97.9%
Final simplification97.9%
(FPCore (x eps) :precision binary64 (* (fma -0.5 eps (- x)) eps))
double code(double x, double eps) {
return fma(-0.5, eps, -x) * eps;
}
function code(x, eps) return Float64(fma(-0.5, eps, Float64(-x)) * eps) end
code[x_, eps_] := N[(N[(-0.5 * eps + (-x)), $MachinePrecision] * eps), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-0.5, \varepsilon, -x\right) \cdot \varepsilon
\end{array}
Initial program 54.4%
Taylor expanded in x around 0
lower--.f64N/A
lower-cos.f6453.3
Applied rewrites53.3%
Taylor expanded in eps around 0
*-commutativeN/A
sub-negN/A
mul-1-negN/A
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f64N/A
associate-*r*N/A
*-commutativeN/A
+-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f64N/A
lower-cos.f64N/A
mul-1-negN/A
lower-neg.f64N/A
lower-sin.f6499.4
Applied rewrites99.4%
Taylor expanded in x around 0
Applied rewrites97.8%
(FPCore (x eps) :precision binary64 (* (- x) eps))
double code(double x, double eps) {
return -x * eps;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = -x * eps
end function
public static double code(double x, double eps) {
return -x * eps;
}
def code(x, eps): return -x * eps
function code(x, eps) return Float64(Float64(-x) * eps) end
function tmp = code(x, eps) tmp = -x * eps; end
code[x_, eps_] := N[((-x) * eps), $MachinePrecision]
\begin{array}{l}
\\
\left(-x\right) \cdot \varepsilon
\end{array}
Initial program 54.4%
Taylor expanded in eps around 0
associate-*r*N/A
lower-*.f64N/A
mul-1-negN/A
lower-neg.f64N/A
lower-sin.f6480.6
Applied rewrites80.6%
Taylor expanded in x around 0
Applied rewrites79.7%
(FPCore (x eps) :precision binary64 (- 1.0 1.0))
double code(double x, double eps) {
return 1.0 - 1.0;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 1.0d0 - 1.0d0
end function
public static double code(double x, double eps) {
return 1.0 - 1.0;
}
def code(x, eps): return 1.0 - 1.0
function code(x, eps) return Float64(1.0 - 1.0) end
function tmp = code(x, eps) tmp = 1.0 - 1.0; end
code[x_, eps_] := N[(1.0 - 1.0), $MachinePrecision]
\begin{array}{l}
\\
1 - 1
\end{array}
Initial program 54.4%
Taylor expanded in x around 0
lower--.f64N/A
lower-cos.f6453.3
Applied rewrites53.3%
Taylor expanded in eps around 0
Applied rewrites53.1%
(FPCore (x eps) :precision binary64 (* (* -2.0 (sin (+ x (/ eps 2.0)))) (sin (/ eps 2.0))))
double code(double x, double eps) {
return (-2.0 * sin((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) * sin((x + (eps / 2.0d0)))) * sin((eps / 2.0d0))
end function
public static double code(double x, double eps) {
return (-2.0 * Math.sin((x + (eps / 2.0)))) * Math.sin((eps / 2.0));
}
def code(x, eps): return (-2.0 * math.sin((x + (eps / 2.0)))) * math.sin((eps / 2.0))
function code(x, eps) return Float64(Float64(-2.0 * sin(Float64(x + Float64(eps / 2.0)))) * sin(Float64(eps / 2.0))) end
function tmp = code(x, eps) tmp = (-2.0 * sin((x + (eps / 2.0)))) * sin((eps / 2.0)); end
code[x_, eps_] := N[(N[(-2.0 * N[Sin[N[(x + N[(eps / 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sin[N[(eps / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(-2 \cdot \sin \left(x + \frac{\varepsilon}{2}\right)\right) \cdot \sin \left(\frac{\varepsilon}{2}\right)
\end{array}
(FPCore (x eps) :precision binary64 (pow (cbrt (* (* -2.0 (sin (* 0.5 (fma 2.0 x eps)))) (sin (* 0.5 eps)))) 3.0))
double code(double x, double eps) {
return pow(cbrt(((-2.0 * sin((0.5 * fma(2.0, x, eps)))) * sin((0.5 * eps)))), 3.0);
}
function code(x, eps) return cbrt(Float64(Float64(-2.0 * sin(Float64(0.5 * fma(2.0, x, eps)))) * sin(Float64(0.5 * eps)))) ^ 3.0 end
code[x_, eps_] := N[Power[N[Power[N[(N[(-2.0 * N[Sin[N[(0.5 * N[(2.0 * x + eps), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sin[N[(0.5 * eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision], 3.0], $MachinePrecision]
\begin{array}{l}
\\
{\left(\sqrt[3]{\left(-2 \cdot \sin \left(0.5 \cdot \mathsf{fma}\left(2, x, \varepsilon\right)\right)\right) \cdot \sin \left(0.5 \cdot \varepsilon\right)}\right)}^{3}
\end{array}
herbie shell --seed 2024240
(FPCore (x eps)
:name "2cos (problem 3.3.5)"
:precision binary64
:pre (and (and (and (<= -10000.0 x) (<= x 10000.0)) (< (* 1e-16 (fabs x)) eps)) (< eps (fabs x)))
:alt
(! :herbie-platform default (* -2 (sin (+ x (/ eps 2))) (sin (/ eps 2))))
:alt
(! :herbie-platform default (pow (cbrt (* -2 (sin (* 1/2 (fma 2 x eps))) (sin (* 1/2 eps)))) 3))
(- (cos (+ x eps)) (cos x)))