
(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 8 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 2.0)) x) (/ (sin x) x)))
double code(double x) {
return (tan((x / 2.0)) / x) * (sin(x) / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (tan((x / 2.0d0)) / x) * (sin(x) / x)
end function
public static double code(double x) {
return (Math.tan((x / 2.0)) / x) * (Math.sin(x) / x);
}
def code(x): return (math.tan((x / 2.0)) / x) * (math.sin(x) / x)
function code(x) return Float64(Float64(tan(Float64(x / 2.0)) / x) * Float64(sin(x) / x)) end
function tmp = code(x) tmp = (tan((x / 2.0)) / x) * (sin(x) / x); end
code[x_] := N[(N[(N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision] * N[(N[Sin[x], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\tan \left(\frac{x}{2}\right)}{x} \cdot \frac{\sin x}{x}
\end{array}
Initial program 51.1%
lift--.f64N/A
flip--N/A
lower-/.f64N/A
metadata-evalN/A
lift-cos.f64N/A
lift-cos.f64N/A
1-sub-cosN/A
pow2N/A
lower-pow.f64N/A
lower-sin.f64N/A
+-commutativeN/A
lower-+.f6475.8
Applied rewrites75.8%
lift-/.f64N/A
lift-/.f64N/A
associate-/l/N/A
lift-pow.f64N/A
unpow2N/A
times-fracN/A
lower-*.f64N/A
lift-sin.f64N/A
lift-+.f64N/A
+-commutativeN/A
lift-cos.f64N/A
hang-0p-tanN/A
lower-tan.f64N/A
lower-/.f64N/A
lower-/.f6476.1
Applied rewrites76.1%
lift-*.f64N/A
lift-/.f64N/A
associate-*r/N/A
lift-*.f64N/A
times-fracN/A
lower-*.f64N/A
lower-/.f64N/A
lower-/.f6499.8
Applied rewrites99.8%
(FPCore (x) :precision binary64 (if (<= x 0.0002) (fma (* x x) -0.041666666666666664 0.5) (* (tan (/ x 2.0)) (/ (sin x) (* x x)))))
double code(double x) {
double tmp;
if (x <= 0.0002) {
tmp = fma((x * x), -0.041666666666666664, 0.5);
} else {
tmp = tan((x / 2.0)) * (sin(x) / (x * x));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 0.0002) tmp = fma(Float64(x * x), -0.041666666666666664, 0.5); else tmp = Float64(tan(Float64(x / 2.0)) * Float64(sin(x) / Float64(x * x))); end return tmp end
code[x_] := If[LessEqual[x, 0.0002], N[(N[(x * x), $MachinePrecision] * -0.041666666666666664 + 0.5), $MachinePrecision], N[(N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision] * N[(N[Sin[x], $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0002:\\
\;\;\;\;\mathsf{fma}\left(x \cdot x, -0.041666666666666664, 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\tan \left(\frac{x}{2}\right) \cdot \frac{\sin x}{x \cdot x}\\
\end{array}
\end{array}
if x < 2.0000000000000001e-4Initial program 34.7%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6467.0
Applied rewrites67.0%
if 2.0000000000000001e-4 < x Initial program 99.3%
lift--.f64N/A
flip--N/A
lower-/.f64N/A
metadata-evalN/A
lift-cos.f64N/A
lift-cos.f64N/A
1-sub-cosN/A
pow2N/A
lower-pow.f64N/A
lower-sin.f64N/A
+-commutativeN/A
lower-+.f6499.2
Applied rewrites99.2%
lift-/.f64N/A
lift-/.f64N/A
associate-/l/N/A
lift-pow.f64N/A
unpow2N/A
times-fracN/A
lower-*.f64N/A
lift-sin.f64N/A
lift-+.f64N/A
+-commutativeN/A
lift-cos.f64N/A
hang-0p-tanN/A
lower-tan.f64N/A
lower-/.f64N/A
lower-/.f6499.6
Applied rewrites99.6%
(FPCore (x) :precision binary64 (if (<= x 0.0056) (fma (* x x) -0.041666666666666664 0.5) (/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
double tmp;
if (x <= 0.0056) {
tmp = fma((x * x), -0.041666666666666664, 0.5);
} else {
tmp = ((1.0 - cos(x)) / x) / x;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 0.0056) tmp = fma(Float64(x * x), -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.0056], N[(N[(x * x), $MachinePrecision] * -0.041666666666666664 + 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.0056:\\
\;\;\;\;\mathsf{fma}\left(x \cdot x, -0.041666666666666664, 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\
\end{array}
\end{array}
if x < 0.00559999999999999994Initial program 34.7%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6467.0
Applied rewrites67.0%
if 0.00559999999999999994 < x Initial program 99.3%
lift-/.f64N/A
lift-*.f64N/A
associate-/r*N/A
lower-/.f64N/A
lower-/.f6499.3
Applied rewrites99.3%
(FPCore (x) :precision binary64 (if (<= x 0.0056) (fma (* x x) -0.041666666666666664 0.5) (/ (- 1.0 (cos x)) (* x x))))
double code(double x) {
double tmp;
if (x <= 0.0056) {
tmp = fma((x * x), -0.041666666666666664, 0.5);
} else {
tmp = (1.0 - cos(x)) / (x * x);
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 0.0056) tmp = fma(Float64(x * x), -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.0056], N[(N[(x * x), $MachinePrecision] * -0.041666666666666664 + 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.0056:\\
\;\;\;\;\mathsf{fma}\left(x \cdot x, -0.041666666666666664, 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x \cdot x}\\
\end{array}
\end{array}
if x < 0.00559999999999999994Initial program 34.7%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6467.0
Applied rewrites67.0%
if 0.00559999999999999994 < x Initial program 99.3%
(FPCore (x)
:precision binary64
(let* ((t_0 (/ x (* x x))))
(if (<= x 290000000000.0)
(fma (* x x) (fma (* 0.001388888888888889 x) x -0.041666666666666664) 0.5)
(fma t_0 t_0 (/ -1.0 (* x x))))))
double code(double x) {
double t_0 = x / (x * x);
double tmp;
if (x <= 290000000000.0) {
tmp = fma((x * x), fma((0.001388888888888889 * x), x, -0.041666666666666664), 0.5);
} else {
tmp = fma(t_0, t_0, (-1.0 / (x * x)));
}
return tmp;
}
function code(x) t_0 = Float64(x / Float64(x * x)) tmp = 0.0 if (x <= 290000000000.0) tmp = fma(Float64(x * x), fma(Float64(0.001388888888888889 * x), x, -0.041666666666666664), 0.5); else tmp = fma(t_0, t_0, Float64(-1.0 / Float64(x * x))); end return tmp end
code[x_] := Block[{t$95$0 = N[(x / N[(x * x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, 290000000000.0], N[(N[(x * x), $MachinePrecision] * N[(N[(0.001388888888888889 * x), $MachinePrecision] * x + -0.041666666666666664), $MachinePrecision] + 0.5), $MachinePrecision], N[(t$95$0 * t$95$0 + N[(-1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x}{x \cdot x}\\
\mathbf{if}\;x \leq 290000000000:\\
\;\;\;\;\mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(0.001388888888888889 \cdot x, x, -0.041666666666666664\right), 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t\_0, t\_0, \frac{-1}{x \cdot x}\right)\\
\end{array}
\end{array}
if x < 2.9e11Initial program 35.4%
Taylor expanded in x around 0
Applied rewrites66.8%
if 2.9e11 < x Initial program 99.3%
lift--.f64N/A
flip--N/A
lower-/.f64N/A
metadata-evalN/A
lift-cos.f64N/A
lift-cos.f64N/A
1-sub-cosN/A
pow2N/A
lower-pow.f64N/A
lower-sin.f64N/A
+-commutativeN/A
lower-+.f6499.2
Applied rewrites99.2%
lift-/.f64N/A
lift-/.f64N/A
associate-/l/N/A
lift-pow.f64N/A
unpow2N/A
times-fracN/A
lower-*.f64N/A
lift-sin.f64N/A
lift-+.f64N/A
+-commutativeN/A
lift-cos.f64N/A
hang-0p-tanN/A
lower-tan.f64N/A
lower-/.f64N/A
lower-/.f6499.6
Applied rewrites99.6%
Applied rewrites98.7%
Taylor expanded in x around 0
Applied rewrites53.3%
(FPCore (x) :precision binary64 (if (<= x 2.15e+80) 0.5 (fma (/ -1.0 x) (/ -1.0 x) (/ -1.0 (* x x)))))
double code(double x) {
double tmp;
if (x <= 2.15e+80) {
tmp = 0.5;
} else {
tmp = fma((-1.0 / x), (-1.0 / x), (-1.0 / (x * x)));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 2.15e+80) tmp = 0.5; else tmp = fma(Float64(-1.0 / x), Float64(-1.0 / x), Float64(-1.0 / Float64(x * x))); end return tmp end
code[x_] := If[LessEqual[x, 2.15e+80], 0.5, N[(N[(-1.0 / x), $MachinePrecision] * N[(-1.0 / x), $MachinePrecision] + N[(-1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.15 \cdot 10^{+80}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{-1}{x}, \frac{-1}{x}, \frac{-1}{x \cdot x}\right)\\
\end{array}
\end{array}
if x < 2.15000000000000002e80Initial program 40.2%
Taylor expanded in x around 0
Applied rewrites62.5%
if 2.15000000000000002e80 < x Initial program 99.7%
lift--.f64N/A
flip--N/A
lower-/.f64N/A
metadata-evalN/A
lift-cos.f64N/A
lift-cos.f64N/A
1-sub-cosN/A
pow2N/A
lower-pow.f64N/A
lower-sin.f64N/A
+-commutativeN/A
lower-+.f6499.5
Applied rewrites99.5%
lift-/.f64N/A
lift-/.f64N/A
lift-pow.f64N/A
unpow2N/A
lift-sin.f64N/A
lift-sin.f64N/A
1-sub-cosN/A
metadata-evalN/A
lift-cos.f64N/A
lift-cos.f64N/A
lift-+.f64N/A
+-commutativeN/A
flip--N/A
*-rgt-identityN/A
fp-cancel-sub-sign-invN/A
div-addN/A
Applied rewrites99.4%
Taylor expanded in x around 0
Applied rewrites69.5%
Final simplification63.8%
(FPCore (x) :precision binary64 (if (<= x 7.6e+76) 0.5 0.0))
double code(double x) {
double tmp;
if (x <= 7.6e+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 <= 7.6d+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 <= 7.6e+76) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 7.6e+76: tmp = 0.5 else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 7.6e+76) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 7.6e+76) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 7.6e+76], 0.5, 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 7.6 \cdot 10^{+76}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 7.60000000000000049e76Initial program 39.6%
Taylor expanded in x around 0
Applied rewrites63.1%
if 7.60000000000000049e76 < x Initial program 99.7%
lift--.f64N/A
flip--N/A
lower-/.f64N/A
metadata-evalN/A
lift-cos.f64N/A
lift-cos.f64N/A
1-sub-cosN/A
pow2N/A
lower-pow.f64N/A
lower-sin.f64N/A
+-commutativeN/A
lower-+.f6499.5
Applied rewrites99.5%
lift-/.f64N/A
frac-2negN/A
lift-/.f64N/A
distribute-neg-fracN/A
lift-pow.f64N/A
unpow2N/A
lift-sin.f64N/A
lift-sin.f64N/A
sub-1-cosN/A
lift-cos.f64N/A
lift-cos.f64N/A
metadata-evalN/A
lift-+.f64N/A
flip--N/A
mul-1-negN/A
lift-*.f64N/A
div-subN/A
metadata-evalN/A
lift-*.f64N/A
mul-1-negN/A
Applied rewrites99.6%
lift-neg.f64N/A
lift-cos.f64N/A
cos-neg-revN/A
lift-neg.f64N/A
cos-+PI-revN/A
lower-cos.f64N/A
lower-+.f64N/A
lower-PI.f6469.9
Applied rewrites69.9%
Taylor expanded in x around 0
cos-PIN/A
metadata-evalN/A
div066.6
Applied rewrites66.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 51.1%
lift--.f64N/A
flip--N/A
lower-/.f64N/A
metadata-evalN/A
lift-cos.f64N/A
lift-cos.f64N/A
1-sub-cosN/A
pow2N/A
lower-pow.f64N/A
lower-sin.f64N/A
+-commutativeN/A
lower-+.f6475.8
Applied rewrites75.8%
lift-/.f64N/A
frac-2negN/A
lift-/.f64N/A
distribute-neg-fracN/A
lift-pow.f64N/A
unpow2N/A
lift-sin.f64N/A
lift-sin.f64N/A
sub-1-cosN/A
lift-cos.f64N/A
lift-cos.f64N/A
metadata-evalN/A
lift-+.f64N/A
flip--N/A
mul-1-negN/A
lift-*.f64N/A
div-subN/A
metadata-evalN/A
lift-*.f64N/A
mul-1-negN/A
Applied rewrites50.9%
lift-neg.f64N/A
lift-cos.f64N/A
cos-neg-revN/A
lift-neg.f64N/A
cos-+PI-revN/A
lower-cos.f64N/A
lower-+.f64N/A
lower-PI.f6431.0
Applied rewrites31.0%
Taylor expanded in x around 0
cos-PIN/A
metadata-evalN/A
div027.6
Applied rewrites27.6%
herbie shell --seed 2024340
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))