math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 5.4s
Alternatives: 9
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 9 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} im = |im|\\ \\ \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right) \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (* (cos re) (fma 0.5 (exp im) (/ 0.5 (exp im)))))
im = abs(im);
double code(double re, double im) {
	return cos(re) * fma(0.5, exp(im), (0.5 / exp(im)));
}
im = abs(im)
function code(re, im)
	return Float64(cos(re) * fma(0.5, exp(im), Float64(0.5 / exp(im))))
end
NOTE: im should be positive before calling this function
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}
im = |im|\\
\\
\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Simplified100.0%

    \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
  3. Final simplification100.0%

    \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right) \]

Alternative 2: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} im = |im|\\ \\ \left(\cos re \cdot 0.5\right) \cdot \left(e^{im} + e^{-im}\right) \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (* (* (cos re) 0.5) (+ (exp im) (exp (- im)))))
im = abs(im);
double code(double re, double im) {
	return (cos(re) * 0.5) * (exp(im) + exp(-im));
}
NOTE: im should be positive before calling this function
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
im = Math.abs(im);
public static double code(double re, double im) {
	return (Math.cos(re) * 0.5) * (Math.exp(im) + Math.exp(-im));
}
im = abs(im)
def code(re, im):
	return (math.cos(re) * 0.5) * (math.exp(im) + math.exp(-im))
im = abs(im)
function code(re, im)
	return Float64(Float64(cos(re) * 0.5) * Float64(exp(im) + exp(Float64(-im))))
end
im = abs(im)
function tmp = code(re, im)
	tmp = (cos(re) * 0.5) * (exp(im) + exp(-im));
end
NOTE: im should be positive before calling this function
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}
im = |im|\\
\\
\left(\cos re \cdot 0.5\right) \cdot \left(e^{im} + e^{-im}\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Final simplification100.0%

    \[\leadsto \left(\cos re \cdot 0.5\right) \cdot \left(e^{im} + e^{-im}\right) \]

Alternative 3: 99.7% accurate, 1.5× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;im \leq 1.3:\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\cos re \cdot e^{im}\right)\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (<= im 1.3)
   (* (* (cos re) 0.5) (fma im im 2.0))
   (* 0.5 (* (cos re) (exp im)))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (im <= 1.3) {
		tmp = (cos(re) * 0.5) * fma(im, im, 2.0);
	} else {
		tmp = 0.5 * (cos(re) * exp(im));
	}
	return tmp;
}
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (im <= 1.3)
		tmp = Float64(Float64(cos(re) * 0.5) * fma(im, im, 2.0));
	else
		tmp = Float64(0.5 * Float64(cos(re) * exp(im)));
	end
	return tmp
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[im, 1.3], N[(N[(N[Cos[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[Cos[re], $MachinePrecision] * N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;im \leq 1.3:\\
\;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 1.30000000000000004

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Taylor expanded in im around 0 82.1%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\left(2 + {im}^{2}\right)} \]
    3. Step-by-step derivation
      1. +-commutative82.1%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\left({im}^{2} + 2\right)} \]
      2. unpow282.1%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-def82.1%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    4. Simplified82.1%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]

    if 1.30000000000000004 < im

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    3. Applied egg-rr99.1%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0}\right) \]
    4. Taylor expanded in re around inf 99.1%

      \[\leadsto \color{blue}{0.5 \cdot \left(\cos re \cdot e^{im}\right)} \]
    5. Step-by-step derivation
      1. *-commutative99.1%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{im} \cdot \cos re\right)} \]
    6. Simplified99.1%

      \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} \cdot \cos re\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification87.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1.3:\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\cos re \cdot e^{im}\right)\\ \end{array} \]

