math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 6.5s
Alternatives: 13
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 13 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, 0.8× speedup?

\[\begin{array}{l} \\ \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (cos re) (fma 0.5 (exp im) (/ 0.5 (exp im)))))
double code(double re, double im) {
	return cos(re) * fma(0.5, exp(im), (0.5 / exp(im)));
}
function code(re, im)
	return Float64(cos(re) * fma(0.5, exp(im), Float64(0.5 / exp(im))))
end
code[re_, im_] := N[(N[Cos[re], $MachinePrecision] * N[(0.5 * N[Exp[im], $MachinePrecision] + N[(0.5 / N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Alternative 2: 100.0% accurate, 1.0× speedup?

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

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

Alternative 3: 73.8% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 0.00185:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.3 \cdot 10^{+103}:\\ \;\;\;\;0.5 \cdot \left(e^{im} + e^{-im}\right)\\ \mathbf{else}:\\ \;\;\;\;\cos re \cdot \left(1 + im \cdot \left(0.5 + im \cdot \left(0.25 + im \cdot 0.08333333333333333\right)\right)\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 0.00185)
   (cos re)
   (if (<= im 1.3e+103)
     (* 0.5 (+ (exp im) (exp (- im))))
     (*
      (cos re)
      (+ 1.0 (* im (+ 0.5 (* im (+ 0.25 (* im 0.08333333333333333))))))))))
double code(double re, double im) {
	double tmp;
	if (im <= 0.00185) {
		tmp = cos(re);
	} else if (im <= 1.3e+103) {
		tmp = 0.5 * (exp(im) + exp(-im));
	} else {
		tmp = cos(re) * (1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333))))));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 0.00185d0) then
        tmp = cos(re)
    else if (im <= 1.3d+103) then
        tmp = 0.5d0 * (exp(im) + exp(-im))
    else
        tmp = cos(re) * (1.0d0 + (im * (0.5d0 + (im * (0.25d0 + (im * 0.08333333333333333d0))))))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 0.00185) {
		tmp = Math.cos(re);
	} else if (im <= 1.3e+103) {
		tmp = 0.5 * (Math.exp(im) + Math.exp(-im));
	} else {
		tmp = Math.cos(re) * (1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333))))));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 0.00185:
		tmp = math.cos(re)
	elif im <= 1.3e+103:
		tmp = 0.5 * (math.exp(im) + math.exp(-im))
	else:
		tmp = math.cos(re) * (1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333))))))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 0.00185)
		tmp = cos(re);
	elseif (im <= 1.3e+103)
		tmp = Float64(0.5 * Float64(exp(im) + exp(Float64(-im))));
	else
		tmp = Float64(cos(re) * Float64(1.0 + Float64(im * Float64(0.5 + Float64(im * Float64(0.25 + Float64(im * 0.08333333333333333)))))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 0.00185)
		tmp = cos(re);
	elseif (im <= 1.3e+103)
		tmp = 0.5 * (exp(im) + exp(-im));
	else
		tmp = cos(re) * (1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333))))));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 0.00185], N[Cos[re], $MachinePrecision], If[LessEqual[im, 1.3e+103], N[(0.5 * N[(N[Exp[im], $MachinePrecision] + N[Exp[(-im)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[re], $MachinePrecision] * N[(1.0 + N[(im * N[(0.5 + N[(im * N[(0.25 + N[(im * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;im \leq 1.3 \cdot 10^{+103}:\\
\;\;\;\;0.5 \cdot \left(e^{im} + e^{-im}\right)\\

\mathbf{else}:\\
\;\;\;\;\cos re \cdot \left(1 + im \cdot \left(0.5 + im \cdot \left(0.25 + im \cdot 0.08333333333333333\right)\right)\right)\\


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

Alternative 4: 86.3% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 0.0068:\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{elif}\;im \leq 10^{+103}:\\ \;\;\;\;0.5 \cdot \left(e^{im} + e^{-im}\right)\\ \mathbf{else}:\\ \;\;\;\;\cos re \cdot \left(1 + im \cdot \left(0.5 + im \cdot \left(0.25 + im \cdot 0.08333333333333333\right)\right)\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 0.0068)
   (* (* (cos re) 0.5) (fma im im 2.0))
   (if (<= im 1e+103)
     (* 0.5 (+ (exp im) (exp (- im))))
     (*
      (cos re)
      (+ 1.0 (* im (+ 0.5 (* im (+ 0.25 (* im 0.08333333333333333))))))))))
double code(double re, double im) {
	double tmp;
	if (im <= 0.0068) {
		tmp = (cos(re) * 0.5) * fma(im, im, 2.0);
	} else if (im <= 1e+103) {
		tmp = 0.5 * (exp(im) + exp(-im));
	} else {
		tmp = cos(re) * (1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333))))));
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (im <= 0.0068)
		tmp = Float64(Float64(cos(re) * 0.5) * fma(im, im, 2.0));
	elseif (im <= 1e+103)
		tmp = Float64(0.5 * Float64(exp(im) + exp(Float64(-im))));
	else
		tmp = Float64(cos(re) * Float64(1.0 + Float64(im * Float64(0.5 + Float64(im * Float64(0.25 + Float64(im * 0.08333333333333333)))))));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[im, 0.0068], N[(N[(N[Cos[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1e+103], N[(0.5 * N[(N[Exp[im], $MachinePrecision] + N[Exp[(-im)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[re], $MachinePrecision] * N[(1.0 + N[(im * N[(0.5 + N[(im * N[(0.25 + N[(im * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;im \leq 10^{+103}:\\
\;\;\;\;0.5 \cdot \left(e^{im} + e^{-im}\right)\\

\mathbf{else}:\\
\;\;\;\;\cos re \cdot \left(1 + im \cdot \left(0.5 + im \cdot \left(0.25 + im \cdot 0.08333333333333333\right)\right)\right)\\


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

Alternative 5: 75.3% accurate, 1.5× speedup?

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

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

Alternative 6: 73.6% accurate, 2.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 2.3:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.3 \cdot 10^{+103}:\\ \;\;\;\;0.5 + 0.5 \cdot e^{im}\\ \mathbf{else}:\\ \;\;\;\;\cos re \cdot \left(1 + im \cdot \left(0.5 + im \cdot \left(0.25 + im \cdot 0.08333333333333333\right)\right)\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 2.3)
   (cos re)
   (if (<= im 1.3e+103)
     (+ 0.5 (* 0.5 (exp im)))
     (*
      (cos re)
      (+ 1.0 (* im (+ 0.5 (* im (+ 0.25 (* im 0.08333333333333333))))))))))
double code(double re, double im) {
	double tmp;
	if (im <= 2.3) {
		tmp = cos(re);
	} else if (im <= 1.3e+103) {
		tmp = 0.5 + (0.5 * exp(im));
	} else {
		tmp = cos(re) * (1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333))))));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 2.3d0) then
        tmp = cos(re)
    else if (im <= 1.3d+103) then
        tmp = 0.5d0 + (0.5d0 * exp(im))
    else
        tmp = cos(re) * (1.0d0 + (im * (0.5d0 + (im * (0.25d0 + (im * 0.08333333333333333d0))))))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 2.3) {
		tmp = Math.cos(re);
	} else if (im <= 1.3e+103) {
		tmp = 0.5 + (0.5 * Math.exp(im));
	} else {
		tmp = Math.cos(re) * (1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333))))));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 2.3:
		tmp = math.cos(re)
	elif im <= 1.3e+103:
		tmp = 0.5 + (0.5 * math.exp(im))
	else:
		tmp = math.cos(re) * (1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333))))))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 2.3)
		tmp = cos(re);
	elseif (im <= 1.3e+103)
		tmp = Float64(0.5 + Float64(0.5 * exp(im)));
	else
		tmp = Float64(cos(re) * Float64(1.0 + Float64(im * Float64(0.5 + Float64(im * Float64(0.25 + Float64(im * 0.08333333333333333)))))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 2.3)
		tmp = cos(re);
	elseif (im <= 1.3e+103)
		tmp = 0.5 + (0.5 * exp(im));
	else
		tmp = cos(re) * (1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333))))));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 2.3], N[Cos[re], $MachinePrecision], If[LessEqual[im, 1.3e+103], N[(0.5 + N[(0.5 * N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[re], $MachinePrecision] * N[(1.0 + N[(im * N[(0.5 + N[(im * N[(0.25 + N[(im * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;im \leq 1.3 \cdot 10^{+103}:\\
\;\;\;\;0.5 + 0.5 \cdot e^{im}\\

\mathbf{else}:\\
\;\;\;\;\cos re \cdot \left(1 + im \cdot \left(0.5 + im \cdot \left(0.25 + im \cdot 0.08333333333333333\right)\right)\right)\\


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

Alternative 7: 72.9% accurate, 2.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 2.6:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 2.8 \cdot 10^{+154}:\\ \;\;\;\;0.5 + 0.5 \cdot e^{im}\\ \mathbf{else}:\\ \;\;\;\;\cos re \cdot \left(1 + im \cdot \left(0.5 + im \cdot 0.25\right)\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 2.6)
   (cos re)
   (if (<= im 2.8e+154)
     (+ 0.5 (* 0.5 (exp im)))
     (* (cos re) (+ 1.0 (* im (+ 0.5 (* im 0.25))))))))
double code(double re, double im) {
	double tmp;
	if (im <= 2.6) {
		tmp = cos(re);
	} else if (im <= 2.8e+154) {
		tmp = 0.5 + (0.5 * exp(im));
	} else {
		tmp = cos(re) * (1.0 + (im * (0.5 + (im * 0.25))));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 2.6d0) then
        tmp = cos(re)
    else if (im <= 2.8d+154) then
        tmp = 0.5d0 + (0.5d0 * exp(im))
    else
        tmp = cos(re) * (1.0d0 + (im * (0.5d0 + (im * 0.25d0))))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 2.6) {
		tmp = Math.cos(re);
	} else if (im <= 2.8e+154) {
		tmp = 0.5 + (0.5 * Math.exp(im));
	} else {
		tmp = Math.cos(re) * (1.0 + (im * (0.5 + (im * 0.25))));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 2.6:
		tmp = math.cos(re)
	elif im <= 2.8e+154:
		tmp = 0.5 + (0.5 * math.exp(im))
	else:
		tmp = math.cos(re) * (1.0 + (im * (0.5 + (im * 0.25))))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 2.6)
		tmp = cos(re);
	elseif (im <= 2.8e+154)
		tmp = Float64(0.5 + Float64(0.5 * exp(im)));
	else
		tmp = Float64(cos(re) * Float64(1.0 + Float64(im * Float64(0.5 + Float64(im * 0.25)))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 2.6)
		tmp = cos(re);
	elseif (im <= 2.8e+154)
		tmp = 0.5 + (0.5 * exp(im));
	else
		tmp = cos(re) * (1.0 + (im * (0.5 + (im * 0.25))));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 2.6], N[Cos[re], $MachinePrecision], If[LessEqual[im, 2.8e+154], N[(0.5 + N[(0.5 * N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[re], $MachinePrecision] * N[(1.0 + N[(im * N[(0.5 + N[(im * 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;im \leq 2.8 \cdot 10^{+154}:\\
\;\;\;\;0.5 + 0.5 \cdot e^{im}\\

\mathbf{else}:\\
\;\;\;\;\cos re \cdot \left(1 + im \cdot \left(0.5 + im \cdot 0.25\right)\right)\\


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

Alternative 8: 69.8% accurate, 2.9× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;0.5 + 0.5 \cdot e^{im}\\


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

Alternative 9: 63.9% accurate, 3.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 15:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;1 + im \cdot \left(0.5 + im \cdot \left(0.25 + im \cdot 0.08333333333333333\right)\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 15.0)
   (cos re)
   (+ 1.0 (* im (+ 0.5 (* im (+ 0.25 (* im 0.08333333333333333))))))))
double code(double re, double im) {
	double tmp;
	if (im <= 15.0) {
		tmp = cos(re);
	} else {
		tmp = 1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333)))));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 15.0d0) then
        tmp = cos(re)
    else
        tmp = 1.0d0 + (im * (0.5d0 + (im * (0.25d0 + (im * 0.08333333333333333d0)))))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 15.0) {
		tmp = Math.cos(re);
	} else {
		tmp = 1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333)))));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 15.0:
		tmp = math.cos(re)
	else:
		tmp = 1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333)))))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 15.0)
		tmp = cos(re);
	else
		tmp = Float64(1.0 + Float64(im * Float64(0.5 + Float64(im * Float64(0.25 + Float64(im * 0.08333333333333333))))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 15.0)
		tmp = cos(re);
	else
		tmp = 1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333)))));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 15.0], N[Cos[re], $MachinePrecision], N[(1.0 + N[(im * N[(0.5 + N[(im * N[(0.25 + N[(im * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

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

\mathbf{else}:\\
\;\;\;\;1 + im \cdot \left(0.5 + im \cdot \left(0.25 + im \cdot 0.08333333333333333\right)\right)\\


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

Alternative 10: 44.5% accurate, 23.7× speedup?

\[\begin{array}{l} \\ 1 + im \cdot \left(0.5 + im \cdot \left(0.25 + im \cdot 0.08333333333333333\right)\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (+ 1.0 (* im (+ 0.5 (* im (+ 0.25 (* im 0.08333333333333333)))))))
double code(double re, double im) {
	return 1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333)))));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 1.0d0 + (im * (0.5d0 + (im * (0.25d0 + (im * 0.08333333333333333d0)))))
end function
public static double code(double re, double im) {
	return 1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333)))));
}
def code(re, im):
	return 1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333)))))
function code(re, im)
	return Float64(1.0 + Float64(im * Float64(0.5 + Float64(im * Float64(0.25 + Float64(im * 0.08333333333333333))))))
end
function tmp = code(re, im)
	tmp = 1.0 + (im * (0.5 + (im * (0.25 + (im * 0.08333333333333333)))));
end
code[re_, im_] := N[(1.0 + N[(im * N[(0.5 + N[(im * N[(0.25 + N[(im * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
1 + im \cdot \left(0.5 + im \cdot \left(0.25 + im \cdot 0.08333333333333333\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 11: 47.0% accurate, 34.2× speedup?

\[\begin{array}{l} \\ 1 + im \cdot \left(0.5 + im \cdot 0.25\right) \end{array} \]
(FPCore (re im) :precision binary64 (+ 1.0 (* im (+ 0.5 (* im 0.25)))))
double code(double re, double im) {
	return 1.0 + (im * (0.5 + (im * 0.25)));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 1.0d0 + (im * (0.5d0 + (im * 0.25d0)))
end function
public static double code(double re, double im) {
	return 1.0 + (im * (0.5 + (im * 0.25)));
}
def code(re, im):
	return 1.0 + (im * (0.5 + (im * 0.25)))
function code(re, im)
	return Float64(1.0 + Float64(im * Float64(0.5 + Float64(im * 0.25))))
end
function tmp = code(re, im)
	tmp = 1.0 + (im * (0.5 + (im * 0.25)));
end
code[re_, im_] := N[(1.0 + N[(im * N[(0.5 + N[(im * 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Alternative 12: 28.8% accurate, 61.6× speedup?

\[\begin{array}{l} \\ 1 + 0.5 \cdot im \end{array} \]
(FPCore (re im) :precision binary64 (+ 1.0 (* 0.5 im)))
double code(double re, double im) {
	return 1.0 + (0.5 * im);
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 1.0d0 + (0.5d0 * im)
end function
public static double code(double re, double im) {
	return 1.0 + (0.5 * im);
}
def code(re, im):
	return 1.0 + (0.5 * im)
function code(re, im)
	return Float64(1.0 + Float64(0.5 * im))
end
function tmp = code(re, im)
	tmp = 1.0 + (0.5 * im);
end
code[re_, im_] := N[(1.0 + N[(0.5 * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Alternative 13: 28.9% accurate, 308.0× speedup?

\[\begin{array}{l} \\ 1 \end{array} \]
(FPCore (re im) :precision binary64 1.0)
double code(double re, double im) {
	return 1.0;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 1.0d0
end function
public static double code(double re, double im) {
	return 1.0;
}
def code(re, im):
	return 1.0
function code(re, im)
	return 1.0
end
function tmp = code(re, im)
	tmp = 1.0;
end
code[re_, im_] := 1.0
\begin{array}{l}

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

Reproduce

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