
(FPCore (x c s) :precision binary64 (/ (cos (* 2.0 x)) (* (pow c 2.0) (* (* x (pow s 2.0)) x))))
double code(double x, double c, double s) {
return cos((2.0 * x)) / (pow(c, 2.0) * ((x * pow(s, 2.0)) * x));
}
real(8) function code(x, c, s)
real(8), intent (in) :: x
real(8), intent (in) :: c
real(8), intent (in) :: s
code = cos((2.0d0 * x)) / ((c ** 2.0d0) * ((x * (s ** 2.0d0)) * x))
end function
public static double code(double x, double c, double s) {
return Math.cos((2.0 * x)) / (Math.pow(c, 2.0) * ((x * Math.pow(s, 2.0)) * x));
}
def code(x, c, s): return math.cos((2.0 * x)) / (math.pow(c, 2.0) * ((x * math.pow(s, 2.0)) * x))
function code(x, c, s) return Float64(cos(Float64(2.0 * x)) / Float64((c ^ 2.0) * Float64(Float64(x * (s ^ 2.0)) * x))) end
function tmp = code(x, c, s) tmp = cos((2.0 * x)) / ((c ^ 2.0) * ((x * (s ^ 2.0)) * x)); end
code[x_, c_, s_] := N[(N[Cos[N[(2.0 * x), $MachinePrecision]], $MachinePrecision] / N[(N[Power[c, 2.0], $MachinePrecision] * N[(N[(x * N[Power[s, 2.0], $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\cos \left(2 \cdot x\right)}{{c}^{2} \cdot \left(\left(x \cdot {s}^{2}\right) \cdot x\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 15 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x c s) :precision binary64 (/ (cos (* 2.0 x)) (* (pow c 2.0) (* (* x (pow s 2.0)) x))))
double code(double x, double c, double s) {
return cos((2.0 * x)) / (pow(c, 2.0) * ((x * pow(s, 2.0)) * x));
}
real(8) function code(x, c, s)
real(8), intent (in) :: x
real(8), intent (in) :: c
real(8), intent (in) :: s
code = cos((2.0d0 * x)) / ((c ** 2.0d0) * ((x * (s ** 2.0d0)) * x))
end function
public static double code(double x, double c, double s) {
return Math.cos((2.0 * x)) / (Math.pow(c, 2.0) * ((x * Math.pow(s, 2.0)) * x));
}
def code(x, c, s): return math.cos((2.0 * x)) / (math.pow(c, 2.0) * ((x * math.pow(s, 2.0)) * x))
function code(x, c, s) return Float64(cos(Float64(2.0 * x)) / Float64((c ^ 2.0) * Float64(Float64(x * (s ^ 2.0)) * x))) end
function tmp = code(x, c, s) tmp = cos((2.0 * x)) / ((c ^ 2.0) * ((x * (s ^ 2.0)) * x)); end
code[x_, c_, s_] := N[(N[Cos[N[(2.0 * x), $MachinePrecision]], $MachinePrecision] / N[(N[Power[c, 2.0], $MachinePrecision] * N[(N[(x * N[Power[s, 2.0], $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\cos \left(2 \cdot x\right)}{{c}^{2} \cdot \left(\left(x \cdot {s}^{2}\right) \cdot x\right)}
\end{array}
s_m = (fabs.f64 s) c_m = (fabs.f64 c) NOTE: x, c_m, and s_m should be sorted in increasing order before calling this function. (FPCore (x c_m s_m) :precision binary64 (if (<= (pow c_m 2.0) 2e+25) (/ (cos (+ x x)) (pow (* x (* c_m s_m)) 2.0)) (/ 1.0 (* (* s_m (* x s_m)) (* c_m (* c_m x))))))
s_m = fabs(s);
c_m = fabs(c);
assert(x < c_m && c_m < s_m);
double code(double x, double c_m, double s_m) {
double tmp;
if (pow(c_m, 2.0) <= 2e+25) {
tmp = cos((x + x)) / pow((x * (c_m * s_m)), 2.0);
} else {
tmp = 1.0 / ((s_m * (x * s_m)) * (c_m * (c_m * x)));
}
return tmp;
}
s_m = abs(s)
c_m = abs(c)
NOTE: x, c_m, and s_m should be sorted in increasing order before calling this function.
real(8) function code(x, c_m, s_m)
real(8), intent (in) :: x
real(8), intent (in) :: c_m
real(8), intent (in) :: s_m
real(8) :: tmp
if ((c_m ** 2.0d0) <= 2d+25) then
tmp = cos((x + x)) / ((x * (c_m * s_m)) ** 2.0d0)
else
tmp = 1.0d0 / ((s_m * (x * s_m)) * (c_m * (c_m * x)))
end if
code = tmp
end function
s_m = Math.abs(s);
c_m = Math.abs(c);
assert x < c_m && c_m < s_m;
public static double code(double x, double c_m, double s_m) {
double tmp;
if (Math.pow(c_m, 2.0) <= 2e+25) {
tmp = Math.cos((x + x)) / Math.pow((x * (c_m * s_m)), 2.0);
} else {
tmp = 1.0 / ((s_m * (x * s_m)) * (c_m * (c_m * x)));
}
return tmp;
}
s_m = math.fabs(s) c_m = math.fabs(c) [x, c_m, s_m] = sort([x, c_m, s_m]) def code(x, c_m, s_m): tmp = 0 if math.pow(c_m, 2.0) <= 2e+25: tmp = math.cos((x + x)) / math.pow((x * (c_m * s_m)), 2.0) else: tmp = 1.0 / ((s_m * (x * s_m)) * (c_m * (c_m * x))) return tmp
s_m = abs(s) c_m = abs(c) x, c_m, s_m = sort([x, c_m, s_m]) function code(x, c_m, s_m) tmp = 0.0 if ((c_m ^ 2.0) <= 2e+25) tmp = Float64(cos(Float64(x + x)) / (Float64(x * Float64(c_m * s_m)) ^ 2.0)); else tmp = Float64(1.0 / Float64(Float64(s_m * Float64(x * s_m)) * Float64(c_m * Float64(c_m * x)))); end return tmp end
s_m = abs(s);
c_m = abs(c);
x, c_m, s_m = num2cell(sort([x, c_m, s_m])){:}
function tmp_2 = code(x, c_m, s_m)
tmp = 0.0;
if ((c_m ^ 2.0) <= 2e+25)
tmp = cos((x + x)) / ((x * (c_m * s_m)) ^ 2.0);
else
tmp = 1.0 / ((s_m * (x * s_m)) * (c_m * (c_m * x)));
end
tmp_2 = tmp;
end
s_m = N[Abs[s], $MachinePrecision] c_m = N[Abs[c], $MachinePrecision] NOTE: x, c_m, and s_m should be sorted in increasing order before calling this function. code[x_, c$95$m_, s$95$m_] := If[LessEqual[N[Power[c$95$m, 2.0], $MachinePrecision], 2e+25], N[(N[Cos[N[(x + x), $MachinePrecision]], $MachinePrecision] / N[Power[N[(x * N[(c$95$m * s$95$m), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[(s$95$m * N[(x * s$95$m), $MachinePrecision]), $MachinePrecision] * N[(c$95$m * N[(c$95$m * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
s_m = \left|s\right|
\\
c_m = \left|c\right|
\\
[x, c_m, s_m] = \mathsf{sort}([x, c_m, s_m])\\
\\
\begin{array}{l}
\mathbf{if}\;{c\_m}^{2} \leq 2 \cdot 10^{+25}:\\
\;\;\;\;\frac{\cos \left(x + x\right)}{{\left(x \cdot \left(c\_m \cdot s\_m\right)\right)}^{2}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\left(s\_m \cdot \left(x \cdot s\_m\right)\right) \cdot \left(c\_m \cdot \left(c\_m \cdot x\right)\right)}\\
\end{array}
\end{array}
if (pow.f64 c #s(literal 2 binary64)) < 2.00000000000000018e25Initial program 62.2%
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-*l*N/A
associate-*r*N/A
lift-pow.f64N/A
lift-pow.f64N/A
pow-prod-downN/A
pow2N/A
pow-prod-downN/A
lower-pow.f64N/A
lower-*.f64N/A
lower-*.f6498.7
Applied rewrites98.7%
lift-*.f64N/A
count-2N/A
lift-+.f6498.7
Applied rewrites98.7%
if 2.00000000000000018e25 < (pow.f64 c #s(literal 2 binary64)) Initial program 79.6%
Taylor expanded in x around 0
lower-/.f64N/A
associate-*r*N/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
unpow2N/A
associate-*l*N/A
lower-*.f64N/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f6474.2
Applied rewrites74.2%
Applied rewrites99.8%
Final simplification99.0%
s_m = (fabs.f64 s)
c_m = (fabs.f64 c)
NOTE: x, c_m, and s_m should be sorted in increasing order before calling this function.
(FPCore (x c_m s_m)
:precision binary64
(let* ((t_0 (* c_m (* x s_m))))
(if (<=
(/ (cos (* 2.0 x)) (* (pow c_m 2.0) (* x (* x (pow s_m 2.0)))))
-2e-180)
(/ (fma x (* x -2.0) 1.0) (* t_0 t_0))
(/ (/ 1.0 t_0) t_0))))s_m = fabs(s);
c_m = fabs(c);
assert(x < c_m && c_m < s_m);
double code(double x, double c_m, double s_m) {
double t_0 = c_m * (x * s_m);
double tmp;
if ((cos((2.0 * x)) / (pow(c_m, 2.0) * (x * (x * pow(s_m, 2.0))))) <= -2e-180) {
tmp = fma(x, (x * -2.0), 1.0) / (t_0 * t_0);
} else {
tmp = (1.0 / t_0) / t_0;
}
return tmp;
}
s_m = abs(s) c_m = abs(c) x, c_m, s_m = sort([x, c_m, s_m]) function code(x, c_m, s_m) t_0 = Float64(c_m * Float64(x * s_m)) tmp = 0.0 if (Float64(cos(Float64(2.0 * x)) / Float64((c_m ^ 2.0) * Float64(x * Float64(x * (s_m ^ 2.0))))) <= -2e-180) tmp = Float64(fma(x, Float64(x * -2.0), 1.0) / Float64(t_0 * t_0)); else tmp = Float64(Float64(1.0 / t_0) / t_0); end return tmp end
s_m = N[Abs[s], $MachinePrecision]
c_m = N[Abs[c], $MachinePrecision]
NOTE: x, c_m, and s_m should be sorted in increasing order before calling this function.
code[x_, c$95$m_, s$95$m_] := Block[{t$95$0 = N[(c$95$m * N[(x * s$95$m), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(N[Cos[N[(2.0 * x), $MachinePrecision]], $MachinePrecision] / N[(N[Power[c$95$m, 2.0], $MachinePrecision] * N[(x * N[(x * N[Power[s$95$m, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -2e-180], N[(N[(x * N[(x * -2.0), $MachinePrecision] + 1.0), $MachinePrecision] / N[(t$95$0 * t$95$0), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / t$95$0), $MachinePrecision] / t$95$0), $MachinePrecision]]]
\begin{array}{l}
s_m = \left|s\right|
\\
c_m = \left|c\right|
\\
[x, c_m, s_m] = \mathsf{sort}([x, c_m, s_m])\\
\\
\begin{array}{l}
t_0 := c\_m \cdot \left(x \cdot s\_m\right)\\
\mathbf{if}\;\frac{\cos \left(2 \cdot x\right)}{{c\_m}^{2} \cdot \left(x \cdot \left(x \cdot {s\_m}^{2}\right)\right)} \leq -2 \cdot 10^{-180}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, x \cdot -2, 1\right)}{t\_0 \cdot t\_0}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{t\_0}}{t\_0}\\
\end{array}
\end{array}
if (/.f64 (cos.f64 (*.f64 #s(literal 2 binary64) x)) (*.f64 (pow.f64 c #s(literal 2 binary64)) (*.f64 (*.f64 x (pow.f64 s #s(literal 2 binary64))) x))) < -2e-180Initial program 79.6%
lift-*.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-*r*N/A
lift-*.f64N/A
lift-pow.f64N/A
unpow2N/A
associate-*r*N/A
associate-*r*N/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-*.f64N/A
lift-pow.f64N/A
unpow2N/A
lower-*.f64N/A
lower-*.f6479.6
Applied rewrites79.6%
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
lift-*.f64N/A
associate-*l*N/A
associate-*r*N/A
*-commutativeN/A
lift-*.f64N/A
associate-*r*N/A
lift-*.f64N/A
lift-*.f64N/A
lower-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
associate-*r*N/A
lift-*.f64N/A
lift-*.f64N/A
lower-*.f6497.4
Applied rewrites97.4%
Taylor expanded in x around 0
+-commutativeN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6454.9
Applied rewrites54.9%
lift-*.f64N/A
lift-*.f64N/A
associate-*l*N/A
lift-*.f64N/A
associate-*r*N/A
*-commutativeN/A
lift-*.f64N/A
lift-*.f64N/A
lower-*.f6454.9
Applied rewrites54.9%
if -2e-180 < (/.f64 (cos.f64 (*.f64 #s(literal 2 binary64) x)) (*.f64 (pow.f64 c #s(literal 2 binary64)) (*.f64 (*.f64 x (pow.f64 s #s(literal 2 binary64))) x))) Initial program 66.1%
Taylor expanded in x around 0
lower-/.f64N/A
associate-*r*N/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
unpow2N/A
associate-*l*N/A
lower-*.f64N/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f6476.2
Applied rewrites76.2%
Applied rewrites85.2%
Final simplification83.4%
herbie shell --seed 2024223
(FPCore (x c s)
:name "mixedcos"
:precision binary64
(/ (cos (* 2.0 x)) (* (pow c 2.0) (* (* x (pow s 2.0)) x))))