
(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}
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (if (<= x 0.0002) (+ 0.5 (* (* x x) -0.041666666666666664)) (* (/ (sin x) (* x x)) (tan (/ x 2.0)))))
x = abs(x);
double code(double x) {
double tmp;
if (x <= 0.0002) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = (sin(x) / (x * x)) * tan((x / 2.0));
}
return tmp;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0002d0) then
tmp = 0.5d0 + ((x * x) * (-0.041666666666666664d0))
else
tmp = (sin(x) / (x * x)) * tan((x / 2.0d0))
end if
code = tmp
end function
x = Math.abs(x);
public static double code(double x) {
double tmp;
if (x <= 0.0002) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = (Math.sin(x) / (x * x)) * Math.tan((x / 2.0));
}
return tmp;
}
x = abs(x) def code(x): tmp = 0 if x <= 0.0002: tmp = 0.5 + ((x * x) * -0.041666666666666664) else: tmp = (math.sin(x) / (x * x)) * math.tan((x / 2.0)) return tmp
x = abs(x) function code(x) tmp = 0.0 if (x <= 0.0002) tmp = Float64(0.5 + Float64(Float64(x * x) * -0.041666666666666664)); else tmp = Float64(Float64(sin(x) / Float64(x * x)) * tan(Float64(x / 2.0))); end return tmp end
x = abs(x) function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0002) tmp = 0.5 + ((x * x) * -0.041666666666666664); else tmp = (sin(x) / (x * x)) * tan((x / 2.0)); end tmp_2 = tmp; end
NOTE: x should be positive before calling this function code[x_] := If[LessEqual[x, 0.0002], N[(0.5 + N[(N[(x * x), $MachinePrecision] * -0.041666666666666664), $MachinePrecision]), $MachinePrecision], N[(N[(N[Sin[x], $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision] * N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0002:\\
\;\;\;\;0.5 + \left(x \cdot x\right) \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{\sin x}{x \cdot x} \cdot \tan \left(\frac{x}{2}\right)\\
\end{array}
\end{array}
if x < 2.0000000000000001e-4Initial program 32.9%
Taylor expanded in x around 0 69.4%
*-commutative69.4%
unpow269.4%
Simplified69.4%
if 2.0000000000000001e-4 < x Initial program 99.1%
frac-2neg99.1%
div-inv98.9%
distribute-rgt-neg-in98.9%
Applied egg-rr98.9%
add-sqr-sqrt0.0%
sqrt-unprod49.2%
sqr-neg49.2%
sqrt-unprod49.2%
add-sqr-sqrt49.2%
flip--49.2%
frac-times49.2%
metadata-eval49.2%
1-sub-cos49.2%
pow249.2%
add-sqr-sqrt0.0%
sqrt-unprod71.4%
swap-sqr71.4%
sqr-neg71.4%
sqrt-unprod98.6%
add-sqr-sqrt98.6%
Applied egg-rr98.6%
*-rgt-identity98.6%
unpow298.6%
*-commutative98.6%
times-frac98.8%
hang-0p-tan99.7%
Simplified99.7%
Final simplification77.2%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (/ (tan (* x 0.5)) (* x (/ x (sin x)))))
x = abs(x);
double code(double x) {
return tan((x * 0.5)) / (x * (x / sin(x)));
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
code = tan((x * 0.5d0)) / (x * (x / sin(x)))
end function
x = Math.abs(x);
public static double code(double x) {
return Math.tan((x * 0.5)) / (x * (x / Math.sin(x)));
}
x = abs(x) def code(x): return math.tan((x * 0.5)) / (x * (x / math.sin(x)))
x = abs(x) function code(x) return Float64(tan(Float64(x * 0.5)) / Float64(x * Float64(x / sin(x)))) end
x = abs(x) function tmp = code(x) tmp = tan((x * 0.5)) / (x * (x / sin(x))); end
NOTE: x should be positive before calling this function code[x_] := N[(N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision] / N[(x * N[(x / N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x = |x|\\
\\
\frac{\tan \left(x \cdot 0.5\right)}{x \cdot \frac{x}{\sin x}}
\end{array}
Initial program 49.9%
flip--49.7%
div-inv49.6%
metadata-eval49.6%
1-sub-cos76.5%
pow276.5%
Applied egg-rr76.5%
unpow276.5%
associate-*l*76.5%
associate-*r/76.5%
*-rgt-identity76.5%
hang-0p-tan76.9%
Simplified76.9%
*-commutative76.9%
times-frac99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
*-commutative99.8%
clear-num99.8%
frac-times99.8%
*-un-lft-identity99.8%
Applied egg-rr99.8%
Final simplification99.8%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (* (/ (tan (* x 0.5)) x) (/ (sin x) x)))
x = abs(x);
double code(double x) {
return (tan((x * 0.5)) / x) * (sin(x) / x);
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
code = (tan((x * 0.5d0)) / x) * (sin(x) / x)
end function
x = Math.abs(x);
public static double code(double x) {
return (Math.tan((x * 0.5)) / x) * (Math.sin(x) / x);
}
x = abs(x) def code(x): return (math.tan((x * 0.5)) / x) * (math.sin(x) / x)
x = abs(x) function code(x) return Float64(Float64(tan(Float64(x * 0.5)) / x) * Float64(sin(x) / x)) end
x = abs(x) function tmp = code(x) tmp = (tan((x * 0.5)) / x) * (sin(x) / x); end
NOTE: x should be positive before calling this function 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}
x = |x|\\
\\
\frac{\tan \left(x \cdot 0.5\right)}{x} \cdot \frac{\sin x}{x}
\end{array}
Initial program 49.9%
flip--49.7%
div-inv49.6%
metadata-eval49.6%
1-sub-cos76.5%
pow276.5%
Applied egg-rr76.5%
unpow276.5%
associate-*l*76.5%
associate-*r/76.5%
*-rgt-identity76.5%
hang-0p-tan76.9%
Simplified76.9%
*-commutative76.9%
times-frac99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
Final simplification99.8%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (if (<= x 0.0054) (+ 0.5 (* (* x x) -0.041666666666666664)) (* (/ (- 1.0 (cos x)) x) (/ 1.0 x))))
x = abs(x);
double code(double x) {
double tmp;
if (x <= 0.0054) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = ((1.0 - cos(x)) / x) * (1.0 / x);
}
return tmp;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0054d0) then
tmp = 0.5d0 + ((x * x) * (-0.041666666666666664d0))
else
tmp = ((1.0d0 - cos(x)) / x) * (1.0d0 / x)
end if
code = tmp
end function
x = Math.abs(x);
public static double code(double x) {
double tmp;
if (x <= 0.0054) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = ((1.0 - Math.cos(x)) / x) * (1.0 / x);
}
return tmp;
}
x = abs(x) def code(x): tmp = 0 if x <= 0.0054: tmp = 0.5 + ((x * x) * -0.041666666666666664) else: tmp = ((1.0 - math.cos(x)) / x) * (1.0 / x) return tmp
x = abs(x) function code(x) tmp = 0.0 if (x <= 0.0054) tmp = Float64(0.5 + Float64(Float64(x * x) * -0.041666666666666664)); else tmp = Float64(Float64(Float64(1.0 - cos(x)) / x) * Float64(1.0 / x)); end return tmp end
x = abs(x) function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0054) tmp = 0.5 + ((x * x) * -0.041666666666666664); else tmp = ((1.0 - cos(x)) / x) * (1.0 / x); end tmp_2 = tmp; end
NOTE: x should be positive before calling this function code[x_] := If[LessEqual[x, 0.0054], N[(0.5 + N[(N[(x * x), $MachinePrecision] * -0.041666666666666664), $MachinePrecision]), $MachinePrecision], N[(N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] * N[(1.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0054:\\
\;\;\;\;0.5 + \left(x \cdot x\right) \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x} \cdot \frac{1}{x}\\
\end{array}
\end{array}
if x < 0.0054000000000000003Initial program 32.9%
Taylor expanded in x around 0 69.4%
*-commutative69.4%
unpow269.4%
Simplified69.4%
if 0.0054000000000000003 < x Initial program 99.1%
associate-/r*99.0%
div-inv99.1%
Applied egg-rr99.1%
Final simplification77.1%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (if (<= x 0.0054) (+ 0.5 (* (* x x) -0.041666666666666664)) (/ (/ (+ (cos x) -1.0) (- x)) x)))
x = abs(x);
double code(double x) {
double tmp;
if (x <= 0.0054) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = ((cos(x) + -1.0) / -x) / x;
}
return tmp;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0054d0) then
tmp = 0.5d0 + ((x * x) * (-0.041666666666666664d0))
else
tmp = ((cos(x) + (-1.0d0)) / -x) / x
end if
code = tmp
end function
x = Math.abs(x);
public static double code(double x) {
double tmp;
if (x <= 0.0054) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = ((Math.cos(x) + -1.0) / -x) / x;
}
return tmp;
}
x = abs(x) def code(x): tmp = 0 if x <= 0.0054: tmp = 0.5 + ((x * x) * -0.041666666666666664) else: tmp = ((math.cos(x) + -1.0) / -x) / x return tmp
x = abs(x) function code(x) tmp = 0.0 if (x <= 0.0054) tmp = Float64(0.5 + Float64(Float64(x * x) * -0.041666666666666664)); else tmp = Float64(Float64(Float64(cos(x) + -1.0) / Float64(-x)) / x); end return tmp end
x = abs(x) function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0054) tmp = 0.5 + ((x * x) * -0.041666666666666664); else tmp = ((cos(x) + -1.0) / -x) / x; end tmp_2 = tmp; end
NOTE: x should be positive before calling this function code[x_] := If[LessEqual[x, 0.0054], N[(0.5 + N[(N[(x * x), $MachinePrecision] * -0.041666666666666664), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[Cos[x], $MachinePrecision] + -1.0), $MachinePrecision] / (-x)), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0054:\\
\;\;\;\;0.5 + \left(x \cdot x\right) \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{\cos x + -1}{-x}}{x}\\
\end{array}
\end{array}
if x < 0.0054000000000000003Initial program 32.9%
Taylor expanded in x around 0 69.4%
*-commutative69.4%
unpow269.4%
Simplified69.4%
if 0.0054000000000000003 < x Initial program 99.1%
frac-2neg99.1%
div-inv98.9%
distribute-rgt-neg-in98.9%
Applied egg-rr98.9%
Taylor expanded in x around 0 98.9%
unpow298.9%
associate-/r*99.0%
Simplified99.0%
associate-*r/99.1%
frac-2neg99.1%
metadata-eval99.1%
un-div-inv99.0%
neg-sub099.0%
metadata-eval99.0%
associate--r-99.0%
metadata-eval99.0%
metadata-eval99.0%
Applied egg-rr99.0%
Final simplification77.1%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (if (<= x 0.0054) (+ 0.5 (* (* x x) -0.041666666666666664)) (/ (- 1.0 (cos x)) (* x x))))
x = abs(x);
double code(double x) {
double tmp;
if (x <= 0.0054) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = (1.0 - cos(x)) / (x * x);
}
return tmp;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0054d0) then
tmp = 0.5d0 + ((x * x) * (-0.041666666666666664d0))
else
tmp = (1.0d0 - cos(x)) / (x * x)
end if
code = tmp
end function
x = Math.abs(x);
public static double code(double x) {
double tmp;
if (x <= 0.0054) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = (1.0 - Math.cos(x)) / (x * x);
}
return tmp;
}
x = abs(x) def code(x): tmp = 0 if x <= 0.0054: tmp = 0.5 + ((x * x) * -0.041666666666666664) else: tmp = (1.0 - math.cos(x)) / (x * x) return tmp
x = abs(x) function code(x) tmp = 0.0 if (x <= 0.0054) 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
x = abs(x) function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0054) tmp = 0.5 + ((x * x) * -0.041666666666666664); else tmp = (1.0 - cos(x)) / (x * x); end tmp_2 = tmp; end
NOTE: x should be positive before calling this function code[x_] := If[LessEqual[x, 0.0054], 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}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0054:\\
\;\;\;\;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.0054000000000000003Initial program 32.9%
Taylor expanded in x around 0 69.4%
*-commutative69.4%
unpow269.4%
Simplified69.4%
if 0.0054000000000000003 < x Initial program 99.1%
Final simplification77.1%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (/ (tan (* x 0.5)) x))
x = abs(x);
double code(double x) {
return tan((x * 0.5)) / x;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
code = tan((x * 0.5d0)) / x
end function
x = Math.abs(x);
public static double code(double x) {
return Math.tan((x * 0.5)) / x;
}
x = abs(x) def code(x): return math.tan((x * 0.5)) / x
x = abs(x) function code(x) return Float64(tan(Float64(x * 0.5)) / x) end
x = abs(x) function tmp = code(x) tmp = tan((x * 0.5)) / x; end
NOTE: x should be positive before calling this function code[x_] := N[(N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
x = |x|\\
\\
\frac{\tan \left(x \cdot 0.5\right)}{x}
\end{array}
Initial program 49.9%
flip--49.7%
div-inv49.6%
metadata-eval49.6%
1-sub-cos76.5%
pow276.5%
Applied egg-rr76.5%
unpow276.5%
associate-*l*76.5%
associate-*r/76.5%
*-rgt-identity76.5%
hang-0p-tan76.9%
Simplified76.9%
*-commutative76.9%
times-frac99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
*-commutative99.8%
clear-num99.8%
frac-times99.8%
*-un-lft-identity99.8%
Applied egg-rr99.8%
Taylor expanded in x around 0 53.7%
Final simplification53.7%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 0.5)
x = abs(x);
double code(double x) {
return 0.5;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0
end function
x = Math.abs(x);
public static double code(double x) {
return 0.5;
}
x = abs(x) def code(x): return 0.5
x = abs(x) function code(x) return 0.5 end
x = abs(x) function tmp = code(x) tmp = 0.5; end
NOTE: x should be positive before calling this function code[x_] := 0.5
\begin{array}{l}
x = |x|\\
\\
0.5
\end{array}
Initial program 49.9%
Taylor expanded in x around 0 52.8%
Final simplification52.8%
herbie shell --seed 2023230
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))