
(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 7 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)) / x) * Float64(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] / x), $MachinePrecision] * N[(N[Sin[x], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\tan \left(x \cdot 0.5\right)}{x} \cdot \frac{\sin x}{x}
\end{array}
Initial program 50.5%
flip--N/A
associate-/l/N/A
metadata-evalN/A
1-sub-cosN/A
times-fracN/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
sin-lowering-sin.f64N/A
*-lowering-*.f64N/A
hang-0p-tanN/A
tan-lowering-tan.f64N/A
/-lowering-/.f6478.6
Applied egg-rr78.6%
*-commutativeN/A
associate-*r/N/A
times-fracN/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
tan-lowering-tan.f64N/A
div-invN/A
*-lowering-*.f64N/A
metadata-evalN/A
/-lowering-/.f64N/A
sin-lowering-sin.f6499.8
Applied egg-rr99.8%
(FPCore (x) :precision binary64 (if (<= x 0.031) (fma x (* x (fma x (* x 0.001388888888888889) -0.041666666666666664)) 0.5) (/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
double tmp;
if (x <= 0.031) {
tmp = fma(x, (x * fma(x, (x * 0.001388888888888889), -0.041666666666666664)), 0.5);
} else {
tmp = ((1.0 - cos(x)) / x) / x;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 0.031) tmp = fma(x, Float64(x * fma(x, Float64(x * 0.001388888888888889), -0.041666666666666664)), 0.5); else tmp = Float64(Float64(Float64(1.0 - cos(x)) / x) / x); end return tmp end
code[x_] := If[LessEqual[x, 0.031], N[(x * N[(x * N[(x * N[(x * 0.001388888888888889), $MachinePrecision] + -0.041666666666666664), $MachinePrecision]), $MachinePrecision] + 0.5), $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.031:\\
\;\;\;\;\mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x, x \cdot 0.001388888888888889, -0.041666666666666664\right), 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\
\end{array}
\end{array}
if x < 0.031Initial program 36.6%
flip--N/A
associate-/l/N/A
metadata-evalN/A
1-sub-cosN/A
times-fracN/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
sin-lowering-sin.f64N/A
*-lowering-*.f64N/A
hang-0p-tanN/A
tan-lowering-tan.f64N/A
/-lowering-/.f6472.8
Applied egg-rr72.8%
associate-*l/N/A
associate-/l*N/A
*-commutativeN/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
tan-lowering-tan.f64N/A
div-invN/A
*-lowering-*.f64N/A
metadata-evalN/A
*-lowering-*.f64N/A
sin-lowering-sin.f6472.8
Applied egg-rr72.8%
Taylor expanded in x around 0
+-commutativeN/A
unpow2N/A
associate-*l*N/A
*-commutativeN/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
sub-negN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
metadata-evalN/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f6465.6
Simplified65.6%
if 0.031 < x Initial program 98.0%
Applied egg-rr97.9%
associate-*l/N/A
frac-timesN/A
*-commutativeN/A
associate-*r/N/A
/-lowering-/.f64N/A
associate-*l/N/A
*-commutativeN/A
neg-mul-1N/A
+-commutativeN/A
distribute-neg-inN/A
metadata-evalN/A
sub-negN/A
/-lowering-/.f64N/A
--lowering--.f64N/A
cos-lowering-cos.f6499.5
Applied egg-rr99.5%
(FPCore (x) :precision binary64 (if (<= x 0.031) (fma x (* x (fma x (* x 0.001388888888888889) -0.041666666666666664)) 0.5) (/ (- 1.0 (cos x)) (* x x))))
double code(double x) {
double tmp;
if (x <= 0.031) {
tmp = fma(x, (x * fma(x, (x * 0.001388888888888889), -0.041666666666666664)), 0.5);
} else {
tmp = (1.0 - cos(x)) / (x * x);
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 0.031) tmp = fma(x, Float64(x * fma(x, Float64(x * 0.001388888888888889), -0.041666666666666664)), 0.5); else tmp = Float64(Float64(1.0 - cos(x)) / Float64(x * x)); end return tmp end
code[x_] := If[LessEqual[x, 0.031], N[(x * N[(x * N[(x * N[(x * 0.001388888888888889), $MachinePrecision] + -0.041666666666666664), $MachinePrecision]), $MachinePrecision] + 0.5), $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.031:\\
\;\;\;\;\mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x, x \cdot 0.001388888888888889, -0.041666666666666664\right), 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x \cdot x}\\
\end{array}
\end{array}
if x < 0.031Initial program 36.6%
flip--N/A
associate-/l/N/A
metadata-evalN/A
1-sub-cosN/A
times-fracN/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
sin-lowering-sin.f64N/A
*-lowering-*.f64N/A
hang-0p-tanN/A
tan-lowering-tan.f64N/A
/-lowering-/.f6472.8
Applied egg-rr72.8%
associate-*l/N/A
associate-/l*N/A
*-commutativeN/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
tan-lowering-tan.f64N/A
div-invN/A
*-lowering-*.f64N/A
metadata-evalN/A
*-lowering-*.f64N/A
sin-lowering-sin.f6472.8
Applied egg-rr72.8%
Taylor expanded in x around 0
+-commutativeN/A
unpow2N/A
associate-*l*N/A
*-commutativeN/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
sub-negN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
metadata-evalN/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f6465.6
Simplified65.6%
if 0.031 < x Initial program 98.0%
(FPCore (x) :precision binary64 (if (<= x 1.5e+29) (fma x (* x (fma x (* x 0.001388888888888889) -0.041666666666666664)) 0.5) (/ (fma (/ 1.0 (* x x)) x (/ -1.0 x)) x)))
double code(double x) {
double tmp;
if (x <= 1.5e+29) {
tmp = fma(x, (x * fma(x, (x * 0.001388888888888889), -0.041666666666666664)), 0.5);
} else {
tmp = fma((1.0 / (x * x)), x, (-1.0 / x)) / x;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 1.5e+29) tmp = fma(x, Float64(x * fma(x, Float64(x * 0.001388888888888889), -0.041666666666666664)), 0.5); else tmp = Float64(fma(Float64(1.0 / Float64(x * x)), x, Float64(-1.0 / x)) / x); end return tmp end
code[x_] := If[LessEqual[x, 1.5e+29], N[(x * N[(x * N[(x * N[(x * 0.001388888888888889), $MachinePrecision] + -0.041666666666666664), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision], N[(N[(N[(1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision] * x + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.5 \cdot 10^{+29}:\\
\;\;\;\;\mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x, x \cdot 0.001388888888888889, -0.041666666666666664\right), 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{1}{x \cdot x}, x, \frac{-1}{x}\right)}{x}\\
\end{array}
\end{array}
if x < 1.5e29Initial program 38.8%
flip--N/A
associate-/l/N/A
metadata-evalN/A
1-sub-cosN/A
times-fracN/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
sin-lowering-sin.f64N/A
*-lowering-*.f64N/A
hang-0p-tanN/A
tan-lowering-tan.f64N/A
/-lowering-/.f6473.7
Applied egg-rr73.7%
associate-*l/N/A
associate-/l*N/A
*-commutativeN/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
tan-lowering-tan.f64N/A
div-invN/A
*-lowering-*.f64N/A
metadata-evalN/A
*-lowering-*.f64N/A
sin-lowering-sin.f6473.7
Applied egg-rr73.7%
Taylor expanded in x around 0
+-commutativeN/A
unpow2N/A
associate-*l*N/A
*-commutativeN/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
sub-negN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
metadata-evalN/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f6463.5
Simplified63.5%
if 1.5e29 < x Initial program 97.8%
Applied egg-rr97.7%
+-commutativeN/A
distribute-lft-inN/A
associate-*l/N/A
metadata-evalN/A
/-rgt-identityN/A
associate-/r*N/A
associate-*l/N/A
frac-addN/A
*-lft-identityN/A
/-lowering-/.f64N/A
Applied egg-rr97.6%
Taylor expanded in x around 0
/-lowering-/.f6455.9
Simplified55.9%
Final simplification62.0%
(FPCore (x) :precision binary64 (if (<= x 6.5e+38) (fma x (* x (fma x (* x 0.001388888888888889) -0.041666666666666664)) 0.5) 0.0))
double code(double x) {
double tmp;
if (x <= 6.5e+38) {
tmp = fma(x, (x * fma(x, (x * 0.001388888888888889), -0.041666666666666664)), 0.5);
} else {
tmp = 0.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 6.5e+38) tmp = fma(x, Float64(x * fma(x, Float64(x * 0.001388888888888889), -0.041666666666666664)), 0.5); else tmp = 0.0; end return tmp end
code[x_] := If[LessEqual[x, 6.5e+38], N[(x * N[(x * N[(x * N[(x * 0.001388888888888889), $MachinePrecision] + -0.041666666666666664), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 6.5 \cdot 10^{+38}:\\
\;\;\;\;\mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x, x \cdot 0.001388888888888889, -0.041666666666666664\right), 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 6.5e38Initial program 39.4%
flip--N/A
associate-/l/N/A
metadata-evalN/A
1-sub-cosN/A
times-fracN/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
sin-lowering-sin.f64N/A
*-lowering-*.f64N/A
hang-0p-tanN/A
tan-lowering-tan.f64N/A
/-lowering-/.f6474.0
Applied egg-rr74.0%
associate-*l/N/A
associate-/l*N/A
*-commutativeN/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
tan-lowering-tan.f64N/A
div-invN/A
*-lowering-*.f64N/A
metadata-evalN/A
*-lowering-*.f64N/A
sin-lowering-sin.f6474.0
Applied egg-rr74.0%
Taylor expanded in x around 0
+-commutativeN/A
unpow2N/A
associate-*l*N/A
*-commutativeN/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
sub-negN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
metadata-evalN/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f6463.0
Simplified63.0%
if 6.5e38 < x Initial program 97.7%
Taylor expanded in x around 0
Simplified57.5%
metadata-evalN/A
div057.5
Applied egg-rr57.5%
(FPCore (x) :precision binary64 (if (<= x 8.5e+76) 0.5 0.0))
double code(double x) {
double tmp;
if (x <= 8.5e+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 <= 8.5d+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 <= 8.5e+76) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 8.5e+76: tmp = 0.5 else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 8.5e+76) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 8.5e+76) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 8.5e+76], 0.5, 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 8.5 \cdot 10^{+76}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 8.49999999999999992e76Initial program 42.1%
Taylor expanded in x around 0
Simplified60.6%
if 8.49999999999999992e76 < x Initial program 97.4%
Taylor expanded in x around 0
Simplified71.2%
metadata-evalN/A
div071.2
Applied egg-rr71.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 50.5%
Taylor expanded in x around 0
Simplified26.5%
metadata-evalN/A
div027.1
Applied egg-rr27.1%
herbie shell --seed 2024194
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))