
(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 (/ (/ (sin x) x) (/ x (tan (/ x 2.0)))))
double code(double x) {
return (sin(x) / x) / (x / tan((x / 2.0)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (sin(x) / x) / (x / tan((x / 2.0d0)))
end function
public static double code(double x) {
return (Math.sin(x) / x) / (x / Math.tan((x / 2.0)));
}
def code(x): return (math.sin(x) / x) / (x / math.tan((x / 2.0)))
function code(x) return Float64(Float64(sin(x) / x) / Float64(x / tan(Float64(x / 2.0)))) end
function tmp = code(x) tmp = (sin(x) / x) / (x / tan((x / 2.0))); end
code[x_] := N[(N[(N[Sin[x], $MachinePrecision] / x), $MachinePrecision] / N[(x / N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\sin x}{x}}{\frac{x}{\tan \left(\frac{x}{2}\right)}}
\end{array}
Initial program 47.8%
flip--N/A
metadata-evalN/A
1-sub-cosN/A
associate-/l*N/A
*-lowering-*.f64N/A
sin-lowering-sin.f64N/A
hang-0p-tanN/A
tan-lowering-tan.f64N/A
/-lowering-/.f6475.1%
Applied egg-rr75.1%
times-fracN/A
clear-numN/A
un-div-invN/A
/-lowering-/.f64N/A
/-lowering-/.f64N/A
sin-lowering-sin.f64N/A
/-lowering-/.f64N/A
tan-lowering-tan.f64N/A
/-lowering-/.f6499.8%
Applied egg-rr99.8%
(FPCore (x) :precision binary64 (/ (* (/ (sin x) x) (tan (/ x 2.0))) x))
double code(double x) {
return ((sin(x) / x) * tan((x / 2.0))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((sin(x) / x) * tan((x / 2.0d0))) / x
end function
public static double code(double x) {
return ((Math.sin(x) / x) * Math.tan((x / 2.0))) / x;
}
def code(x): return ((math.sin(x) / x) * math.tan((x / 2.0))) / x
function code(x) return Float64(Float64(Float64(sin(x) / x) * tan(Float64(x / 2.0))) / x) end
function tmp = code(x) tmp = ((sin(x) / x) * tan((x / 2.0))) / x; end
code[x_] := N[(N[(N[(N[Sin[x], $MachinePrecision] / x), $MachinePrecision] * N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\sin x}{x} \cdot \tan \left(\frac{x}{2}\right)}{x}
\end{array}
Initial program 47.8%
flip--N/A
metadata-evalN/A
1-sub-cosN/A
associate-/l*N/A
*-lowering-*.f64N/A
sin-lowering-sin.f64N/A
hang-0p-tanN/A
tan-lowering-tan.f64N/A
/-lowering-/.f6475.1%
Applied egg-rr75.1%
times-fracN/A
associate-*r/N/A
/-lowering-/.f64N/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
sin-lowering-sin.f64N/A
tan-lowering-tan.f64N/A
/-lowering-/.f6499.8%
Applied egg-rr99.8%
(FPCore (x)
:precision binary64
(if (<= x 0.032)
(+
0.5
(* (* x x) (+ -0.041666666666666664 (* (* x x) 0.001388888888888889))))
(/ (/ (- 1.0 (cos x)) x) x)))
double code(double x) {
double tmp;
if (x <= 0.032) {
tmp = 0.5 + ((x * x) * (-0.041666666666666664 + ((x * x) * 0.001388888888888889)));
} 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.032d0) then
tmp = 0.5d0 + ((x * x) * ((-0.041666666666666664d0) + ((x * x) * 0.001388888888888889d0)))
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.032) {
tmp = 0.5 + ((x * x) * (-0.041666666666666664 + ((x * x) * 0.001388888888888889)));
} else {
tmp = ((1.0 - Math.cos(x)) / x) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.032: tmp = 0.5 + ((x * x) * (-0.041666666666666664 + ((x * x) * 0.001388888888888889))) else: tmp = ((1.0 - math.cos(x)) / x) / x return tmp
function code(x) tmp = 0.0 if (x <= 0.032) tmp = Float64(0.5 + Float64(Float64(x * x) * Float64(-0.041666666666666664 + Float64(Float64(x * x) * 0.001388888888888889)))); 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.032) tmp = 0.5 + ((x * x) * (-0.041666666666666664 + ((x * x) * 0.001388888888888889))); else tmp = ((1.0 - cos(x)) / x) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.032], N[(0.5 + N[(N[(x * x), $MachinePrecision] * N[(-0.041666666666666664 + N[(N[(x * x), $MachinePrecision] * 0.001388888888888889), $MachinePrecision]), $MachinePrecision]), $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.032:\\
\;\;\;\;0.5 + \left(x \cdot x\right) \cdot \left(-0.041666666666666664 + \left(x \cdot x\right) \cdot 0.001388888888888889\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x}{x}}{x}\\
\end{array}
\end{array}
if x < 0.032000000000000001Initial program 34.1%
Taylor expanded in x around 0
+-lowering-+.f64N/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f64N/A
sub-negN/A
metadata-evalN/A
+-commutativeN/A
+-lowering-+.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6468.5%
Simplified68.5%
if 0.032000000000000001 < x Initial program 99.2%
Applied egg-rr99.2%
Taylor expanded in x around inf
associate-*r/N/A
mul-1-negN/A
neg-sub0N/A
associate-+l-N/A
neg-sub0N/A
+-commutativeN/A
sub-negN/A
unpow2N/A
associate-/r*N/A
/-lowering-/.f64N/A
/-lowering-/.f64N/A
--lowering--.f64N/A
cos-lowering-cos.f6499.4%
Simplified99.4%
(FPCore (x)
:precision binary64
(if (<= x 0.032)
(+
0.5
(* (* x x) (+ -0.041666666666666664 (* (* x x) 0.001388888888888889))))
(/ (- 1.0 (cos x)) (* x x))))
double code(double x) {
double tmp;
if (x <= 0.032) {
tmp = 0.5 + ((x * x) * (-0.041666666666666664 + ((x * x) * 0.001388888888888889)));
} 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.032d0) then
tmp = 0.5d0 + ((x * x) * ((-0.041666666666666664d0) + ((x * x) * 0.001388888888888889d0)))
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.032) {
tmp = 0.5 + ((x * x) * (-0.041666666666666664 + ((x * x) * 0.001388888888888889)));
} else {
tmp = (1.0 - Math.cos(x)) / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.032: tmp = 0.5 + ((x * x) * (-0.041666666666666664 + ((x * x) * 0.001388888888888889))) else: tmp = (1.0 - math.cos(x)) / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 0.032) tmp = Float64(0.5 + Float64(Float64(x * x) * Float64(-0.041666666666666664 + Float64(Float64(x * x) * 0.001388888888888889)))); 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.032) tmp = 0.5 + ((x * x) * (-0.041666666666666664 + ((x * x) * 0.001388888888888889))); else tmp = (1.0 - cos(x)) / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.032], N[(0.5 + N[(N[(x * x), $MachinePrecision] * N[(-0.041666666666666664 + N[(N[(x * x), $MachinePrecision] * 0.001388888888888889), $MachinePrecision]), $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.032:\\
\;\;\;\;0.5 + \left(x \cdot x\right) \cdot \left(-0.041666666666666664 + \left(x \cdot x\right) \cdot 0.001388888888888889\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1 - \cos x}{x \cdot x}\\
\end{array}
\end{array}
if x < 0.032000000000000001Initial program 34.1%
Taylor expanded in x around 0
+-lowering-+.f64N/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f64N/A
sub-negN/A
metadata-evalN/A
+-commutativeN/A
+-lowering-+.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6468.5%
Simplified68.5%
if 0.032000000000000001 < x Initial program 99.2%
(FPCore (x)
:precision binary64
(if (<= x 2.7)
(+
0.5
(* (* x x) (+ -0.041666666666666664 (* (* x x) 0.001388888888888889))))
(/ 2.0 (* x x))))
double code(double x) {
double tmp;
if (x <= 2.7) {
tmp = 0.5 + ((x * x) * (-0.041666666666666664 + ((x * x) * 0.001388888888888889)));
} else {
tmp = 2.0 / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 2.7d0) then
tmp = 0.5d0 + ((x * x) * ((-0.041666666666666664d0) + ((x * x) * 0.001388888888888889d0)))
else
tmp = 2.0d0 / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 2.7) {
tmp = 0.5 + ((x * x) * (-0.041666666666666664 + ((x * x) * 0.001388888888888889)));
} else {
tmp = 2.0 / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.7: tmp = 0.5 + ((x * x) * (-0.041666666666666664 + ((x * x) * 0.001388888888888889))) else: tmp = 2.0 / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 2.7) tmp = Float64(0.5 + Float64(Float64(x * x) * Float64(-0.041666666666666664 + Float64(Float64(x * x) * 0.001388888888888889)))); else tmp = Float64(2.0 / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2.7) tmp = 0.5 + ((x * x) * (-0.041666666666666664 + ((x * x) * 0.001388888888888889))); else tmp = 2.0 / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2.7], N[(0.5 + N[(N[(x * x), $MachinePrecision] * N[(-0.041666666666666664 + N[(N[(x * x), $MachinePrecision] * 0.001388888888888889), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.7:\\
\;\;\;\;0.5 + \left(x \cdot x\right) \cdot \left(-0.041666666666666664 + \left(x \cdot x\right) \cdot 0.001388888888888889\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{x \cdot x}\\
\end{array}
\end{array}
if x < 2.7000000000000002Initial program 34.1%
Taylor expanded in x around 0
+-lowering-+.f64N/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f64N/A
sub-negN/A
metadata-evalN/A
+-commutativeN/A
+-lowering-+.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6468.5%
Simplified68.5%
if 2.7000000000000002 < x Initial program 99.2%
Applied egg-rr55.0%
Taylor expanded in x around 0
/-lowering-/.f64N/A
unpow2N/A
*-lowering-*.f6457.5%
Simplified57.5%
(FPCore (x) :precision binary64 (if (<= x 2.4) (+ 0.5 (* (* x x) -0.041666666666666664)) (/ 2.0 (* x x))))
double code(double x) {
double tmp;
if (x <= 2.4) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = 2.0 / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 2.4d0) then
tmp = 0.5d0 + ((x * x) * (-0.041666666666666664d0))
else
tmp = 2.0d0 / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 2.4) {
tmp = 0.5 + ((x * x) * -0.041666666666666664);
} else {
tmp = 2.0 / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.4: tmp = 0.5 + ((x * x) * -0.041666666666666664) else: tmp = 2.0 / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 2.4) tmp = Float64(0.5 + Float64(Float64(x * x) * -0.041666666666666664)); else tmp = Float64(2.0 / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2.4) tmp = 0.5 + ((x * x) * -0.041666666666666664); else tmp = 2.0 / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2.4], N[(0.5 + N[(N[(x * x), $MachinePrecision] * -0.041666666666666664), $MachinePrecision]), $MachinePrecision], N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.4:\\
\;\;\;\;0.5 + \left(x \cdot x\right) \cdot -0.041666666666666664\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{x \cdot x}\\
\end{array}
\end{array}
if x < 2.39999999999999991Initial program 34.1%
Taylor expanded in x around 0
+-lowering-+.f64N/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6468.3%
Simplified68.3%
if 2.39999999999999991 < x Initial program 99.2%
Applied egg-rr55.0%
Taylor expanded in x around 0
/-lowering-/.f64N/A
unpow2N/A
*-lowering-*.f6457.5%
Simplified57.5%
Final simplification66.0%
(FPCore (x) :precision binary64 (if (<= x 2.0) 0.5 (/ 2.0 (* x x))))
double code(double x) {
double tmp;
if (x <= 2.0) {
tmp = 0.5;
} else {
tmp = 2.0 / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 2.0d0) then
tmp = 0.5d0
else
tmp = 2.0d0 / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 2.0) {
tmp = 0.5;
} else {
tmp = 2.0 / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.0: tmp = 0.5 else: tmp = 2.0 / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 2.0) tmp = 0.5; else tmp = Float64(2.0 / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2.0) tmp = 0.5; else tmp = 2.0 / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2.0], 0.5, N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{x \cdot x}\\
\end{array}
\end{array}
if x < 2Initial program 34.1%
Taylor expanded in x around 0
Simplified68.6%
if 2 < x Initial program 99.2%
Applied egg-rr55.0%
Taylor expanded in x around 0
/-lowering-/.f64N/A
unpow2N/A
*-lowering-*.f6457.5%
Simplified57.5%
(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 47.8%
Taylor expanded in x around 0
Simplified55.1%
herbie shell --seed 2024155
(FPCore (x)
:name "cos2 (problem 3.4.1)"
:precision binary64
(/ (- 1.0 (cos x)) (* x x)))