Alternative 4: 99.5% accurate, 1.5× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;im \leq 0.72:\\ \;\;\;\;\cos re + 0.5 \cdot {im}^{2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\cos re \cdot e^{im}\right)\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (<= im 0.72)
   (+ (cos re) (* 0.5 (pow im 2.0)))
   (* 0.5 (* (cos re) (exp im)))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (im <= 0.72) {
		tmp = cos(re) + (0.5 * pow(im, 2.0));
	} else {
		tmp = 0.5 * (cos(re) * exp(im));
	}
	return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 0.72d0) then
        tmp = cos(re) + (0.5d0 * (im ** 2.0d0))
    else
        tmp = 0.5d0 * (cos(re) * exp(im))
    end if
    code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if (im <= 0.72) {
		tmp = Math.cos(re) + (0.5 * Math.pow(im, 2.0));
	} else {
		tmp = 0.5 * (Math.cos(re) * Math.exp(im));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if im <= 0.72:
		tmp = math.cos(re) + (0.5 * math.pow(im, 2.0))
	else:
		tmp = 0.5 * (math.cos(re) * math.exp(im))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (im <= 0.72)
		tmp = Float64(cos(re) + Float64(0.5 * (im ^ 2.0)));
	else
		tmp = Float64(0.5 * Float64(cos(re) * exp(im)));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 0.72)
		tmp = cos(re) + (0.5 * (im ^ 2.0));
	else
		tmp = 0.5 * (cos(re) * exp(im));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[im, 0.72], N[(N[Cos[re], $MachinePrecision] + N[(0.5 * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[Cos[re], $MachinePrecision] * N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;im \leq 0.72:\\
\;\;\;\;\cos re + 0.5 \cdot {im}^{2}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 0.71999999999999997

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    3. Taylor expanded in im around 0 82.1%

      \[\leadsto \color{blue}{\cos re + 0.5 \cdot \left({im}^{2} \cdot \cos re\right)} \]
    4. Taylor expanded in re around 0 77.1%

      \[\leadsto \cos re + \color{blue}{0.5 \cdot {im}^{2}} \]
    5. Step-by-step derivation
      1. *-commutative77.1%

        \[\leadsto \cos re + \color{blue}{{im}^{2} \cdot 0.5} \]
    6. Simplified77.1%

      \[\leadsto \cos re + \color{blue}{{im}^{2} \cdot 0.5} \]

    if 0.71999999999999997 < im

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    3. Applied egg-rr99.1%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0}\right) \]
    4. Taylor expanded in re around inf 99.1%

      \[\leadsto \color{blue}{0.5 \cdot \left(\cos re \cdot e^{im}\right)} \]
    5. Step-by-step derivation
      1. *-commutative99.1%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{im} \cdot \cos re\right)} \]
    6. Simplified99.1%

      \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} \cdot \cos re\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification83.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 0.72:\\ \;\;\;\;\cos re + 0.5 \cdot {im}^{2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\cos re \cdot e^{im}\right)\\ \end{array} \]

Alternative 5: 99.3% accurate, 1.5× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 0.69999999999999996

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    3. Taylor expanded in im around 0 63.1%

      \[\leadsto \color{blue}{\cos re} \]

    if 0.69999999999999996 < im

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    3. Applied egg-rr99.1%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0}\right) \]
    4. Taylor expanded in re around inf 99.1%

      \[\leadsto \color{blue}{0.5 \cdot \left(\cos re \cdot e^{im}\right)} \]
    5. Step-by-step derivation
      1. *-commutative99.1%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{im} \cdot \cos re\right)} \]
    6. Simplified99.1%

      \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} \cdot \cos re\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification73.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 0.7:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\cos re \cdot e^{im}\right)\\ \end{array} \]

