
(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) (/ (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.6%
flip--52.5%
associate-/r*52.5%
div-inv52.4%
metadata-eval52.4%
1-sub-cos74.6%
pow274.6%
*-commutative74.6%
associate-*l*74.7%
+-commutative74.7%
distribute-lft-in74.6%
*-commutative74.6%
fma-def74.7%
*-lft-identity74.7%
Applied egg-rr74.7%
associate-*r/75.5%
times-frac76.3%
fma-udef76.3%
*-rgt-identity76.3%
distribute-lft-in76.3%
+-commutative76.3%
associate-/l/76.3%
associate-*l/76.3%
associate-*r/76.3%
associate-*r/76.3%
*-rgt-identity76.3%
unpow276.3%
associate-*l/76.3%
hang-0p-tan76.5%
associate-/r*75.1%
associate-*r/75.7%
Simplified75.7%
associate-*l/75.1%
times-frac99.8%
associate-*l/99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (x)
:precision binary64
(if (<= x 0.029)
(/
(- -1.0)
(+
(* (* x x) (+ 0.16666666666666666 (* x (* x 0.008333333333333333))))
2.0))
(* (/ (/ 1.0 x) x) (- 1.0 (cos x)))))
double code(double x) {
double tmp;
if (x <= 0.029) {
tmp = -(-1.0) / (((x * x) * (0.16666666666666666 + (x * (x * 0.008333333333333333)))) + 2.0);
} 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.029d0) then
tmp = -(-1.0d0) / (((x * x) * (0.16666666666666666d0 + (x * (x * 0.008333333333333333d0)))) + 2.0d0)
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.029) {
tmp = -(-1.0) / (((x * x) * (0.16666666666666666 + (x * (x * 0.008333333333333333)))) + 2.0);
} else {
tmp = ((1.0 / x) / x) * (1.0 - Math.cos(x));
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.029: tmp = -(-1.0) / (((x * x) * (0.16666666666666666 + (x * (x * 0.008333333333333333)))) + 2.0) else: tmp = ((1.0 / x) / x) * (1.0 - math.cos(x)) return tmp
function code(x) tmp = 0.0 if (x <= 0.029) tmp = Float64(Float64(-(-1.0)) / Float64(Float64(Float64(x * x) * Float64(0.16666666666666666 + Float64(x * Float64(x * 0.008333333333333333)))) + 2.0)); else tmp = Float64(Float64(Float64(1.0 / x) / x) * Float64(1.0 - cos(x))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.029) tmp = -(-1.0) / (((x * x) * (0.16666666666666666 + (x * (x * 0.008333333333333333)))) + 2.0); else tmp = ((1.0 / x) / x) * (1.0 - cos(x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.029], N[((--1.0) / N[(N[(N[(x * x), $MachinePrecision] * N[(0.16666666666666666 + N[(x * N[(x * 0.008333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + 2.0), $MachinePrecision]), $MachinePrecision], N[(N[(N[(1.0 / x), $MachinePrecision] / x), $MachinePrecision] * N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.029:\\
\;\;\;\;\frac{--1}{\left(x \cdot x\right) \cdot \left(0.16666666666666666 + x \cdot \left(x \cdot 0.008333333333333333\right)\right) + 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x}}{x} \cdot \left(1 - \cos x\right)\\
\end{array}
\end{array}
if x < 0.0290000000000000015Initial program 36.6%
remove-double-div36.6%
Applied egg-rr36.6%
Taylor expanded in x around 0 50.2%
fma-def50.2%
unpow250.2%
unpow250.2%
associate-*r/50.2%
metadata-eval50.2%
Simplified50.2%
frac-2neg50.2%
distribute-frac-neg50.2%
remove-double-neg50.2%
neg-mul-150.2%
metadata-eval50.2%
associate-/l*50.2%
metadata-eval50.2%
frac-2neg50.2%
div-inv50.3%
remove-double-div50.3%
fma-udef50.3%
associate-+r+50.3%
distribute-lft-in33.0%
Applied egg-rr82.9%
if 0.0290000000000000015 < x Initial program 98.5%
flip--98.2%
div-inv98.1%
times-frac99.1%
metadata-eval99.1%
1-sub-cos99.0%
pow299.0%
Applied egg-rr99.0%
frac-times97.9%
div-inv97.9%
unpow297.9%
1-sub-cos98.2%
metadata-eval98.2%
flip--98.5%
remove-double-div98.5%
clear-num98.5%
associate-*r/98.5%
remove-double-div98.6%
Applied egg-rr98.6%
associate-/r*99.4%
associate-/r/99.5%
Simplified99.5%
Final simplification87.2%
(FPCore (x)
:precision binary64
(if (<= x 0.029)
(/
(- -1.0)
(+
(* (* x x) (+ 0.16666666666666666 (* x (* x 0.008333333333333333))))
2.0))
(/ (- 1.0 (cos x)) (* x x))))
double code(double x) {
double tmp;
if (x <= 0.029) {
tmp = -(-1.0) / (((x * x) * (0.16666666666666666 + (x * (x * 0.008333333333333333)))) + 2.0);
} 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.029d0) then
tmp = -(-1.0d0) / (((x * x) * (0.16666666666666666d0 + (x * (x * 0.008333333333333333d0)))) + 2.0d0)
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.029) {
tmp = -(-1.0) / (((x * x) * (0.16666666666666666 + (x * (x * 0.008333333333333333)))) + 2.0);
} else {
tmp = (1.0 - Math.cos(x)) / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.029: tmp = -(-1.0) / (((x * x) * (0.16666666666666666 + (x * (x * 0.008333333333333333)))) + 2.0) else: tmp = (1.0 - math.cos(x)) / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 0.029) tmp = Float64(Float64(-(-1.0)) / Float64(Float64(Float64(x * x) * Float64(0.16666666666666666 + Float64(x * Float64(x * 0.008333333333333333)))) + 2.0)); 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.029) tmp = -(-1.0) / (((x * x) * (0.16666666666666666 + (x * (x * 0.008333333333333333)))) + 2.0); else tmp = (1.0 - cos(x)) / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.029], N[((--1.0) / N[(N[(N[(x * x), $MachinePrecision] * N[(0.16666666666666666 + N[(x * N[(x * 0.008333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + 2.0), $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.029:\\
\;\;\;\;\frac{--1}{\left(x \cdot x\right) \cdot \left(0.16666666666666666 + x \cdot \left(x \cdot 0.008333333333333333\right)\right) + 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x \cdot x}\\
\end{array}
\end{array}
if x < 0.0290000000000000015Initial program 36.6%
remove-double-div36.6%
Applied egg-rr36.6%
Taylor expanded in x around 0 50.2%
fma-def50.2%
unpow250.2%
unpow250.2%
associate-*r/50.2%
metadata-eval50.2%
Simplified50.2%
frac-2neg50.2%
distribute-frac-neg50.2%
remove-double-neg50.2%
neg-mul-150.2%
metadata-eval50.2%
associate-/l*50.2%
metadata-eval50.2%
frac-2neg50.2%
div-inv50.3%
remove-double-div50.3%
fma-udef50.3%
associate-+r+50.3%
distribute-lft-in33.0%
Applied egg-rr82.9%
if 0.0290000000000000015 < x Initial program 98.5%
Final simplification86.9%
(FPCore (x)
:precision binary64
(if (<= x 0.029)
(/
(- -1.0)
(+
(* (* x x) (+ 0.16666666666666666 (* x (* x 0.008333333333333333))))
2.0))
(/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
double tmp;
if (x <= 0.029) {
tmp = -(-1.0) / (((x * x) * (0.16666666666666666 + (x * (x * 0.008333333333333333)))) + 2.0);
} 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.029d0) then
tmp = -(-1.0d0) / (((x * x) * (0.16666666666666666d0 + (x * (x * 0.008333333333333333d0)))) + 2.0d0)
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.029) {
tmp = -(-1.0) / (((x * x) * (0.16666666666666666 + (x * (x * 0.008333333333333333)))) + 2.0);
} else {
tmp = ((1.0 - Math.cos(x)) / x) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.029: tmp = -(-1.0) / (((x * x) * (0.16666666666666666 + (x * (x * 0.008333333333333333)))) + 2.0) else: tmp = ((1.0 - math.cos(x)) / x) / x return tmp
function code(x) tmp = 0.0 if (x <= 0.029) tmp = Float64(Float64(-(-1.0)) / Float64(Float64(Float64(x * x) * Float64(0.16666666666666666 + Float64(x * Float64(x * 0.008333333333333333)))) + 2.0)); 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.029) tmp = -(-1.0) / (((x * x) * (0.16666666666666666 + (x * (x * 0.008333333333333333)))) + 2.0); else tmp = ((1.0 - cos(x)) / x) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.029], N[((--1.0) / N[(N[(N[(x * x), $MachinePrecision] * N[(0.16666666666666666 + N[(x * N[(x * 0.008333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + 2.0), $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.029:\\
\;\;\;\;\frac{--1}{\left(x \cdot x\right) \cdot \left(0.16666666666666666 + x \cdot \left(x \cdot 0.008333333333333333\right)\right) + 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\
\end{array}
\end{array}
if x < 0.0290000000000000015Initial program 36.6%
remove-double-div36.6%
Applied egg-rr36.6%
Taylor expanded in x around 0 50.2%
fma-def50.2%
unpow250.2%
unpow250.2%
associate-*r/50.2%
metadata-eval50.2%
Simplified50.2%
frac-2neg50.2%
distribute-frac-neg50.2%
remove-double-neg50.2%
neg-mul-150.2%
metadata-eval50.2%
associate-/l*50.2%
metadata-eval50.2%
frac-2neg50.2%
div-inv50.3%
remove-double-div50.3%
fma-udef50.3%
associate-+r+50.3%
distribute-lft-in33.0%
Applied egg-rr82.9%
if 0.0290000000000000015 < x Initial program 98.5%
flip--98.2%
div-inv98.1%
times-frac99.1%
metadata-eval99.1%
1-sub-cos99.0%
pow299.0%
Applied egg-rr99.0%
associate-*r/99.0%
associate-*l/99.0%
div-inv99.1%
unpow299.1%
1-sub-cos99.3%
metadata-eval99.3%
flip--99.5%
Applied egg-rr99.5%
Final simplification87.2%
(FPCore (x) :precision binary64 (/ (- -1.0) (+ 2.0 (* x (* x 0.16666666666666666)))))
double code(double x) {
return -(-1.0) / (2.0 + (x * (x * 0.16666666666666666)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = -(-1.0d0) / (2.0d0 + (x * (x * 0.16666666666666666d0)))
end function
public static double code(double x) {
return -(-1.0) / (2.0 + (x * (x * 0.16666666666666666)));
}
def code(x): return -(-1.0) / (2.0 + (x * (x * 0.16666666666666666)))
function code(x) return Float64(Float64(-(-1.0)) / Float64(2.0 + Float64(x * Float64(x * 0.16666666666666666)))) end
function tmp = code(x) tmp = -(-1.0) / (2.0 + (x * (x * 0.16666666666666666))); end
code[x_] := N[((--1.0) / N[(2.0 + N[(x * N[(x * 0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{--1}{2 + x \cdot \left(x \cdot 0.16666666666666666\right)}
\end{array}
Initial program 52.6%
remove-double-div52.5%
Applied egg-rr52.5%
Taylor expanded in x around 0 52.0%
fma-def52.0%
unpow252.0%
unpow252.0%
associate-*r/52.0%
metadata-eval52.0%
Simplified52.0%
frac-2neg52.0%
distribute-frac-neg52.0%
remove-double-neg52.0%
neg-mul-152.0%
metadata-eval52.0%
associate-/l*51.9%
metadata-eval51.9%
frac-2neg51.9%
div-inv52.0%
remove-double-div52.0%
fma-udef52.0%
associate-+r+52.0%
distribute-lft-in25.4%
Applied egg-rr76.2%
Taylor expanded in x around 0 78.3%
*-commutative78.3%
unpow278.3%
associate-*r*78.3%
Simplified78.3%
Final simplification78.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.6%
Taylor expanded in x around 0 49.6%
Final simplification49.6%
herbie shell --seed 2023297
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))