
(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 6 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 2.0)) x)))
double code(double x) {
return (sin(x) / x) * (tan((x / 2.0)) / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (sin(x) / x) * (tan((x / 2.0d0)) / x)
end function
public static double code(double x) {
return (Math.sin(x) / x) * (Math.tan((x / 2.0)) / x);
}
def code(x): return (math.sin(x) / x) * (math.tan((x / 2.0)) / x)
function code(x) return Float64(Float64(sin(x) / x) * Float64(tan(Float64(x / 2.0)) / x)) end
function tmp = code(x) tmp = (sin(x) / x) * (tan((x / 2.0)) / x); end
code[x_] := N[(N[(N[Sin[x], $MachinePrecision] / x), $MachinePrecision] * N[(N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sin x}{x} \cdot \frac{\tan \left(\frac{x}{2}\right)}{x}
\end{array}
Initial program 49.7%
flip--49.6%
div-inv49.6%
metadata-eval49.6%
pow249.6%
Applied egg-rr49.6%
associate-*r/49.6%
*-rgt-identity49.6%
Simplified49.6%
unpow249.6%
1-sub-cos73.9%
Applied egg-rr73.9%
associate-/l*73.9%
times-frac99.6%
hang-0p-tan99.8%
Applied egg-rr99.8%
(FPCore (x)
:precision binary64
(if (<= x 0.035)
(+
0.5
(* (pow x 2.0) (- (* 0.001388888888888889 (* x x)) 0.041666666666666664)))
(/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
double tmp;
if (x <= 0.035) {
tmp = 0.5 + (pow(x, 2.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.035d0) then
tmp = 0.5d0 + ((x ** 2.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.035) {
tmp = 0.5 + (Math.pow(x, 2.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.035: tmp = 0.5 + (math.pow(x, 2.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.035) tmp = Float64(0.5 + Float64((x ^ 2.0) * Float64(Float64(0.001388888888888889 * Float64(x * 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.035) tmp = 0.5 + ((x ^ 2.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.035], 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[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.035:\\
\;\;\;\;0.5 + {x}^{2} \cdot \left(0.001388888888888889 \cdot \left(x \cdot x\right) - 0.041666666666666664\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\
\end{array}
\end{array}
if x < 0.035000000000000003Initial program 35.2%
Taylor expanded in x around 0 66.6%
pow266.6%
Applied egg-rr66.6%
if 0.035000000000000003 < x Initial program 96.1%
associate-/r*99.3%
clear-num96.2%
inv-pow96.2%
Applied egg-rr96.2%
unpow-196.2%
clear-num99.3%
Applied egg-rr99.3%
(FPCore (x) :precision binary64 (if (<= x 0.000175) 0.5 (/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
double tmp;
if (x <= 0.000175) {
tmp = 0.5;
} 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.000175d0) then
tmp = 0.5d0
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.000175) {
tmp = 0.5;
} else {
tmp = ((1.0 - Math.cos(x)) / x) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.000175: tmp = 0.5 else: tmp = ((1.0 - math.cos(x)) / x) / x return tmp
function code(x) tmp = 0.0 if (x <= 0.000175) tmp = 0.5; 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.000175) tmp = 0.5; else tmp = ((1.0 - cos(x)) / x) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.000175], 0.5, 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.000175:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\
\end{array}
\end{array}
if x < 1.74999999999999998e-4Initial program 34.9%
Taylor expanded in x around 0 67.0%
if 1.74999999999999998e-4 < x Initial program 95.9%
associate-/r*99.1%
clear-num96.0%
inv-pow96.0%
Applied egg-rr96.0%
unpow-196.0%
clear-num99.1%
Applied egg-rr99.1%
(FPCore (x) :precision binary64 (if (<= x 0.000175) 0.5 (/ (- 1.0 (cos x)) (* x x))))
double code(double x) {
double tmp;
if (x <= 0.000175) {
tmp = 0.5;
} 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.000175d0) then
tmp = 0.5d0
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.000175) {
tmp = 0.5;
} else {
tmp = (1.0 - Math.cos(x)) / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.000175: tmp = 0.5 else: tmp = (1.0 - math.cos(x)) / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 0.000175) tmp = 0.5; 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.000175) tmp = 0.5; else tmp = (1.0 - cos(x)) / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.000175], 0.5, 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.000175:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x \cdot x}\\
\end{array}
\end{array}
if x < 1.74999999999999998e-4Initial program 34.9%
Taylor expanded in x around 0 67.0%
if 1.74999999999999998e-4 < x Initial program 95.9%
(FPCore (x) :precision binary64 (if (<= x 1.15e+77) 0.5 0.0))
double code(double x) {
double tmp;
if (x <= 1.15e+77) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.15d+77) then
tmp = 0.5d0
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.15e+77) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.15e+77: tmp = 0.5 else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 1.15e+77) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.15e+77) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.15e+77], 0.5, 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.15 \cdot 10^{+77}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 1.14999999999999997e77Initial program 40.8%
Taylor expanded in x around 0 61.6%
if 1.14999999999999997e77 < x Initial program 95.1%
Taylor expanded in x around 0 60.4%
Taylor expanded in x around 0 60.4%
(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.7%
Taylor expanded in x around 0 26.9%
Taylor expanded in x around 0 27.7%
herbie shell --seed 2024123
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))