
(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 10 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(Float64(tan(Float64(x * 0.5)) / x) * sin(x)) / x) end
function tmp = code(x) tmp = ((tan((x * 0.5)) / x) * sin(x)) / x; end
code[x_] := N[(N[(N[(N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\tan \left(x \cdot 0.5\right)}{x} \cdot \sin x}{x}
\end{array}
Initial program 49.4%
flip--49.1%
div-inv49.1%
metadata-eval49.1%
1-sub-cos74.0%
pow274.0%
Applied egg-rr74.0%
unpow274.0%
associate-*l*74.0%
associate-*r/74.0%
*-rgt-identity74.0%
hang-0p-tan74.3%
Simplified74.3%
*-commutative74.3%
times-frac99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
associate-*r/99.8%
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (x) :precision binary64 (* (tan (* x 0.5)) (/ (/ (sin x) x) x)))
double code(double x) {
return tan((x * 0.5)) * ((sin(x) / x) / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = tan((x * 0.5d0)) * ((sin(x) / x) / x)
end function
public static double code(double x) {
return Math.tan((x * 0.5)) * ((Math.sin(x) / x) / x);
}
def code(x): return math.tan((x * 0.5)) * ((math.sin(x) / x) / x)
function code(x) return Float64(tan(Float64(x * 0.5)) * Float64(Float64(sin(x) / x) / x)) end
function tmp = code(x) tmp = tan((x * 0.5)) * ((sin(x) / x) / x); end
code[x_] := N[(N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision] * N[(N[(N[Sin[x], $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\tan \left(x \cdot 0.5\right) \cdot \frac{\frac{\sin x}{x}}{x}
\end{array}
Initial program 49.4%
flip--49.1%
div-inv49.1%
metadata-eval49.1%
1-sub-cos74.0%
pow274.0%
Applied egg-rr74.0%
unpow274.0%
associate-*l*74.0%
associate-*r/74.0%
*-rgt-identity74.0%
hang-0p-tan74.3%
Simplified74.3%
*-commutative74.3%
times-frac99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
associate-*r/99.8%
Applied egg-rr99.8%
expm1-log1p-u99.8%
expm1-udef78.0%
associate-/l*78.0%
Applied egg-rr78.0%
expm1-def99.8%
expm1-log1p99.8%
associate-/l/99.0%
associate-/r*99.8%
associate-/l*75.4%
associate-*r/99.8%
*-rgt-identity99.8%
associate-*r/99.7%
associate-*l*99.7%
*-commutative99.7%
associate-*r/99.7%
*-rgt-identity99.7%
Simplified99.7%
Final simplification99.7%
(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 49.4%
flip--49.1%
div-inv49.1%
metadata-eval49.1%
1-sub-cos74.0%
pow274.0%
Applied egg-rr74.0%
unpow274.0%
associate-*l*74.0%
associate-*r/74.0%
*-rgt-identity74.0%
hang-0p-tan74.3%
Simplified74.3%
*-commutative74.3%
times-frac99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (x) :precision binary64 (/ (* (tan (* x 0.5)) (/ (sin x) x)) x))
double code(double x) {
return (tan((x * 0.5)) * (sin(x) / x)) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (tan((x * 0.5d0)) * (sin(x) / x)) / x
end function
public static double code(double x) {
return (Math.tan((x * 0.5)) * (Math.sin(x) / x)) / x;
}
def code(x): return (math.tan((x * 0.5)) * (math.sin(x) / x)) / x
function code(x) return Float64(Float64(tan(Float64(x * 0.5)) * Float64(sin(x) / x)) / x) end
function tmp = code(x) tmp = (tan((x * 0.5)) * (sin(x) / x)) / x; end
code[x_] := N[(N[(N[Tan[N[(x * 0.5), $MachinePrecision]], $MachinePrecision] * N[(N[Sin[x], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\tan \left(x \cdot 0.5\right) \cdot \frac{\sin x}{x}}{x}
\end{array}
Initial program 49.4%
flip--49.1%
div-inv49.1%
metadata-eval49.1%
1-sub-cos74.0%
pow274.0%
Applied egg-rr74.0%
unpow274.0%
associate-*l*74.0%
associate-*r/74.0%
*-rgt-identity74.0%
hang-0p-tan74.3%
Simplified74.3%
*-commutative74.3%
times-frac99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
associate-*l/99.8%
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (x) :precision binary64 (if (<= x 0.0052) (/ (+ (* x 0.5) (* -0.041666666666666664 (pow x 3.0))) x) (* (/ (- 1.0 (cos x)) x) (/ 1.0 x))))
double code(double x) {
double tmp;
if (x <= 0.0052) {
tmp = ((x * 0.5) + (-0.041666666666666664 * pow(x, 3.0))) / x;
} else {
tmp = ((1.0 - cos(x)) / x) * (1.0 / x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0052d0) then
tmp = ((x * 0.5d0) + ((-0.041666666666666664d0) * (x ** 3.0d0))) / x
else
tmp = ((1.0d0 - cos(x)) / x) * (1.0d0 / x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.0052) {
tmp = ((x * 0.5) + (-0.041666666666666664 * Math.pow(x, 3.0))) / x;
} else {
tmp = ((1.0 - Math.cos(x)) / x) * (1.0 / x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0052: tmp = ((x * 0.5) + (-0.041666666666666664 * math.pow(x, 3.0))) / x else: tmp = ((1.0 - math.cos(x)) / x) * (1.0 / x) return tmp
function code(x) tmp = 0.0 if (x <= 0.0052) tmp = Float64(Float64(Float64(x * 0.5) + Float64(-0.041666666666666664 * (x ^ 3.0))) / x); else tmp = Float64(Float64(Float64(1.0 - cos(x)) / x) * Float64(1.0 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0052) tmp = ((x * 0.5) + (-0.041666666666666664 * (x ^ 3.0))) / x; else tmp = ((1.0 - cos(x)) / x) * (1.0 / x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0052], N[(N[(N[(x * 0.5), $MachinePrecision] + N[(-0.041666666666666664 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision], N[(N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] * N[(1.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0052:\\
\;\;\;\;\frac{x \cdot 0.5 + -0.041666666666666664 \cdot {x}^{3}}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x} \cdot \frac{1}{x}\\
\end{array}
\end{array}
if x < 0.0051999999999999998Initial program 33.7%
flip--33.6%
div-inv33.6%
metadata-eval33.6%
1-sub-cos66.3%
pow266.3%
Applied egg-rr66.3%
unpow266.3%
associate-*l*66.3%
associate-*r/66.3%
*-rgt-identity66.3%
hang-0p-tan66.4%
Simplified66.4%
*-commutative66.4%
times-frac99.8%
div-inv99.8%
metadata-eval99.8%
Applied egg-rr99.8%
associate-*r/99.9%
Applied egg-rr99.9%
Taylor expanded in x around 0 67.2%
if 0.0051999999999999998 < x Initial program 99.4%
associate-/r*99.6%
div-inv99.6%
Applied egg-rr99.6%
Final simplification74.9%
(FPCore (x) :precision binary64 (if (<= x 0.0052) (+ 0.5 (* -0.041666666666666664 (* x x))) (* (/ (- 1.0 (cos x)) x) (/ 1.0 x))))
double code(double x) {
double tmp;
if (x <= 0.0052) {
tmp = 0.5 + (-0.041666666666666664 * (x * x));
} else {
tmp = ((1.0 - cos(x)) / x) * (1.0 / x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0052d0) then
tmp = 0.5d0 + ((-0.041666666666666664d0) * (x * x))
else
tmp = ((1.0d0 - cos(x)) / x) * (1.0d0 / x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.0052) {
tmp = 0.5 + (-0.041666666666666664 * (x * x));
} else {
tmp = ((1.0 - Math.cos(x)) / x) * (1.0 / x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0052: tmp = 0.5 + (-0.041666666666666664 * (x * x)) else: tmp = ((1.0 - math.cos(x)) / x) * (1.0 / x) return tmp
function code(x) tmp = 0.0 if (x <= 0.0052) tmp = Float64(0.5 + Float64(-0.041666666666666664 * Float64(x * x))); else tmp = Float64(Float64(Float64(1.0 - cos(x)) / x) * Float64(1.0 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0052) tmp = 0.5 + (-0.041666666666666664 * (x * x)); else tmp = ((1.0 - cos(x)) / x) * (1.0 / x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0052], N[(0.5 + N[(-0.041666666666666664 * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] * N[(1.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0052:\\
\;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x} \cdot \frac{1}{x}\\
\end{array}
\end{array}
if x < 0.0051999999999999998Initial program 33.7%
Taylor expanded in x around 0 67.2%
unpow267.2%
Simplified67.2%
if 0.0051999999999999998 < x Initial program 99.4%
associate-/r*99.6%
div-inv99.6%
Applied egg-rr99.6%
Final simplification74.9%
(FPCore (x) :precision binary64 (if (<= x 0.0052) (+ 0.5 (* -0.041666666666666664 (* x x))) (/ (- 1.0 (cos x)) (* x x))))
double code(double x) {
double tmp;
if (x <= 0.0052) {
tmp = 0.5 + (-0.041666666666666664 * (x * x));
} 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 + ((-0.041666666666666664d0) * (x * x))
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 + (-0.041666666666666664 * (x * x));
} else {
tmp = (1.0 - Math.cos(x)) / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0052: tmp = 0.5 + (-0.041666666666666664 * (x * x)) 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(-0.041666666666666664 * Float64(x * x))); 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 + (-0.041666666666666664 * (x * x)); else tmp = (1.0 - cos(x)) / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0052], N[(0.5 + N[(-0.041666666666666664 * N[(x * x), $MachinePrecision]), $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 + -0.041666666666666664 \cdot \left(x \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x \cdot x}\\
\end{array}
\end{array}
if x < 0.0051999999999999998Initial program 33.7%
Taylor expanded in x around 0 67.2%
unpow267.2%
Simplified67.2%
if 0.0051999999999999998 < x Initial program 99.4%
Final simplification74.9%
(FPCore (x) :precision binary64 (/ 1.0 (+ 2.0 (* (* x x) 0.16666666666666666))))
double code(double x) {
return 1.0 / (2.0 + ((x * x) * 0.16666666666666666));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (2.0d0 + ((x * x) * 0.16666666666666666d0))
end function
public static double code(double x) {
return 1.0 / (2.0 + ((x * x) * 0.16666666666666666));
}
def code(x): return 1.0 / (2.0 + ((x * x) * 0.16666666666666666))
function code(x) return Float64(1.0 / Float64(2.0 + Float64(Float64(x * x) * 0.16666666666666666))) end
function tmp = code(x) tmp = 1.0 / (2.0 + ((x * x) * 0.16666666666666666)); end
code[x_] := N[(1.0 / N[(2.0 + N[(N[(x * x), $MachinePrecision] * 0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2 + \left(x \cdot x\right) \cdot 0.16666666666666666}
\end{array}
Initial program 49.4%
clear-num49.4%
inv-pow49.4%
associate-/l*50.1%
Applied egg-rr50.1%
unpow-150.1%
associate-/r/50.1%
Applied egg-rr50.1%
Taylor expanded in x around 0 79.6%
*-commutative79.6%
unpow279.6%
Simplified79.6%
Final simplification79.6%
(FPCore (x) :precision binary64 (if (<= x 6.2e+76) 0.5 0.0))
double code(double x) {
double tmp;
if (x <= 6.2e+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 <= 6.2d+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 <= 6.2e+76) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 6.2e+76: tmp = 0.5 else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= 6.2e+76) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 6.2e+76) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 6.2e+76], 0.5, 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 6.2 \cdot 10^{+76}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 6.20000000000000023e76Initial program 36.9%
Taylor expanded in x around 0 64.8%
if 6.20000000000000023e76 < x Initial program 99.5%
div-sub99.5%
pow299.5%
pow-flip99.3%
metadata-eval99.3%
div-inv99.4%
pow299.4%
pow-flip99.5%
metadata-eval99.5%
Applied egg-rr99.5%
Taylor expanded in x around 0 67.2%
unpow267.2%
Simplified67.2%
Taylor expanded in x around 0 67.3%
Final simplification65.3%
(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 49.4%
div-sub49.4%
pow249.4%
pow-flip49.5%
metadata-eval49.5%
div-inv49.5%
pow249.5%
pow-flip50.2%
metadata-eval50.2%
Applied egg-rr50.2%
Taylor expanded in x around 0 27.3%
unpow227.3%
Simplified27.3%
Taylor expanded in x around 0 28.0%
Final simplification28.0%
herbie shell --seed 2023293
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))