
(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 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%
clear-num51.7%
inv-pow51.7%
associate-/l*52.5%
Applied egg-rr52.5%
flip--52.4%
div-inv52.4%
metadata-eval52.4%
1-sub-cos74.1%
pow274.1%
Applied egg-rr74.1%
unpow274.1%
associate-*l*74.1%
associate-*r/74.1%
*-rgt-identity74.1%
hang-0p-tan74.3%
Simplified74.3%
unpow-174.3%
clear-num74.6%
associate-/l*99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (x) :precision binary64 (if (<= x 5e-156) 0.5 (/ (* (sin x) (tan (/ x 2.0))) (* x x))))
double code(double x) {
double tmp;
if (x <= 5e-156) {
tmp = 0.5;
} else {
tmp = (sin(x) * tan((x / 2.0))) / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 5d-156) then
tmp = 0.5d0
else
tmp = (sin(x) * tan((x / 2.0d0))) / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 5e-156) {
tmp = 0.5;
} else {
tmp = (Math.sin(x) * Math.tan((x / 2.0))) / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 5e-156: tmp = 0.5 else: tmp = (math.sin(x) * math.tan((x / 2.0))) / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 5e-156) tmp = 0.5; else tmp = Float64(Float64(sin(x) * tan(Float64(x / 2.0))) / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 5e-156) tmp = 0.5; else tmp = (sin(x) * tan((x / 2.0))) / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 5e-156], 0.5, N[(N[(N[Sin[x], $MachinePrecision] * N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5 \cdot 10^{-156}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{\sin x \cdot \tan \left(\frac{x}{2}\right)}{x \cdot x}\\
\end{array}
\end{array}
if x < 5.00000000000000007e-156Initial program 38.8%
Taylor expanded in x around 0 63.4%
if 5.00000000000000007e-156 < x Initial program 72.7%
flip--72.4%
div-inv72.4%
metadata-eval72.4%
1-sub-cos99.0%
pow299.0%
Applied egg-rr99.0%
unpow299.0%
associate-*l*99.0%
associate-*r/99.0%
*-rgt-identity99.0%
hang-0p-tan99.3%
Simplified99.4%
Final simplification77.2%
(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 34.1%
Taylor expanded in x around 0 67.9%
unpow267.9%
Simplified67.9%
if 0.0051999999999999998 < x Initial program 98.7%
Final simplification76.3%
(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 34.1%
Taylor expanded in x around 0 67.9%
unpow267.9%
Simplified67.9%
if 0.0051999999999999998 < x Initial program 98.7%
frac-2neg98.7%
div-inv98.7%
distribute-rgt-neg-in98.7%
Applied egg-rr98.7%
Taylor expanded in x around 0 98.7%
unpow298.7%
associate-/r*99.0%
Simplified99.0%
div-inv98.8%
frac-2neg98.8%
metadata-eval98.8%
inv-pow98.8%
inv-pow98.8%
unpow-prod-down98.7%
*-commutative98.7%
unpow-prod-down98.8%
inv-pow98.8%
inv-pow98.8%
metadata-eval98.8%
frac-2neg98.8%
Applied egg-rr98.8%
associate-*r*98.9%
clear-num98.9%
un-div-inv99.1%
un-div-inv99.1%
add-sqr-sqrt0.0%
sqrt-unprod43.5%
sqr-neg43.5%
sqrt-unprod43.5%
add-sqr-sqrt43.5%
clear-num43.5%
add-sqr-sqrt0.0%
sqrt-unprod99.1%
frac-times98.7%
metadata-eval98.7%
metadata-eval98.7%
frac-times99.1%
sqrt-unprod99.1%
add-sqr-sqrt99.1%
clear-num99.1%
/-rgt-identity99.1%
Applied egg-rr99.1%
Final simplification76.4%
(FPCore (x) :precision binary64 (/ -1.0 (* x (- (* x -0.16666666666666666) (/ 2.0 x)))))
double code(double x) {
return -1.0 / (x * ((x * -0.16666666666666666) - (2.0 / x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-1.0d0) / (x * ((x * (-0.16666666666666666d0)) - (2.0d0 / x)))
end function
public static double code(double x) {
return -1.0 / (x * ((x * -0.16666666666666666) - (2.0 / x)));
}
def code(x): return -1.0 / (x * ((x * -0.16666666666666666) - (2.0 / x)))
function code(x) return Float64(-1.0 / Float64(x * Float64(Float64(x * -0.16666666666666666) - Float64(2.0 / x)))) end
function tmp = code(x) tmp = -1.0 / (x * ((x * -0.16666666666666666) - (2.0 / x))); end
code[x_] := N[(-1.0 / N[(x * N[(N[(x * -0.16666666666666666), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{x \cdot \left(x \cdot -0.16666666666666666 - \frac{2}{x}\right)}
\end{array}
Initial program 51.8%
frac-2neg51.8%
div-inv51.8%
distribute-rgt-neg-in51.8%
Applied egg-rr51.8%
associate-/r*51.9%
associate-*r/52.8%
distribute-lft-neg-in52.8%
div-inv52.8%
frac-2neg52.8%
clear-num52.5%
frac-2neg52.5%
metadata-eval52.5%
frac-2neg52.5%
add-sqr-sqrt24.2%
sqrt-unprod36.2%
sqr-neg36.2%
sqrt-prod12.7%
add-sqr-sqrt23.9%
distribute-frac-neg23.9%
frac-2neg23.9%
Applied egg-rr52.5%
associate-/r/52.5%
Applied egg-rr52.5%
Taylor expanded in x around 0 74.9%
*-commutative74.9%
associate-*r/74.9%
metadata-eval74.9%
Simplified74.9%
Final simplification74.9%
(FPCore (x) :precision binary64 0.5)
double code(double x) {
return 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0
end function
public static double code(double x) {
return 0.5;
}
def code(x): return 0.5
function code(x) return 0.5 end
function tmp = code(x) tmp = 0.5; end
code[x_] := 0.5
\begin{array}{l}
\\
0.5
\end{array}
Initial program 51.8%
Taylor expanded in x around 0 50.8%
Final simplification50.8%
herbie shell --seed 2023280
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))