math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 7.8s
Alternatives: 12
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))
double code(double re, double im) {
	return (0.5 * cos(re)) * (exp(-im) + exp(im));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (0.5d0 * cos(re)) * (exp(-im) + exp(im))
end function
public static double code(double re, double im) {
	return (0.5 * Math.cos(re)) * (Math.exp(-im) + Math.exp(im));
}
def code(re, im):
	return (0.5 * math.cos(re)) * (math.exp(-im) + math.exp(im))
function code(re, im)
	return Float64(Float64(0.5 * cos(re)) * Float64(exp(Float64(-im)) + exp(im)))
end
function tmp = code(re, im)
	tmp = (0.5 * cos(re)) * (exp(-im) + exp(im));
end
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 12 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))
double code(double re, double im) {
	return (0.5 * cos(re)) * (exp(-im) + exp(im));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (0.5d0 * cos(re)) * (exp(-im) + exp(im))
end function
public static double code(double re, double im) {
	return (0.5 * Math.cos(re)) * (Math.exp(-im) + Math.exp(im));
}
def code(re, im):
	return (0.5 * math.cos(re)) * (math.exp(-im) + math.exp(im))
function code(re, im)
	return Float64(Float64(0.5 * cos(re)) * Float64(exp(Float64(-im)) + exp(im)))
end
function tmp = code(re, im)
	tmp = (0.5 * cos(re)) * (exp(-im) + exp(im));
end
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))
double code(double re, double im) {
	return (0.5 * cos(re)) * (exp(-im) + exp(im));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (0.5d0 * cos(re)) * (exp(-im) + exp(im))
end function
public static double code(double re, double im) {
	return (0.5 * Math.cos(re)) * (Math.exp(-im) + Math.exp(im));
}
def code(re, im):
	return (0.5 * math.cos(re)) * (math.exp(-im) + math.exp(im))
function code(re, im)
	return Float64(Float64(0.5 * cos(re)) * Float64(exp(Float64(-im)) + exp(im)))
end
function tmp = code(re, im)
	tmp = (0.5 * cos(re)) * (exp(-im) + exp(im));
end
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 2: 86.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 415:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{elif}\;im \leq 5 \cdot 10^{+22}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left({im}^{6} \cdot 0.001388888888888889\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\cos re \cdot \sqrt{{im}^{12} \cdot 1.9290123456790124 \cdot 10^{-6}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 415.0)
   (* (* 0.5 (cos re)) (fma im im 2.0))
   (if (<= im 5e+22)
     (log1p (expm1 (* (pow im 6.0) 0.001388888888888889)))
     (* (cos re) (sqrt (* (pow im 12.0) 1.9290123456790124e-6))))))
