
(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(Float64(sin(x) / 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[(N[Sin[x], $MachinePrecision] / x), $MachinePrecision] * N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\sin x}{x} \cdot \tan \left(x \cdot 0.5\right)}{x}
\end{array}
Initial program 52.7%
flip--52.5%
div-inv52.5%
metadata-eval52.5%
1-sub-cos75.7%
pow275.7%
Applied egg-rr75.7%
unpow275.7%
associate-*l*75.7%
associate-*r/75.7%
*-rgt-identity75.7%
hang-0p-tan76.0%
Simplified76.0%
associate-/r*76.7%
div-inv76.6%
div-inv76.6%
metadata-eval76.6%
Applied egg-rr76.6%
un-div-inv76.7%
associate-/l*99.8%
div-inv99.7%
*-commutative99.7%
clear-num99.7%
*-commutative99.7%
Applied egg-rr99.7%
expm1-log1p-u99.7%
expm1-udef26.9%
associate-*r/26.9%
Applied egg-rr26.9%
expm1-def76.7%
expm1-log1p76.7%
associate-*l/99.8%
*-commutative99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (x) :precision binary64 (if (<= (/ (- 1.0 (cos x)) (* x x)) 0.5) (* (sin x) (/ (tan (* x 0.5)) (* x x))) 0.5))
double code(double x) {
double tmp;
if (((1.0 - cos(x)) / (x * x)) <= 0.5) {
tmp = sin(x) * (tan((x * 0.5)) / (x * x));
} else {
tmp = 0.5;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (((1.0d0 - cos(x)) / (x * x)) <= 0.5d0) then
tmp = sin(x) * (tan((x * 0.5d0)) / (x * x))
else
tmp = 0.5d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (((1.0 - Math.cos(x)) / (x * x)) <= 0.5) {
tmp = Math.sin(x) * (Math.tan((x * 0.5)) / (x * x));
} else {
tmp = 0.5;
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 - math.cos(x)) / (x * x)) <= 0.5: tmp = math.sin(x) * (math.tan((x * 0.5)) / (x * x)) else: tmp = 0.5 return tmp
function code(x) tmp = 0.0 if (Float64(Float64(1.0 - cos(x)) / Float64(x * x)) <= 0.5) tmp = Float64(sin(x) * Float64(tan(Float64(x * 0.5)) / Float64(x * x))); else tmp = 0.5; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (((1.0 - cos(x)) / (x * x)) <= 0.5) tmp = sin(x) * (tan((x * 0.5)) / (x * x)); else tmp = 0.5; end tmp_2 = tmp; end
code[x_] := If[LessEqual[N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision], 0.5], N[(N[Sin[x], $MachinePrecision] * N[(N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.5]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1 - \cos x}{x \cdot x} \leq 0.5:\\
\;\;\;\;\sin x \cdot \frac{\tan \left(x \cdot 0.5\right)}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if (/.f64 (-.f64 1 (cos.f64 x)) (*.f64 x x)) < 0.5Initial program 68.4%
flip--68.3%
div-inv68.2%
metadata-eval68.2%
1-sub-cos98.4%
pow298.4%
Applied egg-rr98.4%
unpow298.4%
associate-*l*98.4%
associate-*r/98.4%
*-rgt-identity98.4%
hang-0p-tan98.8%
Simplified98.8%
expm1-log1p-u98.8%
expm1-udef62.7%
div-inv61.3%
pow261.3%
pow-flip61.3%
metadata-eval61.3%
associate-*l*61.3%
div-inv61.3%
metadata-eval61.3%
Applied egg-rr61.3%
expm1-def97.6%
expm1-log1p97.6%
associate-*r*97.5%
metadata-eval97.5%
pow-sqr97.3%
unpow-197.3%
unpow-197.3%
associate-*r*98.5%
associate-*r/98.6%
associate-*l/98.6%
*-rgt-identity98.6%
associate-*r/98.7%
*-rgt-identity98.7%
associate-*r/99.6%
associate-*r/99.6%
associate-/r*98.4%
*-commutative98.4%
Simplified98.4%
if 0.5 < (/.f64 (-.f64 1 (cos.f64 x)) (*.f64 x x)) Initial program 0.0%
Taylor expanded in x around 0 100.0%
Final simplification98.8%
(FPCore (x) :precision binary64 (if (or (<= x -0.000135) (not (<= x 0.000145))) (* (- 1.0 (cos x)) (pow x -2.0)) 0.5))
double code(double x) {
double tmp;
if ((x <= -0.000135) || !(x <= 0.000145)) {
tmp = (1.0 - cos(x)) * pow(x, -2.0);
} else {
tmp = 0.5;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((x <= (-0.000135d0)) .or. (.not. (x <= 0.000145d0))) then
tmp = (1.0d0 - cos(x)) * (x ** (-2.0d0))
else
tmp = 0.5d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x <= -0.000135) || !(x <= 0.000145)) {
tmp = (1.0 - Math.cos(x)) * Math.pow(x, -2.0);
} else {
tmp = 0.5;
}
return tmp;
}
def code(x): tmp = 0 if (x <= -0.000135) or not (x <= 0.000145): tmp = (1.0 - math.cos(x)) * math.pow(x, -2.0) else: tmp = 0.5 return tmp
function code(x) tmp = 0.0 if ((x <= -0.000135) || !(x <= 0.000145)) tmp = Float64(Float64(1.0 - cos(x)) * (x ^ -2.0)); else tmp = 0.5; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x <= -0.000135) || ~((x <= 0.000145))) tmp = (1.0 - cos(x)) * (x ^ -2.0); else tmp = 0.5; end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -0.000135], N[Not[LessEqual[x, 0.000145]], $MachinePrecision]], N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] * N[Power[x, -2.0], $MachinePrecision]), $MachinePrecision], 0.5]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.000135 \lor \neg \left(x \leq 0.000145\right):\\
\;\;\;\;\left(1 - \cos x\right) \cdot {x}^{-2}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.35000000000000002e-4 or 1.45e-4 < x Initial program 98.4%
clear-num98.4%
associate-/r/98.3%
pow298.3%
pow-flip98.9%
metadata-eval98.9%
Applied egg-rr98.9%
if -1.35000000000000002e-4 < x < 1.45e-4Initial program 1.6%
Taylor expanded in x around 0 100.0%
Final simplification99.4%
(FPCore (x) :precision binary64 (/ (sin x) (* x (/ x (tan (* x 0.5))))))
double code(double x) {
return sin(x) / (x * (x / tan((x * 0.5))));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sin(x) / (x * (x / tan((x * 0.5d0))))
end function
public static double code(double x) {
return Math.sin(x) / (x * (x / Math.tan((x * 0.5))));
}
def code(x): return math.sin(x) / (x * (x / math.tan((x * 0.5))))
function code(x) return Float64(sin(x) / Float64(x * Float64(x / tan(Float64(x * 0.5))))) end
function tmp = code(x) tmp = sin(x) / (x * (x / tan((x * 0.5)))); end
code[x_] := N[(N[Sin[x], $MachinePrecision] / N[(x * N[(x / N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sin x}{x \cdot \frac{x}{\tan \left(x \cdot 0.5\right)}}
\end{array}
Initial program 52.7%
flip--52.5%
div-inv52.5%
metadata-eval52.5%
1-sub-cos75.7%
pow275.7%
Applied egg-rr75.7%
unpow275.7%
associate-*l*75.7%
associate-*r/75.7%
*-rgt-identity75.7%
hang-0p-tan76.0%
Simplified76.0%
associate-/r*76.7%
div-inv76.6%
div-inv76.6%
metadata-eval76.6%
Applied egg-rr76.6%
*-commutative76.6%
associate-/l*99.6%
frac-times99.5%
*-un-lft-identity99.5%
Applied egg-rr99.5%
Final simplification99.5%
(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 52.7%
flip--52.5%
div-inv52.5%
metadata-eval52.5%
1-sub-cos75.7%
pow275.7%
Applied egg-rr75.7%
unpow275.7%
associate-*l*75.7%
associate-*r/75.7%
*-rgt-identity75.7%
hang-0p-tan76.0%
Simplified76.0%
associate-/r*76.7%
div-inv76.6%
div-inv76.6%
metadata-eval76.6%
Applied egg-rr76.6%
un-div-inv76.7%
associate-/l*99.8%
div-inv99.7%
*-commutative99.7%
clear-num99.7%
*-commutative99.7%
Applied egg-rr99.7%
Final simplification99.7%
(FPCore (x) :precision binary64 (if (or (<= x -0.000135) (not (<= x 0.000145))) (/ (- 1.0 (cos x)) (* x x)) 0.5))
double code(double x) {
double tmp;
if ((x <= -0.000135) || !(x <= 0.000145)) {
tmp = (1.0 - cos(x)) / (x * x);
} else {
tmp = 0.5;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((x <= (-0.000135d0)) .or. (.not. (x <= 0.000145d0))) then
tmp = (1.0d0 - cos(x)) / (x * x)
else
tmp = 0.5d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x <= -0.000135) || !(x <= 0.000145)) {
tmp = (1.0 - Math.cos(x)) / (x * x);
} else {
tmp = 0.5;
}
return tmp;
}
def code(x): tmp = 0 if (x <= -0.000135) or not (x <= 0.000145): tmp = (1.0 - math.cos(x)) / (x * x) else: tmp = 0.5 return tmp
function code(x) tmp = 0.0 if ((x <= -0.000135) || !(x <= 0.000145)) tmp = Float64(Float64(1.0 - cos(x)) / Float64(x * x)); else tmp = 0.5; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x <= -0.000135) || ~((x <= 0.000145))) tmp = (1.0 - cos(x)) / (x * x); else tmp = 0.5; end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -0.000135], N[Not[LessEqual[x, 0.000145]], $MachinePrecision]], N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision], 0.5]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.000135 \lor \neg \left(x \leq 0.000145\right):\\
\;\;\;\;\frac{1 - \cos x}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.35000000000000002e-4 or 1.45e-4 < x Initial program 98.4%
if -1.35000000000000002e-4 < x < 1.45e-4Initial program 1.6%
Taylor expanded in x around 0 100.0%
Final simplification99.2%
(FPCore (x) :precision binary64 (if (or (<= x -0.000135) (not (<= x 0.000145))) (/ (/ (- 1.0 (cos x)) x) x) 0.5))
double code(double x) {
double tmp;
if ((x <= -0.000135) || !(x <= 0.000145)) {
tmp = ((1.0 - cos(x)) / x) / x;
} else {
tmp = 0.5;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((x <= (-0.000135d0)) .or. (.not. (x <= 0.000145d0))) then
tmp = ((1.0d0 - cos(x)) / x) / x
else
tmp = 0.5d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x <= -0.000135) || !(x <= 0.000145)) {
tmp = ((1.0 - Math.cos(x)) / x) / x;
} else {
tmp = 0.5;
}
return tmp;
}
def code(x): tmp = 0 if (x <= -0.000135) or not (x <= 0.000145): tmp = ((1.0 - math.cos(x)) / x) / x else: tmp = 0.5 return tmp
function code(x) tmp = 0.0 if ((x <= -0.000135) || !(x <= 0.000145)) tmp = Float64(Float64(Float64(1.0 - cos(x)) / x) / x); else tmp = 0.5; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x <= -0.000135) || ~((x <= 0.000145))) tmp = ((1.0 - cos(x)) / x) / x; else tmp = 0.5; end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -0.000135], N[Not[LessEqual[x, 0.000145]], $MachinePrecision]], N[(N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision], 0.5]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.000135 \lor \neg \left(x \leq 0.000145\right):\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if x < -1.35000000000000002e-4 or 1.45e-4 < x Initial program 98.4%
clear-num98.4%
associate-/r/98.3%
pow298.3%
pow-flip98.9%
metadata-eval98.9%
Applied egg-rr98.9%
*-commutative98.9%
flip--98.6%
metadata-eval98.6%
1-sub-cos99.1%
unpow299.1%
un-div-inv99.0%
metadata-eval99.0%
pow-flip98.6%
pow298.6%
div-inv98.5%
associate-/r*98.9%
un-div-inv99.0%
unpow299.0%
1-sub-cos98.6%
metadata-eval98.6%
flip--98.9%
Applied egg-rr98.9%
if -1.35000000000000002e-4 < x < 1.45e-4Initial program 1.6%
Taylor expanded in x around 0 100.0%
Final simplification99.4%
(FPCore (x) :precision binary64 (/ (- -1.0) (* x (+ (* x 0.16666666666666666) (* 2.0 (/ 1.0 x))))))
double code(double x) {
return -(-1.0) / (x * ((x * 0.16666666666666666) + (2.0 * (1.0 / x))));
}
real(8) function code(x)
real(8), intent (in) :: x
code = -(-1.0d0) / (x * ((x * 0.16666666666666666d0) + (2.0d0 * (1.0d0 / x))))
end function
public static double code(double x) {
return -(-1.0) / (x * ((x * 0.16666666666666666) + (2.0 * (1.0 / x))));
}
def code(x): return -(-1.0) / (x * ((x * 0.16666666666666666) + (2.0 * (1.0 / x))))
function code(x) return Float64(Float64(-(-1.0)) / Float64(x * Float64(Float64(x * 0.16666666666666666) + Float64(2.0 * Float64(1.0 / x))))) end
function tmp = code(x) tmp = -(-1.0) / (x * ((x * 0.16666666666666666) + (2.0 * (1.0 / x)))); end
code[x_] := N[((--1.0) / N[(x * N[(N[(x * 0.16666666666666666), $MachinePrecision] + N[(2.0 * N[(1.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{--1}{x \cdot \left(x \cdot 0.16666666666666666 + 2 \cdot \frac{1}{x}\right)}
\end{array}
Initial program 52.7%
frac-2neg52.7%
div-inv52.6%
distribute-rgt-neg-in52.6%
Applied egg-rr52.6%
distribute-lft-neg-out52.6%
associate-/r*52.8%
Simplified52.8%
associate-*r/53.6%
add-sqr-sqrt25.6%
sqrt-unprod39.0%
sqr-neg39.0%
sqrt-prod14.1%
div-inv14.1%
add-sqr-sqrt24.8%
div-inv24.8%
clear-num24.8%
frac-2neg24.8%
metadata-eval24.8%
frac-times24.8%
metadata-eval24.8%
add-sqr-sqrt10.7%
sqrt-unprod37.7%
sqr-neg37.7%
sqrt-prod27.7%
add-sqr-sqrt53.3%
Applied egg-rr53.3%
Taylor expanded in x around 0 74.3%
Final simplification74.3%
(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 52.7%
Taylor expanded in x around 0 49.6%
Final simplification49.6%
herbie shell --seed 2023196
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))