
(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) 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 56.8%
lift-cos.f64N/A
flip--N/A
metadata-evalN/A
lift-cos.f64N/A
lift-cos.f64N/A
1-sub-cosN/A
associate-/l*N/A
lower-*.f64N/A
lower-sin.f64N/A
lift-cos.f64N/A
hang-0p-tanN/A
lower-tan.f64N/A
lower-/.f6478.2
Applied egg-rr78.2%
lift-sin.f64N/A
lift-/.f64N/A
lift-tan.f64N/A
times-fracN/A
associate-*r/N/A
lower-/.f64N/A
lower-*.f64N/A
lower-/.f6499.8
lift-/.f64N/A
div-invN/A
lower-*.f64N/A
metadata-eval99.8
Applied egg-rr99.8%
(FPCore (x) :precision binary64 (if (<= x 0.0054) (fma -0.041666666666666664 (* x x) 0.5) (/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
double tmp;
if (x <= 0.0054) {
tmp = fma(-0.041666666666666664, (x * x), 0.5);
} else {
tmp = ((1.0 - cos(x)) / x) / x;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 0.0054) tmp = fma(-0.041666666666666664, Float64(x * x), 0.5); else tmp = Float64(Float64(Float64(1.0 - cos(x)) / x) / x); end return tmp end
code[x_] := If[LessEqual[x, 0.0054], N[(-0.041666666666666664 * N[(x * x), $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.0054:\\
\;\;\;\;\mathsf{fma}\left(-0.041666666666666664, x \cdot x, 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\
\end{array}
\end{array}
if x < 0.0054000000000000003Initial program 38.5%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6463.2
Simplified63.2%
if 0.0054000000000000003 < x Initial program 99.3%
Applied egg-rr99.5%
lift-cos.f64N/A
lift-+.f64N/A
lift-/.f64N/A
distribute-frac-neg2N/A
distribute-frac-negN/A
lower-/.f64N/A
lift-/.f64N/A
distribute-neg-fracN/A
lower-/.f64N/A
lift-+.f64N/A
+-commutativeN/A
distribute-neg-inN/A
metadata-evalN/A
unsub-negN/A
lower--.f6499.5
Applied egg-rr99.5%
(FPCore (x) :precision binary64 (if (<= x 0.0054) (fma -0.041666666666666664 (* x x) 0.5) (/ (- 1.0 (cos x)) (* x x))))
double code(double x) {
double tmp;
if (x <= 0.0054) {
tmp = fma(-0.041666666666666664, (x * x), 0.5);
} else {
tmp = (1.0 - cos(x)) / (x * x);
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 0.0054) tmp = fma(-0.041666666666666664, Float64(x * x), 0.5); else tmp = Float64(Float64(1.0 - cos(x)) / Float64(x * x)); end return tmp end
code[x_] := If[LessEqual[x, 0.0054], N[(-0.041666666666666664 * N[(x * x), $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.0054:\\
\;\;\;\;\mathsf{fma}\left(-0.041666666666666664, x \cdot x, 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x \cdot x}\\
\end{array}
\end{array}
if x < 0.0054000000000000003Initial program 38.5%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6463.2
Simplified63.2%
if 0.0054000000000000003 < x Initial program 99.3%
(FPCore (x)
:precision binary64
(if (<= x 1.4e+58)
0.5
(if (<= x 1.35e+154)
(- (/ 1.0 (* x x)) (/ (* x x) (* x (* x (* x x)))))
(* (* x 0.5) (/ x (* x x))))))
double code(double x) {
double tmp;
if (x <= 1.4e+58) {
tmp = 0.5;
} else if (x <= 1.35e+154) {
tmp = (1.0 / (x * x)) - ((x * x) / (x * (x * (x * x))));
} else {
tmp = (x * 0.5) * (x / (x * x));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.4d+58) then
tmp = 0.5d0
else if (x <= 1.35d+154) then
tmp = (1.0d0 / (x * x)) - ((x * x) / (x * (x * (x * x))))
else
tmp = (x * 0.5d0) * (x / (x * x))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.4e+58) {
tmp = 0.5;
} else if (x <= 1.35e+154) {
tmp = (1.0 / (x * x)) - ((x * x) / (x * (x * (x * x))));
} else {
tmp = (x * 0.5) * (x / (x * x));
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.4e+58: tmp = 0.5 elif x <= 1.35e+154: tmp = (1.0 / (x * x)) - ((x * x) / (x * (x * (x * x)))) else: tmp = (x * 0.5) * (x / (x * x)) return tmp
function code(x) tmp = 0.0 if (x <= 1.4e+58) tmp = 0.5; elseif (x <= 1.35e+154) tmp = Float64(Float64(1.0 / Float64(x * x)) - Float64(Float64(x * x) / Float64(x * Float64(x * Float64(x * x))))); else tmp = Float64(Float64(x * 0.5) * Float64(x / Float64(x * x))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.4e+58) tmp = 0.5; elseif (x <= 1.35e+154) tmp = (1.0 / (x * x)) - ((x * x) / (x * (x * (x * x)))); else tmp = (x * 0.5) * (x / (x * x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.4e+58], 0.5, If[LessEqual[x, 1.35e+154], N[(N[(1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision] - N[(N[(x * x), $MachinePrecision] / N[(x * N[(x * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x * 0.5), $MachinePrecision] * N[(x / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.4 \cdot 10^{+58}:\\
\;\;\;\;0.5\\
\mathbf{elif}\;x \leq 1.35 \cdot 10^{+154}:\\
\;\;\;\;\frac{1}{x \cdot x} - \frac{x \cdot x}{x \cdot \left(x \cdot \left(x \cdot x\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;\left(x \cdot 0.5\right) \cdot \frac{x}{x \cdot x}\\
\end{array}
\end{array}
if x < 1.3999999999999999e58Initial program 42.9%
Taylor expanded in x around 0
Simplified59.7%
if 1.3999999999999999e58 < x < 1.35000000000000003e154Initial program 99.0%
lift-cos.f64N/A
lift-*.f64N/A
div-subN/A
frac-subN/A
div-subN/A
*-lft-identityN/A
unpow1N/A
pow2N/A
pow-divN/A
metadata-evalN/A
inv-powN/A
lower--.f64N/A
lower-/.f64N/A
lower-/.f64N/A
lift-*.f64N/A
associate-*l*N/A
lower-*.f64N/A
lower-*.f64N/A
lift-*.f64N/A
Applied egg-rr22.5%
Taylor expanded in x around 0
Simplified17.4%
if 1.35000000000000003e154 < x Initial program 99.7%
Applied egg-rr100.0%
lift-cos.f64N/A
lift-+.f64N/A
lift-/.f64N/A
distribute-frac-neg2N/A
lift-/.f64N/A
associate-/l/N/A
lift-*.f64N/A
distribute-frac-negN/A
div-invN/A
lift-+.f64N/A
distribute-neg-inN/A
lift-neg.f64N/A
*-lft-identityN/A
metadata-evalN/A
lift-/.f64N/A
distribute-lft1-inN/A
*-lft-identityN/A
distribute-rgt-outN/A
Applied egg-rr99.7%
lift-*.f64N/A
lift-cos.f64N/A
lift--.f64N/A
associate-*l/N/A
*-lft-identityN/A
lift--.f64N/A
sub-divN/A
clear-numN/A
frac-subN/A
Applied egg-rr99.7%
Taylor expanded in x around 0
lower-*.f6499.7
Simplified99.7%
Final simplification63.4%
(FPCore (x) :precision binary64 (if (<= x 3.6e+75) 0.5 0.0))
double code(double x) {
double tmp;
if (x <= 3.6e+75) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 3.6d+75) then
tmp = 0.5d0
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 3.6e+75) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 3.6e+75: tmp = 0.5 else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 3.6e+75) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 3.6e+75) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 3.6e+75], 0.5, 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.6 \cdot 10^{+75}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 3.6e75Initial program 43.2%
Taylor expanded in x around 0
Simplified59.4%
if 3.6e75 < x Initial program 99.5%
Taylor expanded in x around 0
Simplified72.6%
metadata-evalN/A
lift-*.f64N/A
div072.6
Applied egg-rr72.6%
(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 56.8%
Taylor expanded in x around 0
Simplified33.1%
metadata-evalN/A
lift-*.f64N/A
div033.7
Applied egg-rr33.7%
herbie shell --seed 2024207
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))