
(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 7 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) (/ (tan (* x 0.5)) x)) x))
double code(double x) {
return (sin(x) * (tan((x * 0.5)) / x)) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (sin(x) * (tan((x * 0.5d0)) / x)) / x
end function
public static double code(double x) {
return (Math.sin(x) * (Math.tan((x * 0.5)) / x)) / x;
}
def code(x): return (math.sin(x) * (math.tan((x * 0.5)) / x)) / x
function code(x) return Float64(Float64(sin(x) * Float64(tan(Float64(x * 0.5)) / x)) / x) end
function tmp = code(x) tmp = (sin(x) * (tan((x * 0.5)) / x)) / x; end
code[x_] := N[(N[(N[Sin[x], $MachinePrecision] * N[(N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sin x \cdot \frac{\tan \left(x \cdot 0.5\right)}{x}}{x}
\end{array}
Initial program 54.2%
associate-/r*55.2%
div-inv55.2%
Applied egg-rr55.2%
clear-num55.2%
associate-/r/55.1%
Applied egg-rr55.1%
flip--54.8%
metadata-eval54.8%
unpow254.8%
unpow254.8%
1-sub-cos76.3%
frac-times76.4%
*-un-lft-identity76.4%
pow276.4%
Applied egg-rr76.4%
associate-/l/76.4%
unpow276.4%
associate-*r/76.4%
hang-0p-tan76.8%
Simplified76.8%
un-div-inv76.9%
associate-/l*99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
(FPCore (x)
:precision binary64
(if (<= x 0.0295)
(+
0.5
(* (pow x 2.0) (- (* 0.001388888888888889 (* x x)) 0.041666666666666664)))
(/ (/ 1.0 x) (/ x (- 1.0 (cos x))))))
double code(double x) {
double tmp;
if (x <= 0.0295) {
tmp = 0.5 + (pow(x, 2.0) * ((0.001388888888888889 * (x * x)) - 0.041666666666666664));
} else {
tmp = (1.0 / x) / (x / (1.0 - cos(x)));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0295d0) then
tmp = 0.5d0 + ((x ** 2.0d0) * ((0.001388888888888889d0 * (x * x)) - 0.041666666666666664d0))
else
tmp = (1.0d0 / x) / (x / (1.0d0 - cos(x)))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.0295) {
tmp = 0.5 + (Math.pow(x, 2.0) * ((0.001388888888888889 * (x * x)) - 0.041666666666666664));
} else {
tmp = (1.0 / x) / (x / (1.0 - Math.cos(x)));
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0295: tmp = 0.5 + (math.pow(x, 2.0) * ((0.001388888888888889 * (x * x)) - 0.041666666666666664)) else: tmp = (1.0 / x) / (x / (1.0 - math.cos(x))) return tmp
function code(x) tmp = 0.0 if (x <= 0.0295) tmp = Float64(0.5 + Float64((x ^ 2.0) * Float64(Float64(0.001388888888888889 * Float64(x * x)) - 0.041666666666666664))); else tmp = Float64(Float64(1.0 / x) / Float64(x / Float64(1.0 - cos(x)))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0295) tmp = 0.5 + ((x ^ 2.0) * ((0.001388888888888889 * (x * x)) - 0.041666666666666664)); else tmp = (1.0 / x) / (x / (1.0 - cos(x))); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0295], N[(0.5 + N[(N[Power[x, 2.0], $MachinePrecision] * N[(N[(0.001388888888888889 * N[(x * x), $MachinePrecision]), $MachinePrecision] - 0.041666666666666664), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / x), $MachinePrecision] / N[(x / N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0295:\\
\;\;\;\;0.5 + {x}^{2} \cdot \left(0.001388888888888889 \cdot \left(x \cdot x\right) - 0.041666666666666664\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x}}{\frac{x}{1 - \cos x}}\\
\end{array}
\end{array}
if x < 0.029499999999999998Initial program 37.6%
Taylor expanded in x around 0 65.0%
unpow265.0%
Applied egg-rr65.0%
if 0.029499999999999998 < x Initial program 98.3%
clear-num98.2%
inv-pow98.2%
flip--97.7%
associate-/r/97.7%
unpow-prod-down97.6%
pow297.6%
metadata-eval97.6%
pow297.6%
inv-pow97.6%
Applied egg-rr97.6%
unpow-197.6%
Simplified97.6%
frac-times97.7%
associate-/r/97.7%
metadata-eval97.7%
unpow297.7%
flip--98.2%
unpow298.2%
associate-/l*98.2%
frac-times99.2%
un-div-inv99.4%
Applied egg-rr99.4%
(FPCore (x) :precision binary64 (if (<= x 0.0054) (+ 0.5 (* (pow x 2.0) -0.041666666666666664)) (/ (/ 1.0 x) (/ x (- 1.0 (cos x))))))
double code(double x) {
double tmp;
if (x <= 0.0054) {
tmp = 0.5 + (pow(x, 2.0) * -0.041666666666666664);
} else {
tmp = (1.0 / x) / (x / (1.0 - cos(x)));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0054d0) then
tmp = 0.5d0 + ((x ** 2.0d0) * (-0.041666666666666664d0))
else
tmp = (1.0d0 / x) / (x / (1.0d0 - cos(x)))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.0054) {
tmp = 0.5 + (Math.pow(x, 2.0) * -0.041666666666666664);
} else {
tmp = (1.0 / x) / (x / (1.0 - Math.cos(x)));
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0054: tmp = 0.5 + (math.pow(x, 2.0) * -0.041666666666666664) else: tmp = (1.0 / x) / (x / (1.0 - math.cos(x))) return tmp
function code(x) tmp = 0.0 if (x <= 0.0054) tmp = Float64(0.5 + Float64((x ^ 2.0) * -0.041666666666666664)); else tmp = Float64(Float64(1.0 / x) / Float64(x / Float64(1.0 - cos(x)))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0054) tmp = 0.5 + ((x ^ 2.0) * -0.041666666666666664); else tmp = (1.0 / x) / (x / (1.0 - cos(x))); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0054], N[(0.5 + N[(N[Power[x, 2.0], $MachinePrecision] * -0.041666666666666664), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / x), $MachinePrecision] / N[(x / N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0054:\\
\;\;\;\;0.5 + {x}^{2} \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x}}{\frac{x}{1 - \cos x}}\\
\end{array}
\end{array}
if x < 0.0054000000000000003Initial program 37.6%
Taylor expanded in x around 0 64.4%
if 0.0054000000000000003 < x Initial program 98.3%
clear-num98.2%
inv-pow98.2%
flip--97.7%
associate-/r/97.7%
unpow-prod-down97.6%
pow297.6%
metadata-eval97.6%
pow297.6%
inv-pow97.6%
Applied egg-rr97.6%
unpow-197.6%
Simplified97.6%
frac-times97.7%
associate-/r/97.7%
metadata-eval97.7%
unpow297.7%
flip--98.2%
unpow298.2%
associate-/l*98.2%
frac-times99.2%
un-div-inv99.4%
Applied egg-rr99.4%
Final simplification74.0%
(FPCore (x) :precision binary64 (if (<= x 0.0054) (+ 0.5 (* (pow x 2.0) -0.041666666666666664)) (/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
double tmp;
if (x <= 0.0054) {
tmp = 0.5 + (pow(x, 2.0) * -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.0054d0) then
tmp = 0.5d0 + ((x ** 2.0d0) * (-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.0054) {
tmp = 0.5 + (Math.pow(x, 2.0) * -0.041666666666666664);
} else {
tmp = ((1.0 - Math.cos(x)) / x) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0054: tmp = 0.5 + (math.pow(x, 2.0) * -0.041666666666666664) else: tmp = ((1.0 - math.cos(x)) / x) / x return tmp
function code(x) tmp = 0.0 if (x <= 0.0054) tmp = Float64(0.5 + Float64((x ^ 2.0) * -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.0054) tmp = 0.5 + ((x ^ 2.0) * -0.041666666666666664); else tmp = ((1.0 - cos(x)) / x) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0054], N[(0.5 + N[(N[Power[x, 2.0], $MachinePrecision] * -0.041666666666666664), $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.0054:\\
\;\;\;\;0.5 + {x}^{2} \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\
\end{array}
\end{array}
if x < 0.0054000000000000003Initial program 37.6%
Taylor expanded in x around 0 64.4%
if 0.0054000000000000003 < x Initial program 98.3%
clear-num98.2%
inv-pow98.2%
flip--97.7%
associate-/r/97.7%
unpow-prod-down97.6%
pow297.6%
metadata-eval97.6%
pow297.6%
inv-pow97.6%
Applied egg-rr97.6%
unpow-197.6%
Simplified97.6%
frac-times97.7%
associate-/r/97.7%
metadata-eval97.7%
unpow297.7%
flip--98.2%
metadata-eval98.2%
clear-num98.3%
unpow298.3%
associate-/r*99.3%
Applied egg-rr99.3%
Final simplification74.0%
(FPCore (x) :precision binary64 (if (<= x 0.0054) (+ 0.5 (* (pow x 2.0) -0.041666666666666664)) (/ (- 1.0 (cos x)) (* x x))))
double code(double x) {
double tmp;
if (x <= 0.0054) {
tmp = 0.5 + (pow(x, 2.0) * -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.0054d0) then
tmp = 0.5d0 + ((x ** 2.0d0) * (-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.0054) {
tmp = 0.5 + (Math.pow(x, 2.0) * -0.041666666666666664);
} else {
tmp = (1.0 - Math.cos(x)) / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0054: tmp = 0.5 + (math.pow(x, 2.0) * -0.041666666666666664) else: tmp = (1.0 - math.cos(x)) / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 0.0054) tmp = Float64(0.5 + Float64((x ^ 2.0) * -0.041666666666666664)); 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.0054) tmp = 0.5 + ((x ^ 2.0) * -0.041666666666666664); else tmp = (1.0 - cos(x)) / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0054], N[(0.5 + N[(N[Power[x, 2.0], $MachinePrecision] * -0.041666666666666664), $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.0054:\\
\;\;\;\;0.5 + {x}^{2} \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x \cdot x}\\
\end{array}
\end{array}
if x < 0.0054000000000000003Initial program 37.6%
Taylor expanded in x around 0 64.4%
if 0.0054000000000000003 < x Initial program 98.3%
Final simplification73.7%
(FPCore (x) :precision binary64 (if (<= x 9.8e+76) 0.5 0.0))
double code(double x) {
double tmp;
if (x <= 9.8e+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 <= 9.8d+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 <= 9.8e+76) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 9.8e+76: tmp = 0.5 else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 9.8e+76) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 9.8e+76) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 9.8e+76], 0.5, 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 9.8 \cdot 10^{+76}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 9.80000000000000053e76Initial program 43.8%
Taylor expanded in x around 0 59.2%
if 9.80000000000000053e76 < x Initial program 98.1%
Taylor expanded in x around 0 66.0%
Taylor expanded in x around 0 66.0%
(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 54.2%
Taylor expanded in x around 0 24.7%
Taylor expanded in x around 0 25.5%
herbie shell --seed 2024180
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))