
(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 10 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 (/ (/ (tan (* x 0.5)) (/ x (sin x))) x))
double code(double x) {
return (tan((x * 0.5)) / (x / sin(x))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (tan((x * 0.5d0)) / (x / sin(x))) / x
end function
public static double code(double x) {
return (Math.tan((x * 0.5)) / (x / Math.sin(x))) / x;
}
def code(x): return (math.tan((x * 0.5)) / (x / math.sin(x))) / x
function code(x) return Float64(Float64(tan(Float64(x * 0.5)) / Float64(x / sin(x))) / x) end
function tmp = code(x) tmp = (tan((x * 0.5)) / (x / sin(x))) / x; end
code[x_] := N[(N[(N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision] / N[(x / N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\tan \left(x \cdot 0.5\right)}{\frac{x}{\sin x}}}{x}
\end{array}
Initial program 48.5%
flip--48.3%
div-inv48.2%
metadata-eval48.2%
1-sub-cos75.3%
pow275.3%
Applied egg-rr75.3%
associate-*r/75.3%
*-rgt-identity75.3%
Simplified75.3%
Taylor expanded in x around inf 75.3%
unpow275.3%
unpow275.3%
times-frac75.7%
hang-0p-tan75.9%
Simplified75.9%
associate-/r*99.7%
associate-*l/99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
*-commutative99.8%
clear-num99.8%
un-div-inv99.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (/ (tan (* x 0.5)) (/ x (/ (sin x) x))))
double code(double x) {
return tan((x * 0.5)) / (x / (sin(x) / x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = tan((x * 0.5d0)) / (x / (sin(x) / x))
end function
public static double code(double x) {
return Math.tan((x * 0.5)) / (x / (Math.sin(x) / x));
}
def code(x): return math.tan((x * 0.5)) / (x / (math.sin(x) / x))
function code(x) return Float64(tan(Float64(x * 0.5)) / Float64(x / Float64(sin(x) / x))) end
function tmp = code(x) tmp = tan((x * 0.5)) / (x / (sin(x) / x)); end
code[x_] := N[(N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision] / N[(x / N[(N[Sin[x], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\tan \left(x \cdot 0.5\right)}{\frac{x}{\frac{\sin x}{x}}}
\end{array}
Initial program 48.5%
flip--48.3%
div-inv48.2%
metadata-eval48.2%
1-sub-cos75.3%
pow275.3%
Applied egg-rr75.3%
associate-*r/75.3%
*-rgt-identity75.3%
Simplified75.3%
Taylor expanded in x around inf 75.3%
unpow275.3%
unpow275.3%
times-frac75.7%
hang-0p-tan75.9%
Simplified75.9%
*-commutative75.9%
clear-num75.6%
un-div-inv75.7%
div-inv75.7%
metadata-eval75.7%
associate-/l*98.5%
Applied egg-rr98.5%
Final simplification98.5%
(FPCore (x) :precision binary64 (/ (* (tan (* x 0.5)) (/ (sin x) x)) x))
double code(double x) {
return (tan((x * 0.5)) * (sin(x) / x)) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (tan((x * 0.5d0)) * (sin(x) / x)) / x
end function
public static double code(double x) {
return (Math.tan((x * 0.5)) * (Math.sin(x) / x)) / x;
}
def code(x): return (math.tan((x * 0.5)) * (math.sin(x) / x)) / x
function code(x) return Float64(Float64(tan(Float64(x * 0.5)) * Float64(sin(x) / x)) / x) end
function tmp = code(x) tmp = (tan((x * 0.5)) * (sin(x) / x)) / x; end
code[x_] := N[(N[(N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision] * N[(N[Sin[x], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\tan \left(x \cdot 0.5\right) \cdot \frac{\sin x}{x}}{x}
\end{array}
Initial program 48.5%
flip--48.3%
div-inv48.2%
metadata-eval48.2%
1-sub-cos75.3%
pow275.3%
Applied egg-rr75.3%
associate-*r/75.3%
*-rgt-identity75.3%
Simplified75.3%
Taylor expanded in x around inf 75.3%
unpow275.3%
unpow275.3%
times-frac75.7%
hang-0p-tan75.9%
Simplified75.9%
associate-/r*99.7%
associate-*l/99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (x) :precision binary64 (if (<= x 0.0055) (+ 0.5 (* (* x x) -0.041666666666666664)) (/ (- 1.0 (cos x)) (* x x))))
double code(double x) {
double tmp;
if (x <= 0.0055) {
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.0055d0) 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.0055) {
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.0055: 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.0055) 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.0055) 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.0055], 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.0055:\\
\;\;\;\;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.0054999999999999997Initial program 34.0%
Taylor expanded in x around 0 67.4%
*-commutative67.4%
unpow267.4%
Simplified67.4%
if 0.0054999999999999997 < x Initial program 97.9%
Final simplification74.3%
(FPCore (x) :precision binary64 (if (<= x 0.0055) (+ 0.5 (* (* x x) -0.041666666666666664)) (/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
double tmp;
if (x <= 0.0055) {
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.0055d0) 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.0055) {
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.0055: 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.0055) 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.0055) 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.0055], 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.0055:\\
\;\;\;\;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.0054999999999999997Initial program 34.0%
Taylor expanded in x around 0 67.4%
*-commutative67.4%
unpow267.4%
Simplified67.4%
if 0.0054999999999999997 < x Initial program 97.9%
frac-2neg97.9%
div-inv97.8%
distribute-rgt-neg-in97.8%
Applied egg-rr97.8%
Taylor expanded in x around 0 97.8%
unpow297.8%
Simplified97.8%
*-commutative97.8%
associate-/r*99.1%
associate-*l/99.1%
neg-sub099.1%
metadata-eval99.1%
associate--r-99.1%
metadata-eval99.1%
metadata-eval99.1%
Applied egg-rr99.1%
Taylor expanded in x around inf 99.2%
associate-*r/99.2%
sub-neg99.2%
metadata-eval99.2%
+-commutative99.2%
neg-mul-199.2%
distribute-neg-in99.2%
metadata-eval99.2%
unsub-neg99.2%
Simplified99.2%
Final simplification74.6%
(FPCore (x) :precision binary64 (if (<= x 2.45) (+ 0.5 (* (* x x) -0.041666666666666664)) (/ (/ 2.0 x) x)))
double code(double x) {
double tmp;
if (x <= 2.45) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = (2.0 / x) / x;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 2.45d0) then
tmp = 0.5d0 + ((x * x) * (-0.041666666666666664d0))
else
tmp = (2.0d0 / x) / x
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 2.45) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = (2.0 / x) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.45: tmp = 0.5 + ((x * x) * -0.041666666666666664) else: tmp = (2.0 / x) / x return tmp
function code(x) tmp = 0.0 if (x <= 2.45) tmp = Float64(0.5 + Float64(Float64(x * x) * -0.041666666666666664)); else tmp = Float64(Float64(2.0 / x) / x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2.45) tmp = 0.5 + ((x * x) * -0.041666666666666664); else tmp = (2.0 / x) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2.45], N[(0.5 + N[(N[(x * x), $MachinePrecision] * -0.041666666666666664), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 / x), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.45:\\
\;\;\;\;0.5 + \left(x \cdot x\right) \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{2}{x}}{x}\\
\end{array}
\end{array}
if x < 2.4500000000000002Initial program 34.3%
Taylor expanded in x around 0 67.3%
*-commutative67.3%
unpow267.3%
Simplified67.3%
if 2.4500000000000002 < x Initial program 97.9%
frac-2neg97.9%
div-inv97.8%
distribute-rgt-neg-in97.8%
Applied egg-rr97.8%
un-div-inv97.9%
distribute-rgt-neg-out97.9%
frac-2neg97.9%
flip--97.7%
metadata-eval97.7%
1-sub-cos98.0%
unpow298.0%
associate-/r*99.5%
Applied egg-rr65.2%
Taylor expanded in x around 0 66.2%
Final simplification67.0%
(FPCore (x) :precision binary64 (if (<= x 2.0) 0.5 (/ 2.0 (* x x))))
double code(double x) {
double tmp;
if (x <= 2.0) {
tmp = 0.5;
} else {
tmp = 2.0 / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 2.0d0) then
tmp = 0.5d0
else
tmp = 2.0d0 / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 2.0) {
tmp = 0.5;
} else {
tmp = 2.0 / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.0: tmp = 0.5 else: tmp = 2.0 / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 2.0) tmp = 0.5; else tmp = Float64(2.0 / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2.0) tmp = 0.5; else tmp = 2.0 / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2.0], 0.5, N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{x \cdot x}\\
\end{array}
\end{array}
if x < 2Initial program 34.3%
Taylor expanded in x around 0 67.6%
if 2 < x Initial program 97.9%
frac-2neg97.9%
div-inv97.8%
distribute-rgt-neg-in97.8%
Applied egg-rr97.8%
un-div-inv97.9%
distribute-rgt-neg-out97.9%
frac-2neg97.9%
flip--97.7%
metadata-eval97.7%
1-sub-cos98.0%
unpow298.0%
associate-/r*99.5%
Applied egg-rr65.2%
Taylor expanded in x around 0 66.1%
unpow266.1%
Simplified66.1%
Final simplification67.3%
(FPCore (x) :precision binary64 (if (<= x 2.0) 0.5 (/ (/ 2.0 x) x)))
double code(double x) {
double tmp;
if (x <= 2.0) {
tmp = 0.5;
} else {
tmp = (2.0 / x) / x;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 2.0d0) then
tmp = 0.5d0
else
tmp = (2.0d0 / x) / x
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 2.0) {
tmp = 0.5;
} else {
tmp = (2.0 / x) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.0: tmp = 0.5 else: tmp = (2.0 / x) / x return tmp
function code(x) tmp = 0.0 if (x <= 2.0) tmp = 0.5; else tmp = Float64(Float64(2.0 / x) / x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2.0) tmp = 0.5; else tmp = (2.0 / x) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2.0], 0.5, N[(N[(2.0 / x), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{2}{x}}{x}\\
\end{array}
\end{array}
if x < 2Initial program 34.3%
Taylor expanded in x around 0 67.6%
if 2 < x Initial program 97.9%
frac-2neg97.9%
div-inv97.8%
distribute-rgt-neg-in97.8%
Applied egg-rr97.8%
un-div-inv97.9%
distribute-rgt-neg-out97.9%
frac-2neg97.9%
flip--97.7%
metadata-eval97.7%
1-sub-cos98.0%
unpow298.0%
associate-/r*99.5%
Applied egg-rr65.2%
Taylor expanded in x around 0 66.2%
Final simplification67.3%
(FPCore (x) :precision binary64 (if (<= x 9e+76) 0.5 0.0))
double code(double x) {
double tmp;
if (x <= 9e+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 <= 9d+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 <= 9e+76) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 9e+76: tmp = 0.5 else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 9e+76) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 9e+76) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 9e+76], 0.5, 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 9 \cdot 10^{+76}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 8.9999999999999995e76Initial program 37.6%
Taylor expanded in x around 0 64.4%
if 8.9999999999999995e76 < x Initial program 98.0%
Taylor expanded in x around 0 74.5%
Taylor expanded in x around 0 74.5%
Final simplification66.2%
(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 48.5%
Taylor expanded in x around 0 26.6%
Taylor expanded in x around 0 27.3%
Final simplification27.3%
herbie shell --seed 2023279
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))