math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 6.2s
Alternatives: 14
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 14 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
  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} \\ \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
  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: 87.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \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}:\\ \;\;\;\;\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, 0\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 1.3)
   (* (* (cos re) 0.5) (fma im im 2.0))
   (* (cos re) (fma 0.5 (exp im) 0.0))))
double code(double re, double im) {
	double tmp;
	if (im <= 1.3) {
		tmp = (cos(re) * 0.5) * fma(im, im, 2.0);
	} else {
		tmp = cos(re) * fma(0.5, exp(im), 0.0);
	}
	return tmp;
}
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(cos(re) * fma(0.5, exp(im), 0.0));
	end
	return tmp
end
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[(N[Cos[re], $MachinePrecision] * N[(0.5 * N[Exp[im], $MachinePrecision] + 0.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\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}:\\
\;\;\;\;\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, 0\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 78.0%

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

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

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

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

      \[\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.9%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0}\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification84.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}:\\ \;\;\;\;\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, 0\right)\\ \end{array} \]

Alternative 4: 73.1% accurate, 1.5× speedup?

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

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

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

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


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

    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.6%

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

    if 4e3 < im < 1.14999999999999997e77

    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 75.0%

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

    if 1.14999999999999997e77 < 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. Taylor expanded in im around 0 100.0%

      \[\leadsto \color{blue}{\cos re + \left(0.001388888888888889 \cdot \left({im}^{6} \cdot \cos re\right) + \left(0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right) + 0.5 \cdot \left({im}^{2} \cdot \cos re\right)\right)\right)} \]
    4. Step-by-step derivation
      1. +-commutative100.0%

        \[\leadsto \color{blue}{\left(0.001388888888888889 \cdot \left({im}^{6} \cdot \cos re\right) + \left(0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right) + 0.5 \cdot \left({im}^{2} \cdot \cos re\right)\right)\right) + \cos re} \]
      2. associate-+r+100.0%

        \[\leadsto \color{blue}{\left(\left(0.001388888888888889 \cdot \left({im}^{6} \cdot \cos re\right) + 0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)\right) + 0.5 \cdot \left({im}^{2} \cdot \cos re\right)\right)} + \cos re \]
      3. associate-+l+100.0%

        \[\leadsto \color{blue}{\left(0.001388888888888889 \cdot \left({im}^{6} \cdot \cos re\right) + 0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)\right) + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right)} \]
      4. associate-*r*100.0%

        \[\leadsto \left(\color{blue}{\left(0.001388888888888889 \cdot {im}^{6}\right) \cdot \cos re} + 0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)\right) + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right) \]
      5. associate-*r*100.0%

        \[\leadsto \left(\left(0.001388888888888889 \cdot {im}^{6}\right) \cdot \cos re + \color{blue}{\left(0.041666666666666664 \cdot {im}^{4}\right) \cdot \cos re}\right) + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right) \]
      6. distribute-rgt-out100.0%

        \[\leadsto \color{blue}{\cos re \cdot \left(0.001388888888888889 \cdot {im}^{6} + 0.041666666666666664 \cdot {im}^{4}\right)} + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right) \]
      7. fma-def100.0%

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right)} + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right) \]
      8. associate-*r*100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right) + \left(\color{blue}{\left(0.5 \cdot {im}^{2}\right) \cdot \cos re} + \cos re\right) \]
      9. distribute-lft1-in100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right) + \color{blue}{\left(0.5 \cdot {im}^{2} + 1\right) \cdot \cos re} \]
      10. fma-def100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right) + \color{blue}{\mathsf{fma}\left(0.5, {im}^{2}, 1\right)} \cdot \cos re \]
    5. Simplified100.0%

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right) + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re} \]
    6. Taylor expanded in im around 0 100.0%

      \[\leadsto \color{blue}{0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)} + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re \]
    7. Step-by-step derivation
      1. associate-*r*100.0%

        \[\leadsto \color{blue}{\left(0.041666666666666664 \cdot {im}^{4}\right) \cdot \cos re} + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re \]
      2. *-commutative100.0%

        \[\leadsto \color{blue}{\cos re \cdot \left(0.041666666666666664 \cdot {im}^{4}\right)} + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re \]
    8. Simplified100.0%

      \[\leadsto \color{blue}{\cos re \cdot \left(0.041666666666666664 \cdot {im}^{4}\right)} + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re \]
    9. Taylor expanded in re around inf 100.0%

      \[\leadsto \cos re \cdot \left(0.041666666666666664 \cdot {im}^{4}\right) + \color{blue}{\cos re \cdot \left(1 + 0.5 \cdot {im}^{2}\right)} \]
    10. Taylor expanded in im around inf 100.0%

      \[\leadsto \color{blue}{0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification70.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 4000:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.15 \cdot 10^{+77}:\\ \;\;\;\;0.5 \cdot \left(e^{im} + e^{-im}\right)\\ \mathbf{else}:\\ \;\;\;\;0.041666666666666664 \cdot \left(\cos re \cdot {im}^{4}\right)\\ \end{array} \]

Alternative 5: 85.9% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 4000:\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{elif}\;im \leq 1.15 \cdot 10^{+77}:\\ \;\;\;\;0.5 \cdot \left(e^{im} + e^{-im}\right)\\ \mathbf{else}:\\ \;\;\;\;0.041666666666666664 \cdot \left(\cos re \cdot {im}^{4}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 4000.0)
   (* (* (cos re) 0.5) (fma im im 2.0))
   (if (<= im 1.15e+77)
     (* 0.5 (+ (exp im) (exp (- im))))
     (* 0.041666666666666664 (* (cos re) (pow im 4.0))))))