double code(double re, double im) {
	double tmp;
	if (im <= 415.0) {
		tmp = (0.5 * cos(re)) * fma(im, im, 2.0);
	} else if (im <= 5e+22) {
		tmp = log1p(expm1((pow(im, 6.0) * 0.001388888888888889)));
	} else {
		tmp = cos(re) * sqrt((pow(im, 12.0) * 1.9290123456790124e-6));
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (im <= 415.0)
		tmp = Float64(Float64(0.5 * cos(re)) * fma(im, im, 2.0));
	elseif (im <= 5e+22)
		tmp = log1p(expm1(Float64((im ^ 6.0) * 0.001388888888888889)));
	else
		tmp = Float64(cos(re) * sqrt(Float64((im ^ 12.0) * 1.9290123456790124e-6)));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[im, 415.0], N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 5e+22], N[Log[1 + N[(Exp[N[(N[Power[im, 6.0], $MachinePrecision] * 0.001388888888888889), $MachinePrecision]] - 1), $MachinePrecision]], $MachinePrecision], N[(N[Cos[re], $MachinePrecision] * N[Sqrt[N[(N[Power[im, 12.0], $MachinePrecision] * 1.9290123456790124e-6), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 415:\\
\;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\

\mathbf{elif}\;im \leq 5 \cdot 10^{+22}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left({im}^{6} \cdot 0.001388888888888889\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\cos re \cdot \sqrt{{im}^{12} \cdot 1.9290123456790124 \cdot 10^{-6}}\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 3: 86.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \cos re\\ \mathbf{if}\;im \leq 415:\\ \;\;\;\;t_0 \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{elif}\;im \leq 1.5 \cdot 10^{+50}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left({im}^{6} \cdot 0.001388888888888889\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(2 + {im}^{6} \cdot 0.002777777777777778\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (cos re))))
   (if (<= im 415.0)
     (* t_0 (fma im im 2.0))
     (if (<= im 1.5e+50)
       (log1p (expm1 (* (pow im 6.0) 0.001388888888888889)))
       (* t_0 (+ 2.0 (* (pow im 6.0) 0.002777777777777778)))))))
double code(double re, double im) {
	double t_0 = 0.5 * cos(re);
	double tmp;
	if (im <= 415.0) {
		tmp = t_0 * fma(im, im, 2.0);
	} else if (im <= 1.5e+50) {
		tmp = log1p(expm1((pow(im, 6.0) * 0.001388888888888889)));
	} else {
		tmp = t_0 * (2.0 + (pow(im, 6.0) * 0.002777777777777778));
	}
	return tmp;
}
function code(re, im)
	t_0 = Float64(0.5 * cos(re))
	tmp = 0.0
	if (im <= 415.0)
		tmp = Float64(t_0 * fma(im, im, 2.0));
	elseif (im <= 1.5e+50)
		tmp = log1p(expm1(Float64((im ^ 6.0) * 0.001388888888888889)));
	else
		tmp = Float64(t_0 * Float64(2.0 + Float64((im ^ 6.0) * 0.002777777777777778)));
	end
	return tmp
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[im, 415.0], N[(t$95$0 * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.5e+50], N[Log[1 + N[(Exp[N[(N[Power[im, 6.0], $MachinePrecision] * 0.001388888888888889), $MachinePrecision]] - 1), $MachinePrecision]], $MachinePrecision], N[(t$95$0 * N[(2.0 + N[(N[Power[im, 6.0], $MachinePrecision] * 0.002777777777777778), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \cos re\\
\mathbf{if}\;im \leq 415:\\
\;\;\;\;t_0 \cdot \mathsf{fma}\left(im, im, 2\right)\\

\mathbf{elif}\;im \leq 1.5 \cdot 10^{+50}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left({im}^{6} \cdot 0.001388888888888889\right)\right)\\

\mathbf{else}:\\
\;\;\;\;t_0 \cdot \left(2 + {im}^{6} \cdot 0.002777777777777778\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 4: 71.9% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 2.8 \cdot 10^{+17}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.5 \cdot 10^{+50}:\\ \;\;\;\;\sqrt{{im}^{12} \cdot 1.9290123456790124 \cdot 10^{-6}}\\ \mathbf{else}:\\ \;\;\;\;0.001388888888888889 \cdot \left(\cos re \cdot {im}^{6}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 2.8e+17)
   (cos re)
   (if (<= im 1.5e+50)
     (sqrt (* (pow im 12.0) 1.9290123456790124e-6))
     (* 0.001388888888888889 (* (cos re) (pow im 6.0))))))
double code(double re, double im) {
	double tmp;
	if (im <= 2.8e+17) {
		tmp = cos(re);
	} else if (im <= 1.5e+50) {
		tmp = sqrt((pow(im, 12.0) * 1.9290123456790124e-6));
	} else {
		tmp = 0.001388888888888889 * (cos(re) * pow(im, 6.0));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 2.8d+17) then
        tmp = cos(re)
    else if (im <= 1.5d+50) then
        tmp = sqrt(((im ** 12.0d0) * 1.9290123456790124d-6))
    else
        tmp = 0.001388888888888889d0 * (cos(re) * (im ** 6.0d0))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 2.8e+17) {
		tmp = Math.cos(re);
	} else if (im <= 1.5e+50) {
		tmp = Math.sqrt((Math.pow(im, 12.0) * 1.9290123456790124e-6));
	} else {
		tmp = 0.001388888888888889 * (Math.cos(re) * Math.pow(im, 6.0));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 2.8e+17:
		tmp = math.cos(re)
	elif im <= 1.5e+50:
		tmp = math.sqrt((math.pow(im, 12.0) * 1.9290123456790124e-6))
	else:
		tmp = 0.001388888888888889 * (math.cos(re) * math.pow(im, 6.0))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 2.8e+17)
		tmp = cos(re);
	elseif (im <= 1.5e+50)
		tmp = sqrt(Float64((im ^ 12.0) * 1.9290123456790124e-6));
	else
		tmp = Float64(0.001388888888888889 * Float64(cos(re) * (im ^ 6.0)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 2.8e+17)
		tmp = cos(re);
	elseif (im <= 1.5e+50)
		tmp = sqrt(((im ^ 12.0) * 1.9290123456790124e-6));
	else
		tmp = 0.001388888888888889 * (cos(re) * (im ^ 6.0));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 2.8e+17], N[Cos[re], $MachinePrecision], If[LessEqual[im, 1.5e+50], N[Sqrt[N[(N[Power[im, 12.0], $MachinePrecision] * 1.9290123456790124e-6), $MachinePrecision]], $MachinePrecision], N[(0.001388888888888889 * N[(N[Cos[re], $MachinePrecision] * N[Power[im, 6.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 2.8 \cdot 10^{+17}:\\
\;\;\;\;\cos re\\

\mathbf{elif}\;im \leq 1.5 \cdot 10^{+50}:\\
\;\;\;\;\sqrt{{im}^{12} \cdot 1.9290123456790124 \cdot 10^{-6}}\\

\mathbf{else}:\\
\;\;\;\;0.001388888888888889 \cdot \left(\cos re \cdot {im}^{6}\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 5: 71.9% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 2.1 \cdot 10^{+17}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.5 \cdot 10^{+50}:\\ \;\;\;\;\sqrt{{im}^{12} \cdot 1.9290123456790124 \cdot 10^{-6}} + 1\\ \mathbf{else}:\\ \;\;\;\;0.001388888888888889 \cdot \left(\cos re \cdot {im}^{6}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 2.1e+17)
   (cos re)
   (if (<= im 1.5e+50)
     (+ (sqrt (* (pow im 12.0) 1.9290123456790124e-6)) 1.0)
     (* 0.001388888888888889 (* (cos re) (pow im 6.0))))))
double code(double re, double im) {
	double tmp;
	if (im <= 2.1e+17) {
		tmp = cos(re);
	} else if (im <= 1.5e+50) {
		tmp = sqrt((pow(im, 12.0) * 1.9290123456790124e-6)) + 1.0;
	} else {
		tmp = 0.001388888888888889 * (cos(re) * pow(im, 6.0));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 2.1d+17) then
        tmp = cos(re)
    else if (im <= 1.5d+50) then
        tmp = sqrt(((im ** 12.0d0) * 1.9290123456790124d-6)) + 1.0d0
    else
        tmp = 0.001388888888888889d0 * (cos(re) * (im ** 6.0d0))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 2.1e+17) {
		tmp = Math.cos(re);
	} else if (im <= 1.5e+50) {
		tmp = Math.sqrt((Math.pow(im, 12.0) * 1.9290123456790124e-6)) + 1.0;
	} else {
		tmp = 0.001388888888888889 * (Math.cos(re) * Math.pow(im, 6.0));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 2.1e+17:
		tmp = math.cos(re)
	elif im <= 1.5e+50:
		tmp = math.sqrt((math.pow(im, 12.0) * 1.9290123456790124e-6)) + 1.0
	else:
		tmp = 0.001388888888888889 * (math.cos(re) * math.pow(im, 6.0))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 2.1e+17)
		tmp = cos(re);
	elseif (im <= 1.5e+50)
		tmp = Float64(sqrt(Float64((im ^ 12.0) * 1.9290123456790124e-6)) + 1.0);
	else
		tmp = Float64(0.001388888888888889 * Float64(cos(re) * (im ^ 6.0)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 2.1e+17)
		tmp = cos(re);
	elseif (im <= 1.5e+50)
		tmp = sqrt(((im ^ 12.0) * 1.9290123456790124e-6)) + 1.0;
	else
		tmp = 0.001388888888888889 * (cos(re) * (im ^ 6.0));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 2.1e+17], N[Cos[re], $MachinePrecision], If[LessEqual[im, 1.5e+50], N[(N[Sqrt[N[(N[Power[im, 12.0], $MachinePrecision] * 1.9290123456790124e-6), $MachinePrecision]], $MachinePrecision] + 1.0), $MachinePrecision], N[(0.001388888888888889 * N[(N[Cos[re], $MachinePrecision] * N[Power[im, 6.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 2.1 \cdot 10^{+17}:\\
\;\;\;\;\cos re\\

\mathbf{elif}\;im \leq 1.5 \cdot 10^{+50}:\\
\;\;\;\;\sqrt{{im}^{12} \cdot 1.9290123456790124 \cdot 10^{-6}} + 1\\

\mathbf{else}:\\
\;\;\;\;0.001388888888888889 \cdot \left(\cos re \cdot {im}^{6}\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 6: 85.1% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 3.5 \cdot 10^{+17}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{elif}\;im \leq 1.5 \cdot 10^{+50}:\\ \;\;\;\;\sqrt{{im}^{12} \cdot 1.9290123456790124 \cdot 10^{-6}} + 1\\ \mathbf{else}:\\ \;\;\;\;0.001388888888888889 \cdot \left(\cos re \cdot {im}^{6}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 3.5e+17)
   (* (* 0.5 (cos re)) (fma im im 2.0))
   (if (<= im 1.5e+50)
     (+ (sqrt (* (pow im 12.0) 1.9290123456790124e-6)) 1.0)
     (* 0.001388888888888889 (* (cos re) (pow im 6.0))))))
double code(double re, double im) {
	double tmp;
	if (im <= 3.5e+17) {
		tmp = (0.5 * cos(re)) * fma(im, im, 2.0);
	} else if (im <= 1.5e+50) {
		tmp = sqrt((pow(im, 12.0) * 1.9290123456790124e-6)) + 1.0;
	} else {
		tmp = 0.001388888888888889 * (cos(re) * pow(im, 6.0));
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (im <= 3.5e+17)
		tmp = Float64(Float64(0.5 * cos(re)) * fma(im, im, 2.0));
	elseif (im <= 1.5e+50)
		tmp = Float64(sqrt(Float64((im ^ 12.0) * 1.9290123456790124e-6)) + 1.0);
	else
		tmp = Float64(0.001388888888888889 * Float64(cos(re) * (im ^ 6.0)));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[im, 3.5e+17], N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.5e+50], N[(N[Sqrt[N[(N[Power[im, 12.0], $MachinePrecision] * 1.9290123456790124e-6), $MachinePrecision]], $MachinePrecision] + 1.0), $MachinePrecision], N[(0.001388888888888889 * N[(N[Cos[re], $MachinePrecision] * N[Power[im, 6.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 3.5 \cdot 10^{+17}:\\
\;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\

\mathbf{elif}\;im \leq 1.5 \cdot 10^{+50}:\\
\;\;\;\;\sqrt{{im}^{12} \cdot 1.9290123456790124 \cdot 10^{-6}} + 1\\

\mathbf{else}:\\
\;\;\;\;0.001388888888888889 \cdot \left(\cos re \cdot {im}^{6}\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 7: 91.5% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \left(0.5 \cdot \cos re\right) \cdot \left(2 + {im}^{6} \cdot 0.002777777777777778\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (+ 2.0 (* (pow im 6.0) 0.002777777777777778))))
double code(double re, double im) {
	return (0.5 * cos(re)) * (2.0 + (pow(im, 6.0) * 0.002777777777777778));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (0.5d0 * cos(re)) * (2.0d0 + ((im ** 6.0d0) * 0.002777777777777778d0))
end function
public static double code(double re, double im) {
	return (0.5 * Math.cos(re)) * (2.0 + (Math.pow(im, 6.0) * 0.002777777777777778));
}
def code(re, im):
	return (0.5 * math.cos(re)) * (2.0 + (math.pow(im, 6.0) * 0.002777777777777778))
function code(re, im)
	return Float64(Float64(0.5 * cos(re)) * Float64(2.0 + Float64((im ^ 6.0) * 0.002777777777777778)))
end
function tmp = code(re, im)
	tmp = (0.5 * cos(re)) * (2.0 + ((im ^ 6.0) * 0.002777777777777778));
end
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(2.0 + N[(N[Power[im, 6.0], $MachinePrecision] * 0.002777777777777778), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(0.5 \cdot \cos re\right) \cdot \left(2 + {im}^{6} \cdot 0.002777777777777778\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 8: 66.5% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 9 \cdot 10^{+16}:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;\sqrt{{im}^{12} \cdot 1.9290123456790124 \cdot 10^{-6}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 9e+16) (cos re) (sqrt (* (pow im 12.0) 1.9290123456790124e-6))))
double code(double re, double im) {
	double tmp;
	if (im <= 9e+16) {
		tmp = cos(re);
	} else {
		tmp = sqrt((pow(im, 12.0) * 1.9290123456790124e-6));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 9d+16) then
        tmp = cos(re)
    else
        tmp = sqrt(((im ** 12.0d0) * 1.9290123456790124d-6))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 9e+16) {
		tmp = Math.cos(re);
	} else {
		tmp = Math.sqrt((Math.pow(im, 12.0) * 1.9290123456790124e-6));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 9e+16:
		tmp = math.cos(re)
	else:
		tmp = math.sqrt((math.pow(im, 12.0) * 1.9290123456790124e-6))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 9e+16)
		tmp = cos(re);
	else
		tmp = sqrt(Float64((im ^ 12.0) * 1.9290123456790124e-6));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 9e+16)
		tmp = cos(re);
	else
		tmp = sqrt(((im ^ 12.0) * 1.9290123456790124e-6));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 9e+16], N[Cos[re], $MachinePrecision], N[Sqrt[N[(N[Power[im, 12.0], $MachinePrecision] * 1.9290123456790124e-6), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 9 \cdot 10^{+16}:\\
\;\;\;\;\cos re\\

\mathbf{else}:\\
\;\;\;\;\sqrt{{im}^{12} \cdot 1.9290123456790124 \cdot 10^{-6}}\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 9: 65.4% accurate, 2.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 0.0076:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 3.85 \cdot 10^{+55}:\\ \;\;\;\;1 + {re}^{2} \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;{im}^{6} \cdot 0.001388888888888889 + 1\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 0.0076)
   (cos re)
   (if (<= im 3.85e+55)
     (+ 1.0 (* (pow re 2.0) -0.5))
     (+ (* (pow im 6.0) 0.001388888888888889) 1.0))))
double code(double re, double im) {
	double tmp;
	if (im <= 0.0076) {
		tmp = cos(re);
	} else if (im <= 3.85e+55) {
		tmp = 1.0 + (pow(re, 2.0) * -0.5);
	} else {
		tmp = (pow(im, 6.0) * 0.001388888888888889) + 1.0;
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 0.0076d0) then
        tmp = cos(re)
    else if (im <= 3.85d+55) then
        tmp = 1.0d0 + ((re ** 2.0d0) * (-0.5d0))
    else
        tmp = ((im ** 6.0d0) * 0.001388888888888889d0) + 1.0d0
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 0.0076) {
		tmp = Math.cos(re);
	} else if (im <= 3.85e+55) {
		tmp = 1.0 + (Math.pow(re, 2.0) * -0.5);
	} else {
		tmp = (Math.pow(im, 6.0) * 0.001388888888888889) + 1.0;
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 0.0076:
		tmp = math.cos(re)
	elif im <= 3.85e+55:
		tmp = 1.0 + (math.pow(re, 2.0) * -0.5)
	else:
		tmp = (math.pow(im, 6.0) * 0.001388888888888889) + 1.0
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 0.0076)
		tmp = cos(re);
	elseif (im <= 3.85e+55)
		tmp = Float64(1.0 + Float64((re ^ 2.0) * -0.5));
	else
		tmp = Float64(Float64((im ^ 6.0) * 0.001388888888888889) + 1.0);
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 0.0076)
		tmp = cos(re);
	elseif (im <= 3.85e+55)
		tmp = 1.0 + ((re ^ 2.0) * -0.5);
	else
		tmp = ((im ^ 6.0) * 0.001388888888888889) + 1.0;
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 0.0076], N[Cos[re], $MachinePrecision], If[LessEqual[im, 3.85e+55], N[(1.0 + N[(N[Power[re, 2.0], $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision], N[(N[(N[Power[im, 6.0], $MachinePrecision] * 0.001388888888888889), $MachinePrecision] + 1.0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 0.0076:\\
\;\;\;\;\cos re\\

\mathbf{elif}\;im \leq 3.85 \cdot 10^{+55}:\\
\;\;\;\;1 + {re}^{2} \cdot -0.5\\

\mathbf{else}:\\
\;\;\;\;{im}^{6} \cdot 0.001388888888888889 + 1\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 10: 65.2% accurate, 2.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 7.8 \cdot 10^{+16}:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;{im}^{6} \cdot 0.001388888888888889 + 1\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 7.8e+16) (cos re) (+ (* (pow im 6.0) 0.001388888888888889) 1.0)))
double code(double re, double im) {
	double tmp;
	if (im <= 7.8e+16) {
		tmp = cos(re);
	} else {
		tmp = (pow(im, 6.0) * 0.001388888888888889) + 1.0;
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 7.8d+16) then
        tmp = cos(re)
    else
        tmp = ((im ** 6.0d0) * 0.001388888888888889d0) + 1.0d0
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 7.8e+16) {
		tmp = Math.cos(re);
	} else {
		tmp = (Math.pow(im, 6.0) * 0.001388888888888889) + 1.0;
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 7.8e+16:
		tmp = math.cos(re)
	else:
		tmp = (math.pow(im, 6.0) * 0.001388888888888889) + 1.0
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 7.8e+16)
		tmp = cos(re);
	else
		tmp = Float64(Float64((im ^ 6.0) * 0.001388888888888889) + 1.0);
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 7.8e+16)
		tmp = cos(re);
	else
		tmp = ((im ^ 6.0) * 0.001388888888888889) + 1.0;
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 7.8e+16], N[Cos[re], $MachinePrecision], N[(N[(N[Power[im, 6.0], $MachinePrecision] * 0.001388888888888889), $MachinePrecision] + 1.0), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 7.8 \cdot 10^{+16}:\\
\;\;\;\;\cos re\\

\mathbf{else}:\\
\;\;\;\;{im}^{6} \cdot 0.001388888888888889 + 1\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 11: 65.2% accurate, 2.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 1.5 \cdot 10^{+17}:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;{im}^{6} \cdot 0.001388888888888889\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 1.5e+17) (cos re) (* (pow im 6.0) 0.001388888888888889)))
double code(double re, double im) {
	double tmp;
	if (im <= 1.5e+17) {
		tmp = cos(re);
	} else {
		tmp = pow(im, 6.0) * 0.001388888888888889;
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 1.5d+17) then
        tmp = cos(re)
    else
        tmp = (im ** 6.0d0) * 0.001388888888888889d0
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 1.5e+17) {
		tmp = Math.cos(re);
	} else {
		tmp = Math.pow(im, 6.0) * 0.001388888888888889;
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 1.5e+17:
		tmp = math.cos(re)
	else:
		tmp = math.pow(im, 6.0) * 0.001388888888888889
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 1.5e+17)
		tmp = cos(re);
	else
		tmp = Float64((im ^ 6.0) * 0.001388888888888889);
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 1.5e+17)
		tmp = cos(re);
	else
		tmp = (im ^ 6.0) * 0.001388888888888889;
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 1.5e+17], N[Cos[re], $MachinePrecision], N[(N[Power[im, 6.0], $MachinePrecision] * 0.001388888888888889), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 1.5 \cdot 10^{+17}:\\
\;\;\;\;\cos re\\

\mathbf{else}:\\
\;\;\;\;{im}^{6} \cdot 0.001388888888888889\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 12: 50.6% accurate, 3.0× speedup?

\[\begin{array}{l} \\ \cos re \end{array} \]
(FPCore (re im) :precision binary64 (cos re))
double code(double re, double im) {
	return cos(re);
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = cos(re)
end function
public static double code(double re, double im) {
	return Math.cos(re);
}
def code(re, im):
	return math.cos(re)
function code(re, im)
	return cos(re)
end
function tmp = code(re, im)
	tmp = cos(re);
end
code[re_, im_] := N[Cos[re], $MachinePrecision]
\begin{array}{l}

\\
\cos re
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Reproduce

?
herbie shell --seed 2024006 
(FPCore (re im)
  :name "math.cos on complex, real part"
  :precision binary64
  (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))