Alternative 6: 86.5% accurate, 2.9× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;im \leq 2.6:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot e^{im}\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im) :precision binary64 (if (<= im 2.6) (cos re) (* 0.5 (exp im))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (im <= 2.6) {
		tmp = cos(re);
	} else {
		tmp = 0.5 * exp(im);
	}
	return tmp;
}
NOTE: im should be positive before calling this function
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
        tmp = 0.5d0 * exp(im)
    end if
    code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if (im <= 2.6) {
		tmp = Math.cos(re);
	} else {
		tmp = 0.5 * Math.exp(im);
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if im <= 2.6:
		tmp = math.cos(re)
	else:
		tmp = 0.5 * math.exp(im)
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (im <= 2.6)
		tmp = cos(re);
	else
		tmp = Float64(0.5 * exp(im));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 2.6)
		tmp = cos(re);
	else
		tmp = 0.5 * exp(im);
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[im, 2.6], N[Cos[re], $MachinePrecision], N[(0.5 * N[Exp[im], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;im \leq 2.6:\\
\;\;\;\;\cos re\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 2.60000000000000009

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    3. Taylor expanded in im around 0 62.8%

      \[\leadsto \color{blue}{\cos re} \]

    if 2.60000000000000009 < im

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    3. Applied egg-rr100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0}\right) \]
    4. Taylor expanded in re around 0 82.4%

      \[\leadsto \color{blue}{0.5 \cdot e^{im}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification68.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 2.6:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot e^{im}\\ \end{array} \]

Alternative 7: 50.7% accurate, 3.0× speedup?

\[\begin{array}{l} im = |im|\\ \\ \cos re \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im) :precision binary64 (cos re))
im = abs(im);
double code(double re, double im) {
	return cos(re);
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = cos(re)
end function
im = Math.abs(im);
public static double code(double re, double im) {
	return Math.cos(re);
}
im = abs(im)
def code(re, im):
	return math.cos(re)
im = abs(im)
function code(re, im)
	return cos(re)
end
im = abs(im)
function tmp = code(re, im)
	tmp = cos(re);
end
NOTE: im should be positive before calling this function
code[re_, im_] := N[Cos[re], $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
\cos re
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Simplified100.0%

    \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
  3. Taylor expanded in im around 0 45.6%

    \[\leadsto \color{blue}{\cos re} \]
  4. Final simplification45.6%

    \[\leadsto \cos re \]

Alternative 8: 8.9% accurate, 308.0× speedup?

\[\begin{array}{l} im = |im|\\ \\ 1.5 \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im) :precision binary64 1.5)
im = abs(im);
double code(double re, double im) {
	return 1.5;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 1.5d0
end function
im = Math.abs(im);
public static double code(double re, double im) {
	return 1.5;
}
im = abs(im)
def code(re, im):
	return 1.5
im = abs(im)
function code(re, im)
	return 1.5
end
im = abs(im)
function tmp = code(re, im)
	tmp = 1.5;
end
NOTE: im should be positive before calling this function
code[re_, im_] := 1.5
\begin{array}{l}
im = |im|\\
\\
1.5
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Simplified100.0%

    \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
  3. Taylor expanded in im around 0 71.5%

    \[\leadsto \color{blue}{\cos re + 0.5 \cdot \left({im}^{2} \cdot \cos re\right)} \]
  4. Applied egg-rr9.2%

    \[\leadsto \cos re + 0.5 \cdot \color{blue}{1} \]
  5. Taylor expanded in re around 0 7.4%

    \[\leadsto \color{blue}{1.5} \]
  6. Final simplification7.4%

    \[\leadsto 1.5 \]

Alternative 9: 28.7% accurate, 308.0× speedup?

\[\begin{array}{l} im = |im|\\ \\ 1 \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im) :precision binary64 1.0)
im = abs(im);
double code(double re, double im) {
	return 1.0;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 1.0d0
end function
im = Math.abs(im);
public static double code(double re, double im) {
	return 1.0;
}
im = abs(im)
def code(re, im):
	return 1.0
im = abs(im)
function code(re, im)
	return 1.0
end
im = abs(im)
function tmp = code(re, im)
	tmp = 1.0;
end
NOTE: im should be positive before calling this function
code[re_, im_] := 1.0
\begin{array}{l}
im = |im|\\
\\
1
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Taylor expanded in re around 0 65.9%

    \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
  3. Taylor expanded in im around 0 23.5%

    \[\leadsto 0.5 \cdot \color{blue}{2} \]
  4. Final simplification23.5%

    \[\leadsto 1 \]

Reproduce

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