double code(double re, double im) {
	double tmp;
	if (im <= 4000.0) {
		tmp = (cos(re) * 0.5) * fma(im, im, 2.0);
	} else if (im <= 1.15e+77) {
		tmp = 0.5 * (exp(im) + exp(-im));
	} else {
		tmp = 0.041666666666666664 * (cos(re) * pow(im, 4.0));
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (im <= 4000.0)
		tmp = Float64(Float64(cos(re) * 0.5) * fma(im, im, 2.0));
	elseif (im <= 1.15e+77)
		tmp = Float64(0.5 * Float64(exp(im) + exp(Float64(-im))));
	else
		tmp = Float64(0.041666666666666664 * Float64(cos(re) * (im ^ 4.0)));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[im, 4000.0], N[(N[(N[Cos[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.15e+77], N[(0.5 * N[(N[Exp[im], $MachinePrecision] + N[Exp[(-im)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.041666666666666664 * N[(N[Cos[re], $MachinePrecision] * N[Power[im, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

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

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


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

    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 76.8%

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

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

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

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

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

    if 4e3 < im < 1.14999999999999997e77

    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 75.0%

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

    if 1.14999999999999997e77 < 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. Taylor expanded in im around 0 100.0%

      \[\leadsto \color{blue}{\cos re + \left(0.001388888888888889 \cdot \left({im}^{6} \cdot \cos re\right) + \left(0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right) + 0.5 \cdot \left({im}^{2} \cdot \cos re\right)\right)\right)} \]
    4. Step-by-step derivation
      1. +-commutative100.0%

        \[\leadsto \color{blue}{\left(0.001388888888888889 \cdot \left({im}^{6} \cdot \cos re\right) + \left(0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right) + 0.5 \cdot \left({im}^{2} \cdot \cos re\right)\right)\right) + \cos re} \]
      2. associate-+r+100.0%

        \[\leadsto \color{blue}{\left(\left(0.001388888888888889 \cdot \left({im}^{6} \cdot \cos re\right) + 0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)\right) + 0.5 \cdot \left({im}^{2} \cdot \cos re\right)\right)} + \cos re \]
      3. associate-+l+100.0%

        \[\leadsto \color{blue}{\left(0.001388888888888889 \cdot \left({im}^{6} \cdot \cos re\right) + 0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)\right) + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right)} \]
      4. associate-*r*100.0%

        \[\leadsto \left(\color{blue}{\left(0.001388888888888889 \cdot {im}^{6}\right) \cdot \cos re} + 0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)\right) + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right) \]
      5. associate-*r*100.0%

        \[\leadsto \left(\left(0.001388888888888889 \cdot {im}^{6}\right) \cdot \cos re + \color{blue}{\left(0.041666666666666664 \cdot {im}^{4}\right) \cdot \cos re}\right) + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right) \]
      6. distribute-rgt-out100.0%

        \[\leadsto \color{blue}{\cos re \cdot \left(0.001388888888888889 \cdot {im}^{6} + 0.041666666666666664 \cdot {im}^{4}\right)} + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right) \]
      7. fma-def100.0%

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right)} + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right) \]
      8. associate-*r*100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right) + \left(\color{blue}{\left(0.5 \cdot {im}^{2}\right) \cdot \cos re} + \cos re\right) \]
      9. distribute-lft1-in100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right) + \color{blue}{\left(0.5 \cdot {im}^{2} + 1\right) \cdot \cos re} \]
      10. fma-def100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right) + \color{blue}{\mathsf{fma}\left(0.5, {im}^{2}, 1\right)} \cdot \cos re \]
    5. Simplified100.0%

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right) + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re} \]
    6. Taylor expanded in im around 0 100.0%

      \[\leadsto \color{blue}{0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)} + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re \]
    7. Step-by-step derivation
      1. associate-*r*100.0%

        \[\leadsto \color{blue}{\left(0.041666666666666664 \cdot {im}^{4}\right) \cdot \cos re} + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re \]
      2. *-commutative100.0%

        \[\leadsto \color{blue}{\cos re \cdot \left(0.041666666666666664 \cdot {im}^{4}\right)} + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re \]
    8. Simplified100.0%

      \[\leadsto \color{blue}{\cos re \cdot \left(0.041666666666666664 \cdot {im}^{4}\right)} + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re \]
    9. Taylor expanded in re around inf 100.0%

      \[\leadsto \cos re \cdot \left(0.041666666666666664 \cdot {im}^{4}\right) + \color{blue}{\cos re \cdot \left(1 + 0.5 \cdot {im}^{2}\right)} \]
    10. Taylor expanded in im around inf 100.0%

      \[\leadsto \color{blue}{0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification81.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 4000:\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{elif}\;im \leq 1.15 \cdot 10^{+77}:\\ \;\;\;\;0.5 \cdot \left(e^{im} + e^{-im}\right)\\ \mathbf{else}:\\ \;\;\;\;0.041666666666666664 \cdot \left(\cos re \cdot {im}^{4}\right)\\ \end{array} \]

Alternative 6: 68.8% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 2.2:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;0.041666666666666664 \cdot \left(\cos re \cdot {im}^{4}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 2.2) (cos re) (* 0.041666666666666664 (* (cos re) (pow im 4.0)))))
double code(double re, double im) {
	double tmp;
	if (im <= 2.2) {
		tmp = cos(re);
	} else {
		tmp = 0.041666666666666664 * (cos(re) * pow(im, 4.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.2d0) then
        tmp = cos(re)
    else
        tmp = 0.041666666666666664d0 * (cos(re) * (im ** 4.0d0))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 2.2) {
		tmp = Math.cos(re);
	} else {
		tmp = 0.041666666666666664 * (Math.cos(re) * Math.pow(im, 4.0));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 2.2:
		tmp = math.cos(re)
	else:
		tmp = 0.041666666666666664 * (math.cos(re) * math.pow(im, 4.0))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 2.2)
		tmp = cos(re);
	else
		tmp = Float64(0.041666666666666664 * Float64(cos(re) * (im ^ 4.0)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 2.2)
		tmp = cos(re);
	else
		tmp = 0.041666666666666664 * (cos(re) * (im ^ 4.0));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 2.2], N[Cos[re], $MachinePrecision], N[(0.041666666666666664 * N[(N[Cos[re], $MachinePrecision] * N[Power[im, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

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

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


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

    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.5%

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

    if 2.2000000000000002 < 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. Taylor expanded in im around 0 77.3%

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

        \[\leadsto \color{blue}{\left(0.001388888888888889 \cdot \left({im}^{6} \cdot \cos re\right) + \left(0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right) + 0.5 \cdot \left({im}^{2} \cdot \cos re\right)\right)\right) + \cos re} \]
      2. associate-+r+77.3%

        \[\leadsto \color{blue}{\left(\left(0.001388888888888889 \cdot \left({im}^{6} \cdot \cos re\right) + 0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)\right) + 0.5 \cdot \left({im}^{2} \cdot \cos re\right)\right)} + \cos re \]
      3. associate-+l+77.3%

        \[\leadsto \color{blue}{\left(0.001388888888888889 \cdot \left({im}^{6} \cdot \cos re\right) + 0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)\right) + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right)} \]
      4. associate-*r*77.3%

        \[\leadsto \left(\color{blue}{\left(0.001388888888888889 \cdot {im}^{6}\right) \cdot \cos re} + 0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)\right) + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right) \]
      5. associate-*r*77.3%

        \[\leadsto \left(\left(0.001388888888888889 \cdot {im}^{6}\right) \cdot \cos re + \color{blue}{\left(0.041666666666666664 \cdot {im}^{4}\right) \cdot \cos re}\right) + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right) \]
      6. distribute-rgt-out77.3%

        \[\leadsto \color{blue}{\cos re \cdot \left(0.001388888888888889 \cdot {im}^{6} + 0.041666666666666664 \cdot {im}^{4}\right)} + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right) \]
      7. fma-def77.3%

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right)} + \left(0.5 \cdot \left({im}^{2} \cdot \cos re\right) + \cos re\right) \]
      8. associate-*r*77.3%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right) + \left(\color{blue}{\left(0.5 \cdot {im}^{2}\right) \cdot \cos re} + \cos re\right) \]
      9. distribute-lft1-in77.3%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right) + \color{blue}{\left(0.5 \cdot {im}^{2} + 1\right) \cdot \cos re} \]
      10. fma-def77.3%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right) + \color{blue}{\mathsf{fma}\left(0.5, {im}^{2}, 1\right)} \cdot \cos re \]
    5. Simplified77.3%

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.001388888888888889, {im}^{6}, 0.041666666666666664 \cdot {im}^{4}\right) + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re} \]
    6. Taylor expanded in im around 0 74.5%

      \[\leadsto \color{blue}{0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)} + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re \]
    7. Step-by-step derivation
      1. associate-*r*74.5%

        \[\leadsto \color{blue}{\left(0.041666666666666664 \cdot {im}^{4}\right) \cdot \cos re} + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re \]
      2. *-commutative74.5%

        \[\leadsto \color{blue}{\cos re \cdot \left(0.041666666666666664 \cdot {im}^{4}\right)} + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re \]
    8. Simplified74.5%

      \[\leadsto \color{blue}{\cos re \cdot \left(0.041666666666666664 \cdot {im}^{4}\right)} + \mathsf{fma}\left(0.5, {im}^{2}, 1\right) \cdot \cos re \]
    9. Taylor expanded in re around inf 74.5%

      \[\leadsto \cos re \cdot \left(0.041666666666666664 \cdot {im}^{4}\right) + \color{blue}{\cos re \cdot \left(1 + 0.5 \cdot {im}^{2}\right)} \]
    10. Taylor expanded in im around inf 74.5%

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

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

