
(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 8 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) / Float64(x / 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] / N[(x / N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\sin x}{\frac{x}{\tan \left(x \cdot 0.5\right)}}}{x}
\end{array}
Initial program 51.8%
flip--51.6%
div-inv51.6%
metadata-eval51.6%
1-sub-cos76.8%
pow276.8%
Applied egg-rr76.8%
associate-*r/76.8%
*-rgt-identity76.8%
Simplified76.8%
Taylor expanded in x around inf 77.4%
+-commutative77.4%
unpow277.4%
associate-/r*76.8%
+-commutative76.8%
unpow276.8%
associate-*r/76.8%
associate-*l/77.4%
hang-0p-tan77.6%
Simplified77.6%
associate-*l/77.0%
associate-/r*78.0%
associate-/l*99.9%
div-inv99.9%
metadata-eval99.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (<= x 0.0052) (+ 0.5 (* (* x x) -0.041666666666666664)) (/ (- 1.0 (cos x)) (* x x))))
double code(double x) {
double tmp;
if (x <= 0.0052) {
tmp = 0.5 + ((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.0052d0) then
tmp = 0.5d0 + ((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.0052) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = (1.0 - Math.cos(x)) / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0052: tmp = 0.5 + ((x * x) * -0.041666666666666664) else: tmp = (1.0 - math.cos(x)) / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 0.0052) tmp = Float64(0.5 + Float64(Float64(x * x) * -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.0052) tmp = 0.5 + ((x * x) * -0.041666666666666664); else tmp = (1.0 - cos(x)) / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0052], N[(0.5 + N[(N[(x * x), $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.0052:\\
\;\;\;\;0.5 + \left(x \cdot x\right) \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x \cdot x}\\
\end{array}
\end{array}
if x < 0.0051999999999999998Initial program 33.8%
Taylor expanded in x around 0 67.9%
*-commutative67.9%
unpow267.9%
Simplified67.9%
if 0.0051999999999999998 < x Initial program 98.7%
Final simplification76.5%
(FPCore (x) :precision binary64 (if (<= x 0.0052) (+ 0.5 (* (* x x) -0.041666666666666664)) (/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
double tmp;
if (x <= 0.0052) {
tmp = 0.5 + ((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.0052d0) then
tmp = 0.5d0 + ((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.0052) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = ((1.0 - Math.cos(x)) / x) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0052: tmp = 0.5 + ((x * x) * -0.041666666666666664) else: tmp = ((1.0 - math.cos(x)) / x) / x return tmp
function code(x) tmp = 0.0 if (x <= 0.0052) tmp = Float64(0.5 + Float64(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.0052) tmp = 0.5 + ((x * x) * -0.041666666666666664); else tmp = ((1.0 - cos(x)) / x) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0052], N[(0.5 + N[(N[(x * x), $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.0052:\\
\;\;\;\;0.5 + \left(x \cdot x\right) \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\
\end{array}
\end{array}
if x < 0.0051999999999999998Initial program 33.8%
Taylor expanded in x around 0 67.9%
*-commutative67.9%
unpow267.9%
Simplified67.9%
if 0.0051999999999999998 < x Initial program 98.7%
add-sqr-sqrt98.5%
pow298.5%
sqrt-div98.4%
sqrt-prod99.1%
add-sqr-sqrt99.1%
Applied egg-rr99.1%
unpow299.1%
frac-times98.5%
add-sqr-sqrt98.7%
associate-/r*99.4%
Applied egg-rr99.4%
Final simplification76.7%
(FPCore (x) :precision binary64 (if (<= x 2.05) (+ 0.5 (* (* x x) -0.041666666666666664)) (/ 1.0 (* (* x x) (+ 0.16666666666666666 (/ 2.0 (* x x)))))))
double code(double x) {
double tmp;
if (x <= 2.05) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = 1.0 / ((x * x) * (0.16666666666666666 + (2.0 / (x * x))));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 2.05d0) then
tmp = 0.5d0 + ((x * x) * (-0.041666666666666664d0))
else
tmp = 1.0d0 / ((x * x) * (0.16666666666666666d0 + (2.0d0 / (x * x))))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 2.05) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = 1.0 / ((x * x) * (0.16666666666666666 + (2.0 / (x * x))));
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.05: tmp = 0.5 + ((x * x) * -0.041666666666666664) else: tmp = 1.0 / ((x * x) * (0.16666666666666666 + (2.0 / (x * x)))) return tmp
function code(x) tmp = 0.0 if (x <= 2.05) tmp = Float64(0.5 + Float64(Float64(x * x) * -0.041666666666666664)); else tmp = Float64(1.0 / Float64(Float64(x * x) * Float64(0.16666666666666666 + Float64(2.0 / Float64(x * x))))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2.05) tmp = 0.5 + ((x * x) * -0.041666666666666664); else tmp = 1.0 / ((x * x) * (0.16666666666666666 + (2.0 / (x * x)))); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2.05], N[(0.5 + N[(N[(x * x), $MachinePrecision] * -0.041666666666666664), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[(x * x), $MachinePrecision] * N[(0.16666666666666666 + N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.05:\\
\;\;\;\;0.5 + \left(x \cdot x\right) \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\left(x \cdot x\right) \cdot \left(0.16666666666666666 + \frac{2}{x \cdot x}\right)}\\
\end{array}
\end{array}
if x < 2.0499999999999998Initial program 34.1%
Taylor expanded in x around 0 67.8%
*-commutative67.8%
unpow267.8%
Simplified67.8%
if 2.0499999999999998 < x Initial program 98.7%
add-sqr-sqrt98.5%
pow298.5%
sqrt-div98.5%
sqrt-prod99.1%
add-sqr-sqrt99.2%
Applied egg-rr99.2%
Applied egg-rr98.6%
Taylor expanded in x around 0 64.7%
unpow264.7%
associate-*r/64.7%
metadata-eval64.7%
Simplified64.7%
Final simplification66.9%
(FPCore (x) :precision binary64 (if (<= x 3.25) (+ 0.5 (* (* x x) -0.041666666666666664)) (/ 6.0 (* x x))))
double code(double x) {
double tmp;
if (x <= 3.25) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = 6.0 / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 3.25d0) then
tmp = 0.5d0 + ((x * x) * (-0.041666666666666664d0))
else
tmp = 6.0d0 / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 3.25) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = 6.0 / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 3.25: tmp = 0.5 + ((x * x) * -0.041666666666666664) else: tmp = 6.0 / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 3.25) tmp = Float64(0.5 + Float64(Float64(x * x) * -0.041666666666666664)); else tmp = Float64(6.0 / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 3.25) tmp = 0.5 + ((x * x) * -0.041666666666666664); else tmp = 6.0 / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 3.25], N[(0.5 + N[(N[(x * x), $MachinePrecision] * -0.041666666666666664), $MachinePrecision]), $MachinePrecision], N[(6.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.25:\\
\;\;\;\;0.5 + \left(x \cdot x\right) \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{6}{x \cdot x}\\
\end{array}
\end{array}
if x < 3.25Initial program 34.1%
Taylor expanded in x around 0 67.8%
*-commutative67.8%
unpow267.8%
Simplified67.8%
if 3.25 < x Initial program 98.7%
add-sqr-sqrt98.5%
pow298.5%
sqrt-div98.5%
sqrt-prod99.1%
add-sqr-sqrt99.2%
Applied egg-rr99.2%
Applied egg-rr98.6%
Taylor expanded in x around 0 64.7%
unpow264.7%
associate-*r/64.7%
metadata-eval64.7%
Simplified64.7%
Taylor expanded in x around inf 64.7%
unpow264.7%
Simplified64.7%
Final simplification66.9%
(FPCore (x) :precision binary64 (if (<= x 3.5) 0.5 (/ 6.0 (* x x))))
double code(double x) {
double tmp;
if (x <= 3.5) {
tmp = 0.5;
} else {
tmp = 6.0 / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 3.5d0) then
tmp = 0.5d0
else
tmp = 6.0d0 / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 3.5) {
tmp = 0.5;
} else {
tmp = 6.0 / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 3.5: tmp = 0.5 else: tmp = 6.0 / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 3.5) tmp = 0.5; else tmp = Float64(6.0 / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 3.5) tmp = 0.5; else tmp = 6.0 / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 3.5], 0.5, N[(6.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.5:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{6}{x \cdot x}\\
\end{array}
\end{array}
if x < 3.5Initial program 34.1%
Taylor expanded in x around 0 68.3%
if 3.5 < x Initial program 98.7%
add-sqr-sqrt98.5%
pow298.5%
sqrt-div98.5%
sqrt-prod99.1%
add-sqr-sqrt99.2%
Applied egg-rr99.2%
Applied egg-rr98.6%
Taylor expanded in x around 0 64.7%
unpow264.7%
associate-*r/64.7%
metadata-eval64.7%
Simplified64.7%
Taylor expanded in x around inf 64.7%
unpow264.7%
Simplified64.7%
Final simplification67.3%
(FPCore (x) :precision binary64 (if (<= x 9.5e+76) 0.5 0.0))
double code(double x) {
double tmp;
if (x <= 9.5e+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.5d+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.5e+76) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 9.5e+76: tmp = 0.5 else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 9.5e+76) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 9.5e+76) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 9.5e+76], 0.5, 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 9.5 \cdot 10^{+76}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 9.5000000000000003e76Initial program 38.7%
Taylor expanded in x around 0 64.0%
if 9.5000000000000003e76 < x Initial program 98.6%
Taylor expanded in x around 0 74.3%
Taylor expanded in x around 0 74.3%
Final simplification66.3%
(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 51.8%
Taylor expanded in x around 0 28.3%
Taylor expanded in x around 0 29.0%
Final simplification29.0%
herbie shell --seed 2023238
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))