
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (* x x)))
double code(double x) {
return (1.0 - cos(x)) / (x * x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 - cos(x)) / (x * x)
end function
public static double code(double x) {
return (1.0 - Math.cos(x)) / (x * x);
}
def code(x): return (1.0 - math.cos(x)) / (x * x)
function code(x) return Float64(Float64(1.0 - cos(x)) / Float64(x * x)) end
function tmp = code(x) tmp = (1.0 - cos(x)) / (x * x); end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1 - \cos x}{x \cdot x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (* x x)))
double code(double x) {
return (1.0 - cos(x)) / (x * x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 - cos(x)) / (x * x)
end function
public static double code(double x) {
return (1.0 - Math.cos(x)) / (x * x);
}
def code(x): return (1.0 - math.cos(x)) / (x * x)
function code(x) return Float64(Float64(1.0 - cos(x)) / Float64(x * x)) end
function tmp = code(x) tmp = (1.0 - cos(x)) / (x * x); end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1 - \cos x}{x \cdot x}
\end{array}
(FPCore (x) :precision binary64 (* (/ (sin x) x) (/ (tan (* x 0.5)) x)))
double code(double x) {
return (sin(x) / x) * (tan((x * 0.5)) / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (sin(x) / x) * (tan((x * 0.5d0)) / x)
end function
public static double code(double x) {
return (Math.sin(x) / x) * (Math.tan((x * 0.5)) / x);
}
def code(x): return (math.sin(x) / x) * (math.tan((x * 0.5)) / x)
function code(x) return Float64(Float64(sin(x) / x) * Float64(tan(Float64(x * 0.5)) / x)) end
function tmp = code(x) tmp = (sin(x) / x) * (tan((x * 0.5)) / x); end
code[x_] := N[(N[(N[Sin[x], $MachinePrecision] / x), $MachinePrecision] * N[(N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sin x}{x} \cdot \frac{\tan \left(x \cdot 0.5\right)}{x}
\end{array}
Initial program 49.8%
flip--49.8%
div-inv49.8%
metadata-eval49.8%
1-sub-cos75.0%
pow275.0%
Applied egg-rr75.0%
associate-*r/75.0%
*-rgt-identity75.0%
Simplified75.0%
Taylor expanded in x around -inf 75.0%
+-commutative75.0%
unpow275.0%
associate-*r/75.0%
hang-0p-tan75.1%
Simplified75.1%
times-frac99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (x)
:precision binary64
(if (<= x 0.0275)
(+
0.5
(+ (* (pow x 4.0) 0.001388888888888889) (* x (* x -0.041666666666666664))))
(* (pow x -2.0) (- 1.0 (cos x)))))
double code(double x) {
double tmp;
if (x <= 0.0275) {
tmp = 0.5 + ((pow(x, 4.0) * 0.001388888888888889) + (x * (x * -0.041666666666666664)));
} else {
tmp = pow(x, -2.0) * (1.0 - cos(x));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0275d0) then
tmp = 0.5d0 + (((x ** 4.0d0) * 0.001388888888888889d0) + (x * (x * (-0.041666666666666664d0))))
else
tmp = (x ** (-2.0d0)) * (1.0d0 - cos(x))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.0275) {
tmp = 0.5 + ((Math.pow(x, 4.0) * 0.001388888888888889) + (x * (x * -0.041666666666666664)));
} else {
tmp = Math.pow(x, -2.0) * (1.0 - Math.cos(x));
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0275: tmp = 0.5 + ((math.pow(x, 4.0) * 0.001388888888888889) + (x * (x * -0.041666666666666664))) else: tmp = math.pow(x, -2.0) * (1.0 - math.cos(x)) return tmp
function code(x) tmp = 0.0 if (x <= 0.0275) tmp = Float64(0.5 + Float64(Float64((x ^ 4.0) * 0.001388888888888889) + Float64(x * Float64(x * -0.041666666666666664)))); else tmp = Float64((x ^ -2.0) * Float64(1.0 - cos(x))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0275) tmp = 0.5 + (((x ^ 4.0) * 0.001388888888888889) + (x * (x * -0.041666666666666664))); else tmp = (x ^ -2.0) * (1.0 - cos(x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0275], N[(0.5 + N[(N[(N[Power[x, 4.0], $MachinePrecision] * 0.001388888888888889), $MachinePrecision] + N[(x * N[(x * -0.041666666666666664), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Power[x, -2.0], $MachinePrecision] * N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0275:\\
\;\;\;\;0.5 + \left({x}^{4} \cdot 0.001388888888888889 + x \cdot \left(x \cdot -0.041666666666666664\right)\right)\\
\mathbf{else}:\\
\;\;\;\;{x}^{-2} \cdot \left(1 - \cos x\right)\\
\end{array}
\end{array}
if x < 0.0275000000000000001Initial program 35.2%
Taylor expanded in x around 0 67.5%
+-commutative67.5%
*-commutative67.5%
fma-def67.5%
*-commutative67.5%
unpow267.5%
Simplified67.5%
fma-udef67.5%
associate-*l*67.5%
Applied egg-rr67.5%
if 0.0275000000000000001 < x Initial program 98.6%
clear-num98.6%
associate-/r/98.5%
pow298.5%
pow-flip99.5%
metadata-eval99.5%
Applied egg-rr99.5%
Final simplification74.9%
(FPCore (x)
:precision binary64
(if (<= x 0.0275)
(+
0.5
(+ (* (pow x 4.0) 0.001388888888888889) (* x (* x -0.041666666666666664))))
(/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
double tmp;
if (x <= 0.0275) {
tmp = 0.5 + ((pow(x, 4.0) * 0.001388888888888889) + (x * (x * -0.041666666666666664)));
} else {
tmp = ((1.0 - cos(x)) / x) / x;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0275d0) then
tmp = 0.5d0 + (((x ** 4.0d0) * 0.001388888888888889d0) + (x * (x * (-0.041666666666666664d0))))
else
tmp = ((1.0d0 - cos(x)) / x) / x
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.0275) {
tmp = 0.5 + ((Math.pow(x, 4.0) * 0.001388888888888889) + (x * (x * -0.041666666666666664)));
} else {
tmp = ((1.0 - Math.cos(x)) / x) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0275: tmp = 0.5 + ((math.pow(x, 4.0) * 0.001388888888888889) + (x * (x * -0.041666666666666664))) else: tmp = ((1.0 - math.cos(x)) / x) / x return tmp
function code(x) tmp = 0.0 if (x <= 0.0275) tmp = Float64(0.5 + Float64(Float64((x ^ 4.0) * 0.001388888888888889) + Float64(x * Float64(x * -0.041666666666666664)))); else tmp = Float64(Float64(Float64(1.0 - cos(x)) / x) / x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0275) tmp = 0.5 + (((x ^ 4.0) * 0.001388888888888889) + (x * (x * -0.041666666666666664))); else tmp = ((1.0 - cos(x)) / x) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0275], N[(0.5 + N[(N[(N[Power[x, 4.0], $MachinePrecision] * 0.001388888888888889), $MachinePrecision] + N[(x * N[(x * -0.041666666666666664), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0275:\\
\;\;\;\;0.5 + \left({x}^{4} \cdot 0.001388888888888889 + x \cdot \left(x \cdot -0.041666666666666664\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\
\end{array}
\end{array}
if x < 0.0275000000000000001Initial program 35.2%
Taylor expanded in x around 0 67.5%
+-commutative67.5%
*-commutative67.5%
fma-def67.5%
*-commutative67.5%
unpow267.5%
Simplified67.5%
fma-udef67.5%
associate-*l*67.5%
Applied egg-rr67.5%
if 0.0275000000000000001 < x Initial program 98.6%
associate-/r*99.5%
div-inv99.5%
Applied egg-rr99.5%
un-div-inv99.5%
Applied egg-rr99.5%
Final simplification74.9%
(FPCore (x) :precision binary64 (if (<= x 0.0058) (+ 0.5 (* -0.041666666666666664 (* x x))) (/ (- 1.0 (cos x)) (* x x))))
double code(double x) {
double tmp;
if (x <= 0.0058) {
tmp = 0.5 + (-0.041666666666666664 * (x * x));
} else {
tmp = (1.0 - cos(x)) / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0058d0) then
tmp = 0.5d0 + ((-0.041666666666666664d0) * (x * x))
else
tmp = (1.0d0 - cos(x)) / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.0058) {
tmp = 0.5 + (-0.041666666666666664 * (x * x));
} else {
tmp = (1.0 - Math.cos(x)) / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0058: tmp = 0.5 + (-0.041666666666666664 * (x * x)) else: tmp = (1.0 - math.cos(x)) / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 0.0058) tmp = Float64(0.5 + Float64(-0.041666666666666664 * Float64(x * x))); else tmp = Float64(Float64(1.0 - cos(x)) / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0058) tmp = 0.5 + (-0.041666666666666664 * (x * x)); else tmp = (1.0 - cos(x)) / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0058], N[(0.5 + N[(-0.041666666666666664 * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0058:\\
\;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x \cdot x}\\
\end{array}
\end{array}
if x < 0.0058Initial program 34.7%
Taylor expanded in x around 0 67.2%
*-commutative67.2%
unpow267.2%
Simplified67.2%
if 0.0058 < x Initial program 98.1%
Final simplification74.5%
(FPCore (x) :precision binary64 (if (<= x 0.0058) (+ 0.5 (* -0.041666666666666664 (* x x))) (/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
double tmp;
if (x <= 0.0058) {
tmp = 0.5 + (-0.041666666666666664 * (x * x));
} else {
tmp = ((1.0 - cos(x)) / x) / x;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0058d0) then
tmp = 0.5d0 + ((-0.041666666666666664d0) * (x * x))
else
tmp = ((1.0d0 - cos(x)) / x) / x
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.0058) {
tmp = 0.5 + (-0.041666666666666664 * (x * x));
} else {
tmp = ((1.0 - Math.cos(x)) / x) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0058: tmp = 0.5 + (-0.041666666666666664 * (x * x)) else: tmp = ((1.0 - math.cos(x)) / x) / x return tmp
function code(x) tmp = 0.0 if (x <= 0.0058) tmp = Float64(0.5 + Float64(-0.041666666666666664 * Float64(x * x))); else tmp = Float64(Float64(Float64(1.0 - cos(x)) / x) / x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0058) tmp = 0.5 + (-0.041666666666666664 * (x * x)); else tmp = ((1.0 - cos(x)) / x) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0058], N[(0.5 + N[(-0.041666666666666664 * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0058:\\
\;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\
\end{array}
\end{array}
if x < 0.0058Initial program 34.7%
Taylor expanded in x around 0 67.2%
*-commutative67.2%
unpow267.2%
Simplified67.2%
if 0.0058 < x Initial program 98.1%
associate-/r*98.9%
div-inv98.8%
Applied egg-rr98.8%
un-div-inv98.9%
Applied egg-rr98.9%
Final simplification74.7%
(FPCore (x) :precision binary64 (if (<= x 2.0) (+ 0.5 (* -0.041666666666666664 (* x x))) (/ (/ -1.0 (- -0.16666666666666666 (/ 2.0 (* x x)))) (* x x))))
double code(double x) {
double tmp;
if (x <= 2.0) {
tmp = 0.5 + (-0.041666666666666664 * (x * x));
} else {
tmp = (-1.0 / (-0.16666666666666666 - (2.0 / (x * x)))) / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 2.0d0) then
tmp = 0.5d0 + ((-0.041666666666666664d0) * (x * x))
else
tmp = ((-1.0d0) / ((-0.16666666666666666d0) - (2.0d0 / (x * x)))) / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 2.0) {
tmp = 0.5 + (-0.041666666666666664 * (x * x));
} else {
tmp = (-1.0 / (-0.16666666666666666 - (2.0 / (x * x)))) / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.0: tmp = 0.5 + (-0.041666666666666664 * (x * x)) else: tmp = (-1.0 / (-0.16666666666666666 - (2.0 / (x * x)))) / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 2.0) tmp = Float64(0.5 + Float64(-0.041666666666666664 * Float64(x * x))); else tmp = Float64(Float64(-1.0 / Float64(-0.16666666666666666 - Float64(2.0 / Float64(x * x)))) / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2.0) tmp = 0.5 + (-0.041666666666666664 * (x * x)); else tmp = (-1.0 / (-0.16666666666666666 - (2.0 / (x * x)))) / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2.0], N[(0.5 + N[(-0.041666666666666664 * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(-1.0 / N[(-0.16666666666666666 - N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2:\\
\;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{-1}{-0.16666666666666666 - \frac{2}{x \cdot x}}}{x \cdot x}\\
\end{array}
\end{array}
if x < 2Initial program 35.2%
Taylor expanded in x around 0 67.2%
*-commutative67.2%
unpow267.2%
Simplified67.2%
if 2 < x Initial program 98.6%
add-log-exp98.6%
Applied egg-rr98.6%
add-log-exp98.6%
add-sqr-sqrt98.5%
sqrt-unprod98.6%
sqr-neg98.6%
sqrt-unprod0.0%
add-sqr-sqrt50.2%
flip--50.2%
metadata-eval50.2%
1-sub-cos50.2%
unpow250.2%
clear-num50.2%
distribute-neg-frac50.2%
metadata-eval50.2%
clear-num50.2%
unpow250.2%
1-sub-cos50.2%
metadata-eval50.2%
flip--50.2%
add-sqr-sqrt50.2%
sqrt-unprod50.2%
Applied egg-rr98.6%
Taylor expanded in x around 0 56.7%
distribute-neg-in56.7%
metadata-eval56.7%
associate-*r/56.7%
metadata-eval56.7%
unpow256.7%
Simplified56.7%
Final simplification64.8%
(FPCore (x) :precision binary64 (if (<= x 3.4) (+ 0.5 (* -0.041666666666666664 (* x x))) 0.0))
double code(double x) {
double tmp;
if (x <= 3.4) {
tmp = 0.5 + (-0.041666666666666664 * (x * x));
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 3.4d0) then
tmp = 0.5d0 + ((-0.041666666666666664d0) * (x * x))
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 3.4) {
tmp = 0.5 + (-0.041666666666666664 * (x * x));
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 3.4: tmp = 0.5 + (-0.041666666666666664 * (x * x)) else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 3.4) tmp = Float64(0.5 + Float64(-0.041666666666666664 * Float64(x * x))); else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 3.4) tmp = 0.5 + (-0.041666666666666664 * (x * x)); else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 3.4], N[(0.5 + N[(-0.041666666666666664 * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.4:\\
\;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 3.39999999999999991Initial program 35.2%
Taylor expanded in x around 0 67.2%
*-commutative67.2%
unpow267.2%
Simplified67.2%
if 3.39999999999999991 < x Initial program 98.6%
Taylor expanded in x around 0 51.0%
Taylor expanded in x around 0 51.0%
Final simplification63.5%
(FPCore (x) :precision binary64 (if (<= x 3e+76) 0.5 0.0))
double code(double x) {
double tmp;
if (x <= 3e+76) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 3d+76) then
tmp = 0.5d0
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 3e+76) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 3e+76: tmp = 0.5 else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 3e+76) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 3e+76) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 3e+76], 0.5, 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3 \cdot 10^{+76}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 2.9999999999999998e76Initial program 39.7%
Taylor expanded in x around 0 63.2%
if 2.9999999999999998e76 < x Initial program 98.7%
Taylor expanded in x around 0 67.1%
Taylor expanded in x around 0 67.1%
Final simplification63.9%
(FPCore (x) :precision binary64 0.0)
double code(double x) {
return 0.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.0d0
end function
public static double code(double x) {
return 0.0;
}
def code(x): return 0.0
function code(x) return 0.0 end
function tmp = code(x) tmp = 0.0; end
code[x_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 49.8%
Taylor expanded in x around 0 24.7%
Taylor expanded in x around 0 25.5%
Final simplification25.5%
herbie shell --seed 2023242
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))