Alternative 7: 59.2% accurate, 2.9× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\


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

    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 59.0%

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

    if 3.8000000000000001e41 < im

    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 69.6%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(2 + {im}^{2}\right)} \]
    4. Step-by-step derivation
      1. +-commutative44.5%

        \[\leadsto 0.5 \cdot \color{blue}{\left({im}^{2} + 2\right)} \]
      2. unpow244.5%

        \[\leadsto 0.5 \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-def44.5%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified44.5%

      \[\leadsto 0.5 \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification55.8%

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

Alternative 8: 50.5% 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
  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 46.8%

    \[\leadsto \color{blue}{\cos re} \]
  4. Final simplification46.8%

    \[\leadsto \cos re \]

Alternative 9: 3.7% accurate, 308.0× speedup?

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

\\
-0.5
\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 66.1%

    \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
  3. Applied egg-rr3.6%

    \[\leadsto 0.5 \cdot \color{blue}{-1} \]
  4. Final simplification3.6%

    \[\leadsto -0.5 \]

Alternative 10: 7.6% accurate, 308.0× speedup?

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

\\
0.125
\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 66.1%

    \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
  3. Applied egg-rr7.2%

    \[\leadsto 0.5 \cdot \color{blue}{0.25} \]
  4. Final simplification7.2%

    \[\leadsto 0.125 \]

Alternative 11: 7.9% accurate, 308.0× speedup?

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

\\
0.25
\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 66.1%

    \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
  3. Applied egg-rr7.5%

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

    \[\leadsto 0.25 \]

Alternative 12: 8.5% accurate, 308.0× speedup?

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

\\
0.5
\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 66.1%

    \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
  3. Applied egg-rr8.0%

    \[\leadsto 0.5 \cdot \color{blue}{1} \]
  4. Final simplification8.0%

    \[\leadsto 0.5 \]

Alternative 13: 9.0% accurate, 308.0× speedup?

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

\\
0.75
\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 66.1%

    \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
  3. Applied egg-rr8.4%

    \[\leadsto 0.5 \cdot \color{blue}{1.5} \]
  4. Final simplification8.4%

    \[\leadsto 0.75 \]

Alternative 14: 28.1% 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
  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 66.1%

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

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

    \[\leadsto 1 \]

Reproduce

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