
(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) (/ (tan (/ x 2.0)) x)) x))
double code(double x) {
return (sin(x) * (tan((x / 2.0)) / x)) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (sin(x) * (tan((x / 2.0d0)) / x)) / x
end function
public static double code(double x) {
return (Math.sin(x) * (Math.tan((x / 2.0)) / x)) / x;
}
def code(x): return (math.sin(x) * (math.tan((x / 2.0)) / x)) / x
function code(x) return Float64(Float64(sin(x) * Float64(tan(Float64(x / 2.0)) / x)) / x) end
function tmp = code(x) tmp = (sin(x) * (tan((x / 2.0)) / x)) / x; end
code[x_] := N[(N[(N[Sin[x], $MachinePrecision] * N[(N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sin x \cdot \frac{\tan \left(\frac{x}{2}\right)}{x}}{x}
\end{array}
Initial program 48.1%
add-sqr-sqrt48.0%
pow248.0%
sqrt-div48.0%
sqrt-prod26.1%
add-sqr-sqrt49.5%
Applied egg-rr49.5%
unpow249.5%
frac-times48.0%
add-sqr-sqrt48.1%
flip--48.0%
metadata-eval48.0%
unpow248.0%
un-div-inv48.0%
associate-/r*49.5%
un-div-inv49.5%
metadata-eval49.5%
unpow249.5%
flip--49.6%
Applied egg-rr49.6%
flip--49.5%
metadata-eval49.5%
1-sub-cos75.5%
un-div-inv75.4%
associate-*l*75.5%
*-un-lft-identity75.5%
times-frac99.7%
un-div-inv99.7%
Applied egg-rr99.7%
/-rgt-identity99.7%
hang-0p-tan99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (<= x 0.0052) (+ 0.5 (* (pow x 2.0) -0.041666666666666664)) (/ (- 1.0 (cos x)) (* x x))))
double code(double x) {
double tmp;
if (x <= 0.0052) {
tmp = 0.5 + (pow(x, 2.0) * -0.041666666666666664);
} else {
tmp = (1.0 - cos(x)) / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0052d0) then
tmp = 0.5d0 + ((x ** 2.0d0) * (-0.041666666666666664d0))
else
tmp = (1.0d0 - cos(x)) / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.0052) {
tmp = 0.5 + (Math.pow(x, 2.0) * -0.041666666666666664);
} else {
tmp = (1.0 - Math.cos(x)) / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0052: tmp = 0.5 + (math.pow(x, 2.0) * -0.041666666666666664) else: tmp = (1.0 - math.cos(x)) / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 0.0052) tmp = Float64(0.5 + Float64((x ^ 2.0) * -0.041666666666666664)); else tmp = Float64(Float64(1.0 - cos(x)) / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0052) tmp = 0.5 + ((x ^ 2.0) * -0.041666666666666664); else tmp = (1.0 - cos(x)) / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0052], N[(0.5 + N[(N[Power[x, 2.0], $MachinePrecision] * -0.041666666666666664), $MachinePrecision]), $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.0052:\\
\;\;\;\;0.5 + {x}^{2} \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x \cdot x}\\
\end{array}
\end{array}
if x < 0.0051999999999999998Initial program 31.1%
Taylor expanded in x around 0 70.8%
*-commutative70.8%
Simplified70.8%
if 0.0051999999999999998 < x Initial program 98.9%
Final simplification77.8%
(FPCore (x) :precision binary64 (if (<= x 0.0052) (+ 0.5 (* (pow x 2.0) -0.041666666666666664)) (/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
double tmp;
if (x <= 0.0052) {
tmp = 0.5 + (pow(x, 2.0) * -0.041666666666666664);
} else {
tmp = ((1.0 - cos(x)) / x) / x;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0052d0) then
tmp = 0.5d0 + ((x ** 2.0d0) * (-0.041666666666666664d0))
else
tmp = ((1.0d0 - cos(x)) / x) / x
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.0052) {
tmp = 0.5 + (Math.pow(x, 2.0) * -0.041666666666666664);
} else {
tmp = ((1.0 - Math.cos(x)) / x) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0052: tmp = 0.5 + (math.pow(x, 2.0) * -0.041666666666666664) else: tmp = ((1.0 - math.cos(x)) / x) / x return tmp
function code(x) tmp = 0.0 if (x <= 0.0052) tmp = Float64(0.5 + Float64((x ^ 2.0) * -0.041666666666666664)); else tmp = Float64(Float64(Float64(1.0 - cos(x)) / x) / x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0052) tmp = 0.5 + ((x ^ 2.0) * -0.041666666666666664); else tmp = ((1.0 - cos(x)) / x) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0052], N[(0.5 + N[(N[Power[x, 2.0], $MachinePrecision] * -0.041666666666666664), $MachinePrecision]), $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.0052:\\
\;\;\;\;0.5 + {x}^{2} \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\
\end{array}
\end{array}
if x < 0.0051999999999999998Initial program 31.1%
Taylor expanded in x around 0 70.8%
*-commutative70.8%
Simplified70.8%
if 0.0051999999999999998 < x Initial program 98.9%
add-sqr-sqrt98.7%
pow298.7%
sqrt-div98.7%
sqrt-prod98.7%
add-sqr-sqrt98.9%
Applied egg-rr98.9%
unpow298.9%
frac-times98.9%
add-sqr-sqrt98.9%
flip--99.0%
metadata-eval99.0%
unpow299.0%
un-div-inv98.9%
associate-/r*99.0%
un-div-inv99.1%
metadata-eval99.1%
unpow299.1%
flip--99.1%
Applied egg-rr99.1%
Final simplification77.9%
(FPCore (x) :precision binary64 (if (<= x 9.5e+76) 0.5 (/ (+ (/ 1.0 x) (/ -1.0 x)) x)))
double code(double x) {
double tmp;
if (x <= 9.5e+76) {
tmp = 0.5;
} else {
tmp = ((1.0 / x) + (-1.0 / x)) / x;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 9.5d+76) then
tmp = 0.5d0
else
tmp = ((1.0d0 / x) + ((-1.0d0) / x)) / x
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 9.5e+76) {
tmp = 0.5;
} else {
tmp = ((1.0 / x) + (-1.0 / x)) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= 9.5e+76: tmp = 0.5 else: tmp = ((1.0 / x) + (-1.0 / x)) / x return tmp
function code(x) tmp = 0.0 if (x <= 9.5e+76) tmp = 0.5; else tmp = Float64(Float64(Float64(1.0 / x) + Float64(-1.0 / x)) / x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 9.5e+76) tmp = 0.5; else tmp = ((1.0 / x) + (-1.0 / x)) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 9.5e+76], 0.5, N[(N[(N[(1.0 / x), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 9.5 \cdot 10^{+76}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x} + \frac{-1}{x}}{x}\\
\end{array}
\end{array}
if x < 9.5000000000000003e76Initial program 36.8%
Taylor expanded in x around 0 65.6%
if 9.5000000000000003e76 < x Initial program 99.5%
add-sqr-sqrt99.4%
pow299.4%
sqrt-div99.5%
sqrt-prod99.5%
add-sqr-sqrt99.7%
Applied egg-rr99.7%
unpow299.7%
frac-times99.5%
add-sqr-sqrt99.5%
flip--99.3%
metadata-eval99.3%
unpow299.3%
un-div-inv99.3%
associate-/r*99.5%
un-div-inv99.5%
metadata-eval99.5%
unpow299.5%
flip--99.7%
Applied egg-rr99.7%
div-sub99.3%
Applied egg-rr99.3%
Taylor expanded in x around 0 73.5%
Final simplification67.0%
(FPCore (x) :precision binary64 (/ 1.0 (* x (+ (* x 0.16666666666666666) (* 2.0 (/ 1.0 x))))))
double code(double x) {
return 1.0 / (x * ((x * 0.16666666666666666) + (2.0 * (1.0 / x))));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (x * ((x * 0.16666666666666666d0) + (2.0d0 * (1.0d0 / x))))
end function
public static double code(double x) {
return 1.0 / (x * ((x * 0.16666666666666666) + (2.0 * (1.0 / x))));
}
def code(x): return 1.0 / (x * ((x * 0.16666666666666666) + (2.0 * (1.0 / x))))
function code(x) return Float64(1.0 / Float64(x * Float64(Float64(x * 0.16666666666666666) + Float64(2.0 * Float64(1.0 / x))))) end
function tmp = code(x) tmp = 1.0 / (x * ((x * 0.16666666666666666) + (2.0 * (1.0 / x)))); end
code[x_] := N[(1.0 / N[(x * N[(N[(x * 0.16666666666666666), $MachinePrecision] + N[(2.0 * N[(1.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x \cdot \left(x \cdot 0.16666666666666666 + 2 \cdot \frac{1}{x}\right)}
\end{array}
Initial program 48.1%
add-sqr-sqrt48.0%
pow248.0%
sqrt-div48.0%
sqrt-prod26.1%
add-sqr-sqrt49.5%
Applied egg-rr49.5%
unpow249.5%
frac-times48.0%
add-sqr-sqrt48.1%
pow248.1%
div-sub48.2%
pow-flip47.8%
metadata-eval47.8%
Applied egg-rr47.8%
metadata-eval47.8%
pow-flip48.2%
div-sub48.1%
pow248.1%
associate-/l/49.6%
div-inv49.5%
clear-num49.5%
frac-times48.8%
metadata-eval48.8%
Applied egg-rr48.8%
Taylor expanded in x around 0 81.4%
Final simplification81.4%
(FPCore (x) :precision binary64 0.5)
double code(double x) {
return 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0
end function
public static double code(double x) {
return 0.5;
}
def code(x): return 0.5
function code(x) return 0.5 end
function tmp = code(x) tmp = 0.5; end
code[x_] := 0.5
\begin{array}{l}
\\
0.5
\end{array}
Initial program 48.1%
Taylor expanded in x around 0 54.4%
Final simplification54.4%
herbie shell --seed 2024020
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))