
(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 3 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 (let* ((t_0 (/ (- 1.0 (cos x)) (* x x)))) (if (<= x -1.05e-8) t_0 (if (<= x 1.05e-8) (cos x) t_0))))
double code(double x) {
double t_0 = (1.0 - cos(x)) / (x * x);
double tmp;
if (x <= -1.05e-8) {
tmp = t_0;
} else if (x <= 1.05e-8) {
tmp = cos(x);
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = (1.0d0 - cos(x)) / (x * x)
if (x <= (-1.05d-8)) then
tmp = t_0
else if (x <= 1.05d-8) then
tmp = cos(x)
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x) {
double t_0 = (1.0 - Math.cos(x)) / (x * x);
double tmp;
if (x <= -1.05e-8) {
tmp = t_0;
} else if (x <= 1.05e-8) {
tmp = Math.cos(x);
} else {
tmp = t_0;
}
return tmp;
}
def code(x): t_0 = (1.0 - math.cos(x)) / (x * x) tmp = 0 if x <= -1.05e-8: tmp = t_0 elif x <= 1.05e-8: tmp = math.cos(x) else: tmp = t_0 return tmp
function code(x) t_0 = Float64(Float64(1.0 - cos(x)) / Float64(x * x)) tmp = 0.0 if (x <= -1.05e-8) tmp = t_0; elseif (x <= 1.05e-8) tmp = cos(x); else tmp = t_0; end return tmp end
function tmp_2 = code(x) t_0 = (1.0 - cos(x)) / (x * x); tmp = 0.0; if (x <= -1.05e-8) tmp = t_0; elseif (x <= 1.05e-8) tmp = cos(x); else tmp = t_0; end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -1.05e-8], t$95$0, If[LessEqual[x, 1.05e-8], N[Cos[x], $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1 - \cos x}{x \cdot x}\\
\mathbf{if}\;x \leq -1.05 \cdot 10^{-8}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \leq 1.05 \cdot 10^{-8}:\\
\;\;\;\;\cos x\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if x < -1.04999999999999997e-8 or 1.04999999999999997e-8 < x Initial program 97.3%
if -1.04999999999999997e-8 < x < 1.04999999999999997e-8Initial program 1.7%
Taylor expanded in x around 0
Applied rewrites18.8%
(FPCore (x) :precision binary64 (cos x))
double code(double x) {
return cos(x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = cos(x)
end function
public static double code(double x) {
return Math.cos(x);
}
def code(x): return math.cos(x)
function code(x) return cos(x) end
function tmp = code(x) tmp = cos(x); end
code[x_] := N[Cos[x], $MachinePrecision]
\begin{array}{l}
\\
\cos x
\end{array}
Initial program 52.1%
Taylor expanded in x around 0
Applied rewrites10.7%
(FPCore (x) :precision binary64 (* x x))
double code(double x) {
return x * x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = x * x
end function
public static double code(double x) {
return x * x;
}
def code(x): return x * x
function code(x) return Float64(x * x) end
function tmp = code(x) tmp = x * x; end
code[x_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x
\end{array}
Initial program 52.1%
Taylor expanded in x around 0
Applied rewrites3.3%
herbie shell --seed 2024321
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
:pre (TRUE)
(/ (- 1.0 (cos x)) (* x x)))