math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 5.4s
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
  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(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]

Alternative 2: 85.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 4.2 \cdot 10^{-7} \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\ \;\;\;\;\mathsf{fma}\left(0.5, \cos re \cdot \left(im \cdot im\right), \cos re\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= im 4.2e-7) (not (<= im 1.35e+154)))
   (fma 0.5 (* (cos re) (* im im)) (cos re))
   (* 0.5 (+ (exp (- im)) (exp im)))))
double code(double re, double im) {
	double tmp;
	if ((im <= 4.2e-7) || !(im <= 1.35e+154)) {
		tmp = fma(0.5, (cos(re) * (im * im)), cos(re));
	} else {
		tmp = 0.5 * (exp(-im) + exp(im));
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if ((im <= 4.2e-7) || !(im <= 1.35e+154))
		tmp = fma(0.5, Float64(cos(re) * Float64(im * im)), cos(re));
	else
		tmp = Float64(0.5 * Float64(exp(Float64(-im)) + exp(im)));
	end
	return tmp
end
code[re_, im_] := If[Or[LessEqual[im, 4.2e-7], N[Not[LessEqual[im, 1.35e+154]], $MachinePrecision]], N[(0.5 * N[(N[Cos[re], $MachinePrecision] * N[(im * im), $MachinePrecision]), $MachinePrecision] + N[Cos[re], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 4.2 \cdot 10^{-7} \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\
\;\;\;\;\mathsf{fma}\left(0.5, \cos re \cdot \left(im \cdot im\right), \cos re\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 4.2e-7 or 1.35000000000000003e154 < im

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

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

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

    if 4.2e-7 < im < 1.35000000000000003e154

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 4.2 \cdot 10^{-7} \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\ \;\;\;\;\mathsf{fma}\left(0.5, \cos re \cdot \left(im \cdot im\right), \cos re\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \end{array} \]

Alternative 3: 69.8% accurate, 1.4× speedup?

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

\\
\begin{array}{l}
t_0 := e^{-im} + e^{im}\\
\mathbf{if}\;im \leq 4.2 \cdot 10^{-7}:\\
\;\;\;\;\cos re\\

\mathbf{elif}\;im \leq 9 \cdot 10^{+127}:\\
\;\;\;\;0.5 \cdot t_0\\

\mathbf{else}:\\
\;\;\;\;t_0 \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if im < 4.2e-7

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

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

    if 4.2e-7 < im < 9.00000000000000068e127

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

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

    if 9.00000000000000068e127 < 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 0.0%

      \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right) + -0.25 \cdot \left({re}^{2} \cdot \left(e^{im} + e^{-im}\right)\right)} \]
    3. Simplified85.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 4.2 \cdot 10^{-7}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 9 \cdot 10^{+127}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(e^{-im} + e^{im}\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \end{array} \]

Alternative 4: 73.6% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := re \cdot \left(re \cdot -0.5\right)\\ \mathbf{if}\;im \leq 1.95 \cdot 10^{+23} \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\ \;\;\;\;\mathsf{fma}\left(0.5, im \cdot im, \cos re\right)\\ \mathbf{else}:\\ \;\;\;\;1 + \frac{t_0 \cdot t_0 - {im}^{4} \cdot 0.25}{t_0 - 0.5 \cdot \left(im \cdot im\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* re (* re -0.5))))
   (if (or (<= im 1.95e+23) (not (<= im 1.35e+154)))
     (fma 0.5 (* im im) (cos re))
     (+
      1.0
      (/ (- (* t_0 t_0) (* (pow im 4.0) 0.25)) (- t_0 (* 0.5 (* im im))))))))
double code(double re, double im) {
	double t_0 = re * (re * -0.5);
	double tmp;
	if ((im <= 1.95e+23) || !(im <= 1.35e+154)) {
		tmp = fma(0.5, (im * im), cos(re));
	} else {
		tmp = 1.0 + (((t_0 * t_0) - (pow(im, 4.0) * 0.25)) / (t_0 - (0.5 * (im * im))));
	}
	return tmp;
}
function code(re, im)
	t_0 = Float64(re * Float64(re * -0.5))
	tmp = 0.0
	if ((im <= 1.95e+23) || !(im <= 1.35e+154))
		tmp = fma(0.5, Float64(im * im), cos(re));
	else
		tmp = Float64(1.0 + Float64(Float64(Float64(t_0 * t_0) - Float64((im ^ 4.0) * 0.25)) / Float64(t_0 - Float64(0.5 * Float64(im * im)))));
	end
	return tmp
end
code[re_, im_] := Block[{t$95$0 = N[(re * N[(re * -0.5), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[im, 1.95e+23], N[Not[LessEqual[im, 1.35e+154]], $MachinePrecision]], N[(0.5 * N[(im * im), $MachinePrecision] + N[Cos[re], $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(N[(N[(t$95$0 * t$95$0), $MachinePrecision] - N[(N[Power[im, 4.0], $MachinePrecision] * 0.25), $MachinePrecision]), $MachinePrecision] / N[(t$95$0 - N[(0.5 * N[(im * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := re \cdot \left(re \cdot -0.5\right)\\
\mathbf{if}\;im \leq 1.95 \cdot 10^{+23} \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\
\;\;\;\;\mathsf{fma}\left(0.5, im \cdot im, \cos re\right)\\

\mathbf{else}:\\
\;\;\;\;1 + \frac{t_0 \cdot t_0 - {im}^{4} \cdot 0.25}{t_0 - 0.5 \cdot \left(im \cdot im\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 1.95e23 or 1.35000000000000003e154 < im

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

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

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{{im}^{2}}, \cos re\right) \]
    5. Step-by-step derivation
      1. unpow271.2%

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

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

    if 1.95e23 < im < 1.35000000000000003e154

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

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

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{{im}^{2}}, \cos re\right) \]
    5. Step-by-step derivation
      1. unpow24.7%

        \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{im \cdot im}, \cos re\right) \]
    6. Simplified4.7%

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

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

        \[\leadsto 1 + \color{blue}{\left(-0.5 \cdot {re}^{2} + 0.5 \cdot {im}^{2}\right)} \]
      2. *-commutative10.6%

        \[\leadsto 1 + \left(\color{blue}{{re}^{2} \cdot -0.5} + 0.5 \cdot {im}^{2}\right) \]
      3. unpow210.6%

        \[\leadsto 1 + \left({re}^{2} \cdot -0.5 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
      4. fma-def10.6%

        \[\leadsto 1 + \color{blue}{\mathsf{fma}\left({re}^{2}, -0.5, 0.5 \cdot \left(im \cdot im\right)\right)} \]
      5. unpow210.6%

        \[\leadsto 1 + \mathsf{fma}\left(\color{blue}{re \cdot re}, -0.5, 0.5 \cdot \left(im \cdot im\right)\right) \]
    9. Simplified10.6%

      \[\leadsto \color{blue}{1 + \mathsf{fma}\left(re \cdot re, -0.5, 0.5 \cdot \left(im \cdot im\right)\right)} \]
    10. Step-by-step derivation
      1. fma-udef10.6%

        \[\leadsto 1 + \color{blue}{\left(\left(re \cdot re\right) \cdot -0.5 + 0.5 \cdot \left(im \cdot im\right)\right)} \]
      2. flip-+23.6%

        \[\leadsto 1 + \color{blue}{\frac{\left(\left(re \cdot re\right) \cdot -0.5\right) \cdot \left(\left(re \cdot re\right) \cdot -0.5\right) - \left(0.5 \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot \left(im \cdot im\right)\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)}} \]
      3. associate-*l*23.6%

        \[\leadsto 1 + \frac{\color{blue}{\left(re \cdot \left(re \cdot -0.5\right)\right)} \cdot \left(\left(re \cdot re\right) \cdot -0.5\right) - \left(0.5 \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot \left(im \cdot im\right)\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      4. associate-*l*23.6%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \color{blue}{\left(re \cdot \left(re \cdot -0.5\right)\right)} - \left(0.5 \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot \left(im \cdot im\right)\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      5. *-commutative23.6%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - \color{blue}{\left(\left(im \cdot im\right) \cdot 0.5\right)} \cdot \left(0.5 \cdot \left(im \cdot im\right)\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      6. *-commutative23.6%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - \left(\left(im \cdot im\right) \cdot 0.5\right) \cdot \color{blue}{\left(\left(im \cdot im\right) \cdot 0.5\right)}}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      7. swap-sqr23.6%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - \color{blue}{\left(\left(im \cdot im\right) \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot 0.5\right)}}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      8. pow223.6%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - \left(\color{blue}{{im}^{2}} \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot 0.5\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      9. pow223.6%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - \left({im}^{2} \cdot \color{blue}{{im}^{2}}\right) \cdot \left(0.5 \cdot 0.5\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      10. pow-prod-up23.6%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - \color{blue}{{im}^{\left(2 + 2\right)}} \cdot \left(0.5 \cdot 0.5\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      11. metadata-eval23.6%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - {im}^{\color{blue}{4}} \cdot \left(0.5 \cdot 0.5\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      12. metadata-eval23.6%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - {im}^{4} \cdot \color{blue}{0.25}}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      13. associate-*l*23.6%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - {im}^{4} \cdot 0.25}{\color{blue}{re \cdot \left(re \cdot -0.5\right)} - 0.5 \cdot \left(im \cdot im\right)} \]
    11. Applied egg-rr23.6%

      \[\leadsto 1 + \color{blue}{\frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - {im}^{4} \cdot 0.25}{re \cdot \left(re \cdot -0.5\right) - 0.5 \cdot \left(im \cdot im\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification66.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1.95 \cdot 10^{+23} \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\ \;\;\;\;\mathsf{fma}\left(0.5, im \cdot im, \cos re\right)\\ \mathbf{else}:\\ \;\;\;\;1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - {im}^{4} \cdot 0.25}{re \cdot \left(re \cdot -0.5\right) - 0.5 \cdot \left(im \cdot im\right)}\\ \end{array} \]

Alternative 5: 69.7% accurate, 1.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 4.2 \cdot 10^{-7}:\\
\;\;\;\;\cos re\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 4.2e-7

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

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

    if 4.2e-7 < 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 72.1%

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

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

Alternative 6: 64.4% accurate, 2.3× speedup?

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

\\
\begin{array}{l}
t_0 := re \cdot \left(re \cdot -0.5\right)\\
t_1 := 0.5 \cdot \left(im \cdot im\right)\\
\mathbf{if}\;im \leq 2.6 \cdot 10^{+25}:\\
\;\;\;\;\cos re\\

\mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\
\;\;\;\;1 + \frac{t_0 \cdot t_0 - {im}^{4} \cdot 0.25}{t_0 - t_1}\\

\mathbf{else}:\\
\;\;\;\;1 + t_1\\


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

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

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

    if 2.5999999999999998e25 < im < 1.35000000000000003e154

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

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

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{{im}^{2}}, \cos re\right) \]
    5. Step-by-step derivation
      1. unpow24.8%

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

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

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

        \[\leadsto 1 + \color{blue}{\left(-0.5 \cdot {re}^{2} + 0.5 \cdot {im}^{2}\right)} \]
      2. *-commutative10.8%

        \[\leadsto 1 + \left(\color{blue}{{re}^{2} \cdot -0.5} + 0.5 \cdot {im}^{2}\right) \]
      3. unpow210.8%

        \[\leadsto 1 + \left({re}^{2} \cdot -0.5 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
      4. fma-def10.8%

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

        \[\leadsto 1 + \mathsf{fma}\left(\color{blue}{re \cdot re}, -0.5, 0.5 \cdot \left(im \cdot im\right)\right) \]
    9. Simplified10.8%

      \[\leadsto \color{blue}{1 + \mathsf{fma}\left(re \cdot re, -0.5, 0.5 \cdot \left(im \cdot im\right)\right)} \]
    10. Step-by-step derivation
      1. fma-udef10.8%

        \[\leadsto 1 + \color{blue}{\left(\left(re \cdot re\right) \cdot -0.5 + 0.5 \cdot \left(im \cdot im\right)\right)} \]
      2. flip-+24.3%

        \[\leadsto 1 + \color{blue}{\frac{\left(\left(re \cdot re\right) \cdot -0.5\right) \cdot \left(\left(re \cdot re\right) \cdot -0.5\right) - \left(0.5 \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot \left(im \cdot im\right)\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)}} \]
      3. associate-*l*24.3%

        \[\leadsto 1 + \frac{\color{blue}{\left(re \cdot \left(re \cdot -0.5\right)\right)} \cdot \left(\left(re \cdot re\right) \cdot -0.5\right) - \left(0.5 \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot \left(im \cdot im\right)\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      4. associate-*l*24.3%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \color{blue}{\left(re \cdot \left(re \cdot -0.5\right)\right)} - \left(0.5 \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot \left(im \cdot im\right)\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      5. *-commutative24.3%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - \color{blue}{\left(\left(im \cdot im\right) \cdot 0.5\right)} \cdot \left(0.5 \cdot \left(im \cdot im\right)\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      6. *-commutative24.3%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - \left(\left(im \cdot im\right) \cdot 0.5\right) \cdot \color{blue}{\left(\left(im \cdot im\right) \cdot 0.5\right)}}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      7. swap-sqr24.3%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - \color{blue}{\left(\left(im \cdot im\right) \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot 0.5\right)}}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      8. pow224.3%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - \left(\color{blue}{{im}^{2}} \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot 0.5\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      9. pow224.3%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - \left({im}^{2} \cdot \color{blue}{{im}^{2}}\right) \cdot \left(0.5 \cdot 0.5\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      10. pow-prod-up24.3%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - \color{blue}{{im}^{\left(2 + 2\right)}} \cdot \left(0.5 \cdot 0.5\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      11. metadata-eval24.3%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - {im}^{\color{blue}{4}} \cdot \left(0.5 \cdot 0.5\right)}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      12. metadata-eval24.3%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - {im}^{4} \cdot \color{blue}{0.25}}{\left(re \cdot re\right) \cdot -0.5 - 0.5 \cdot \left(im \cdot im\right)} \]
      13. associate-*l*24.3%

        \[\leadsto 1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - {im}^{4} \cdot 0.25}{\color{blue}{re \cdot \left(re \cdot -0.5\right)} - 0.5 \cdot \left(im \cdot im\right)} \]
    11. Applied egg-rr24.3%

      \[\leadsto 1 + \color{blue}{\frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - {im}^{4} \cdot 0.25}{re \cdot \left(re \cdot -0.5\right) - 0.5 \cdot \left(im \cdot im\right)}} \]

    if 1.35000000000000003e154 < im

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

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

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{{im}^{2}}, \cos re\right) \]
    5. Step-by-step derivation
      1. unpow255.6%

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

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

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

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

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

        \[\leadsto 1 + \left({re}^{2} \cdot -0.5 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
      4. fma-def47.2%

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

        \[\leadsto 1 + \mathsf{fma}\left(\color{blue}{re \cdot re}, -0.5, 0.5 \cdot \left(im \cdot im\right)\right) \]
    9. Simplified47.2%

      \[\leadsto \color{blue}{1 + \mathsf{fma}\left(re \cdot re, -0.5, 0.5 \cdot \left(im \cdot im\right)\right)} \]
    10. Taylor expanded in re around 0 55.6%

      \[\leadsto 1 + \color{blue}{0.5 \cdot {im}^{2}} \]
    11. Step-by-step derivation
      1. unpow255.6%

        \[\leadsto 1 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
    12. Simplified55.6%

      \[\leadsto 1 + \color{blue}{0.5 \cdot \left(im \cdot im\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification56.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 2.6 \cdot 10^{+25}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\ \;\;\;\;1 + \frac{\left(re \cdot \left(re \cdot -0.5\right)\right) \cdot \left(re \cdot \left(re \cdot -0.5\right)\right) - {im}^{4} \cdot 0.25}{re \cdot \left(re \cdot -0.5\right) - 0.5 \cdot \left(im \cdot im\right)}\\ \mathbf{else}:\\ \;\;\;\;1 + 0.5 \cdot \left(im \cdot im\right)\\ \end{array} \]

Alternative 7: 62.2% accurate, 3.0× speedup?

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

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

\mathbf{elif}\;im \leq 3.5 \cdot 10^{+151}:\\
\;\;\;\;0.25 + \left(re \cdot re\right) \cdot 0.25\\

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


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

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

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

    if 720 < im < 3.5000000000000003e151

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Applied egg-rr2.9%

      \[\leadsto \color{blue}{{\left(\cos re \cdot -2\right)}^{-2}} \]
    3. Taylor expanded in re around 0 22.4%

      \[\leadsto \color{blue}{0.25 + 0.25 \cdot {re}^{2}} \]
    4. Step-by-step derivation
      1. *-commutative22.4%

        \[\leadsto 0.25 + \color{blue}{{re}^{2} \cdot 0.25} \]
      2. unpow222.4%

        \[\leadsto 0.25 + \color{blue}{\left(re \cdot re\right)} \cdot 0.25 \]
    5. Simplified22.4%

      \[\leadsto \color{blue}{0.25 + \left(re \cdot re\right) \cdot 0.25} \]

    if 3.5000000000000003e151 < im

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

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

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{{im}^{2}}, \cos re\right) \]
    5. Step-by-step derivation
      1. unpow255.6%

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

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

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

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

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

        \[\leadsto 1 + \left({re}^{2} \cdot -0.5 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
      4. fma-def47.2%

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

        \[\leadsto 1 + \mathsf{fma}\left(\color{blue}{re \cdot re}, -0.5, 0.5 \cdot \left(im \cdot im\right)\right) \]
    9. Simplified47.2%

      \[\leadsto \color{blue}{1 + \mathsf{fma}\left(re \cdot re, -0.5, 0.5 \cdot \left(im \cdot im\right)\right)} \]
    10. Taylor expanded in re around 0 55.6%

      \[\leadsto 1 + \color{blue}{0.5 \cdot {im}^{2}} \]
    11. Step-by-step derivation
      1. unpow255.6%

        \[\leadsto 1 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
    12. Simplified55.6%

      \[\leadsto 1 + \color{blue}{0.5 \cdot \left(im \cdot im\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification57.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 720:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 3.5 \cdot 10^{+151}:\\ \;\;\;\;0.25 + \left(re \cdot re\right) \cdot 0.25\\ \mathbf{else}:\\ \;\;\;\;1 + 0.5 \cdot \left(im \cdot im\right)\\ \end{array} \]

Alternative 8: 47.7% accurate, 23.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq 5.8 \cdot 10^{+165}:\\ \;\;\;\;1 + 0.5 \cdot \left(im \cdot im\right)\\ \mathbf{elif}\;re \leq 6.5 \cdot 10^{+238} \lor \neg \left(re \leq 1.8 \cdot 10^{+290}\right):\\ \;\;\;\;1 + \left(re \cdot re\right) \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;0.25 + \left(re \cdot re\right) \cdot 0.25\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re 5.8e+165)
   (+ 1.0 (* 0.5 (* im im)))
   (if (or (<= re 6.5e+238) (not (<= re 1.8e+290)))
     (+ 1.0 (* (* re re) -0.5))
     (+ 0.25 (* (* re re) 0.25)))))
double code(double re, double im) {
	double tmp;
	if (re <= 5.8e+165) {
		tmp = 1.0 + (0.5 * (im * im));
	} else if ((re <= 6.5e+238) || !(re <= 1.8e+290)) {
		tmp = 1.0 + ((re * re) * -0.5);
	} else {
		tmp = 0.25 + ((re * re) * 0.25);
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= 5.8d+165) then
        tmp = 1.0d0 + (0.5d0 * (im * im))
    else if ((re <= 6.5d+238) .or. (.not. (re <= 1.8d+290))) then
        tmp = 1.0d0 + ((re * re) * (-0.5d0))
    else
        tmp = 0.25d0 + ((re * re) * 0.25d0)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= 5.8e+165) {
		tmp = 1.0 + (0.5 * (im * im));
	} else if ((re <= 6.5e+238) || !(re <= 1.8e+290)) {
		tmp = 1.0 + ((re * re) * -0.5);
	} else {
		tmp = 0.25 + ((re * re) * 0.25);
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= 5.8e+165:
		tmp = 1.0 + (0.5 * (im * im))
	elif (re <= 6.5e+238) or not (re <= 1.8e+290):
		tmp = 1.0 + ((re * re) * -0.5)
	else:
		tmp = 0.25 + ((re * re) * 0.25)
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= 5.8e+165)
		tmp = Float64(1.0 + Float64(0.5 * Float64(im * im)));
	elseif ((re <= 6.5e+238) || !(re <= 1.8e+290))
		tmp = Float64(1.0 + Float64(Float64(re * re) * -0.5));
	else
		tmp = Float64(0.25 + Float64(Float64(re * re) * 0.25));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= 5.8e+165)
		tmp = 1.0 + (0.5 * (im * im));
	elseif ((re <= 6.5e+238) || ~((re <= 1.8e+290)))
		tmp = 1.0 + ((re * re) * -0.5);
	else
		tmp = 0.25 + ((re * re) * 0.25);
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, 5.8e+165], N[(1.0 + N[(0.5 * N[(im * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 6.5e+238], N[Not[LessEqual[re, 1.8e+290]], $MachinePrecision]], N[(1.0 + N[(N[(re * re), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision], N[(0.25 + N[(N[(re * re), $MachinePrecision] * 0.25), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq 5.8 \cdot 10^{+165}:\\
\;\;\;\;1 + 0.5 \cdot \left(im \cdot im\right)\\

\mathbf{elif}\;re \leq 6.5 \cdot 10^{+238} \lor \neg \left(re \leq 1.8 \cdot 10^{+290}\right):\\
\;\;\;\;1 + \left(re \cdot re\right) \cdot -0.5\\

\mathbf{else}:\\
\;\;\;\;0.25 + \left(re \cdot re\right) \cdot 0.25\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < 5.80000000000000011e165

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

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

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{{im}^{2}}, \cos re\right) \]
    5. Step-by-step derivation
      1. unpow265.5%

        \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{im \cdot im}, \cos re\right) \]
    6. Simplified65.5%

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

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

        \[\leadsto 1 + \color{blue}{\left(-0.5 \cdot {re}^{2} + 0.5 \cdot {im}^{2}\right)} \]
      2. *-commutative49.0%

        \[\leadsto 1 + \left(\color{blue}{{re}^{2} \cdot -0.5} + 0.5 \cdot {im}^{2}\right) \]
      3. unpow249.0%

        \[\leadsto 1 + \left({re}^{2} \cdot -0.5 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
      4. fma-def49.0%

        \[\leadsto 1 + \color{blue}{\mathsf{fma}\left({re}^{2}, -0.5, 0.5 \cdot \left(im \cdot im\right)\right)} \]
      5. unpow249.0%

        \[\leadsto 1 + \mathsf{fma}\left(\color{blue}{re \cdot re}, -0.5, 0.5 \cdot \left(im \cdot im\right)\right) \]
    9. Simplified49.0%

      \[\leadsto \color{blue}{1 + \mathsf{fma}\left(re \cdot re, -0.5, 0.5 \cdot \left(im \cdot im\right)\right)} \]
    10. Taylor expanded in re around 0 49.1%

      \[\leadsto 1 + \color{blue}{0.5 \cdot {im}^{2}} \]
    11. Step-by-step derivation
      1. unpow249.1%

        \[\leadsto 1 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
    12. Simplified49.1%

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

    if 5.80000000000000011e165 < re < 6.5000000000000005e238 or 1.79999999999999994e290 < re

    1. Initial program 99.9%

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

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

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{{im}^{2}}, \cos re\right) \]
    5. Step-by-step derivation
      1. unpow261.0%

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

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

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

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

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

        \[\leadsto 1 + \left({re}^{2} \cdot -0.5 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
      4. fma-def9.2%

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

        \[\leadsto 1 + \mathsf{fma}\left(\color{blue}{re \cdot re}, -0.5, 0.5 \cdot \left(im \cdot im\right)\right) \]
    9. Simplified9.2%

      \[\leadsto \color{blue}{1 + \mathsf{fma}\left(re \cdot re, -0.5, 0.5 \cdot \left(im \cdot im\right)\right)} \]
    10. Taylor expanded in re around inf 40.0%

      \[\leadsto 1 + \color{blue}{-0.5 \cdot {re}^{2}} \]
    11. Step-by-step derivation
      1. unpow240.0%

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\left(re \cdot re\right)} \]
    12. Simplified40.0%

      \[\leadsto 1 + \color{blue}{-0.5 \cdot \left(re \cdot re\right)} \]

    if 6.5000000000000005e238 < re < 1.79999999999999994e290

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Applied egg-rr4.4%

      \[\leadsto \color{blue}{{\left(\cos re \cdot -2\right)}^{-2}} \]
    3. Taylor expanded in re around 0 56.0%

      \[\leadsto \color{blue}{0.25 + 0.25 \cdot {re}^{2}} \]
    4. Step-by-step derivation
      1. *-commutative56.0%

        \[\leadsto 0.25 + \color{blue}{{re}^{2} \cdot 0.25} \]
      2. unpow256.0%

        \[\leadsto 0.25 + \color{blue}{\left(re \cdot re\right)} \cdot 0.25 \]
    5. Simplified56.0%

      \[\leadsto \color{blue}{0.25 + \left(re \cdot re\right) \cdot 0.25} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification48.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 5.8 \cdot 10^{+165}:\\ \;\;\;\;1 + 0.5 \cdot \left(im \cdot im\right)\\ \mathbf{elif}\;re \leq 6.5 \cdot 10^{+238} \lor \neg \left(re \leq 1.8 \cdot 10^{+290}\right):\\ \;\;\;\;1 + \left(re \cdot re\right) \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;0.25 + \left(re \cdot re\right) \cdot 0.25\\ \end{array} \]

Alternative 9: 40.2% accurate, 27.7× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 24.5:\\
\;\;\;\;1 + \left(re \cdot re\right) \cdot -0.5\\

\mathbf{elif}\;im \leq 2.8 \cdot 10^{+151}:\\
\;\;\;\;0.25 + \left(re \cdot re\right) \cdot 0.25\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot im\right)\\


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

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

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

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{{im}^{2}}, \cos re\right) \]
    5. Step-by-step derivation
      1. unpow275.6%

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

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

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

        \[\leadsto 1 + \color{blue}{\left(-0.5 \cdot {re}^{2} + 0.5 \cdot {im}^{2}\right)} \]
      2. *-commutative50.8%

        \[\leadsto 1 + \left(\color{blue}{{re}^{2} \cdot -0.5} + 0.5 \cdot {im}^{2}\right) \]
      3. unpow250.8%

        \[\leadsto 1 + \left({re}^{2} \cdot -0.5 + 0.5 \cdot \color{blue}{\left(im \cdot im\right)}\right) \]
      4. fma-def50.8%

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

        \[\leadsto 1 + \mathsf{fma}\left(\color{blue}{re \cdot re}, -0.5, 0.5 \cdot \left(im \cdot im\right)\right) \]
    9. Simplified50.8%

      \[\leadsto \color{blue}{1 + \mathsf{fma}\left(re \cdot re, -0.5, 0.5 \cdot \left(im \cdot im\right)\right)} \]
    10. Taylor expanded in re around inf 39.2%

      \[\leadsto 1 + \color{blue}{-0.5 \cdot {re}^{2}} \]
    11. Step-by-step derivation
      1. unpow239.2%

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\left(re \cdot re\right)} \]
    12. Simplified39.2%

      \[\leadsto 1 + \color{blue}{-0.5 \cdot \left(re \cdot re\right)} \]

    if 24.5 < im < 2.79999999999999987e151

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Applied egg-rr2.9%

      \[\leadsto \color{blue}{{\left(\cos re \cdot -2\right)}^{-2}} \]
    3. Taylor expanded in re around 0 22.4%

      \[\leadsto \color{blue}{0.25 + 0.25 \cdot {re}^{2}} \]
    4. Step-by-step derivation
      1. *-commutative22.4%

        \[\leadsto 0.25 + \color{blue}{{re}^{2} \cdot 0.25} \]
      2. unpow222.4%

        \[\leadsto 0.25 + \color{blue}{\left(re \cdot re\right)} \cdot 0.25 \]
    5. Simplified22.4%

      \[\leadsto \color{blue}{0.25 + \left(re \cdot re\right) \cdot 0.25} \]

    if 2.79999999999999987e151 < im

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

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

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{{im}^{2}}, \cos re\right) \]
    5. Step-by-step derivation
      1. unpow255.6%

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{im \cdot im}, \cos re\right) \]
    7. Taylor expanded in im around inf 55.6%

      \[\leadsto \color{blue}{0.5 \cdot {im}^{2}} \]
    8. Step-by-step derivation
      1. unpow255.6%

        \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
    9. Simplified55.6%

      \[\leadsto \color{blue}{0.5 \cdot \left(im \cdot im\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification39.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 24.5:\\ \;\;\;\;1 + \left(re \cdot re\right) \cdot -0.5\\ \mathbf{elif}\;im \leq 2.8 \cdot 10^{+151}:\\ \;\;\;\;0.25 + \left(re \cdot re\right) \cdot 0.25\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot im\right)\\ \end{array} \]

Alternative 10: 20.0% accurate, 34.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 7.5 \cdot 10^{+150}:\\ \;\;\;\;0.25 + \left(re \cdot re\right) \cdot 0.25\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot im\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 7.5e+150) (+ 0.25 (* (* re re) 0.25)) (* 0.5 (* im im))))
double code(double re, double im) {
	double tmp;
	if (im <= 7.5e+150) {
		tmp = 0.25 + ((re * re) * 0.25);
	} else {
		tmp = 0.5 * (im * im);
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 7.5d+150) then
        tmp = 0.25d0 + ((re * re) * 0.25d0)
    else
        tmp = 0.5d0 * (im * im)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 7.5e+150) {
		tmp = 0.25 + ((re * re) * 0.25);
	} else {
		tmp = 0.5 * (im * im);
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 7.5e+150:
		tmp = 0.25 + ((re * re) * 0.25)
	else:
		tmp = 0.5 * (im * im)
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 7.5e+150)
		tmp = Float64(0.25 + Float64(Float64(re * re) * 0.25));
	else
		tmp = Float64(0.5 * Float64(im * im));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 7.5e+150)
		tmp = 0.25 + ((re * re) * 0.25);
	else
		tmp = 0.5 * (im * im);
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 7.5e+150], N[(0.25 + N[(N[(re * re), $MachinePrecision] * 0.25), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * im), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 7.5 \cdot 10^{+150}:\\
\;\;\;\;0.25 + \left(re \cdot re\right) \cdot 0.25\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot im\right)\\


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

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Applied egg-rr8.6%

      \[\leadsto \color{blue}{{\left(\cos re \cdot -2\right)}^{-2}} \]
    3. Taylor expanded in re around 0 12.5%

      \[\leadsto \color{blue}{0.25 + 0.25 \cdot {re}^{2}} \]
    4. Step-by-step derivation
      1. *-commutative12.5%

        \[\leadsto 0.25 + \color{blue}{{re}^{2} \cdot 0.25} \]
      2. unpow212.5%

        \[\leadsto 0.25 + \color{blue}{\left(re \cdot re\right)} \cdot 0.25 \]
    5. Simplified12.5%

      \[\leadsto \color{blue}{0.25 + \left(re \cdot re\right) \cdot 0.25} \]

    if 7.4999999999999998e150 < im

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

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

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{{im}^{2}}, \cos re\right) \]
    5. Step-by-step derivation
      1. unpow255.6%

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{im \cdot im}, \cos re\right) \]
    7. Taylor expanded in im around inf 55.6%

      \[\leadsto \color{blue}{0.5 \cdot {im}^{2}} \]
    8. Step-by-step derivation
      1. unpow255.6%

        \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
    9. Simplified55.6%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 7.5 \cdot 10^{+150}:\\ \;\;\;\;0.25 + \left(re \cdot re\right) \cdot 0.25\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot im\right)\\ \end{array} \]

Alternative 11: 17.4% accurate, 43.6× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 0.71:\\
\;\;\;\;0.25\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot im\right)\\


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

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Applied egg-rr9.6%

      \[\leadsto \color{blue}{{\left(\cos re \cdot -2\right)}^{-2}} \]
    3. Taylor expanded in re around 0 9.6%

      \[\leadsto \color{blue}{0.25} \]

    if 0.70999999999999996 < im

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

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

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{{im}^{2}}, \cos re\right) \]
    5. Step-by-step derivation
      1. unpow232.0%

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

      \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{im \cdot im}, \cos re\right) \]
    7. Taylor expanded in im around inf 32.0%

      \[\leadsto \color{blue}{0.5 \cdot {im}^{2}} \]
    8. Step-by-step derivation
      1. unpow232.0%

        \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
    9. Simplified32.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 0.71:\\ \;\;\;\;0.25\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot im\right)\\ \end{array} \]

Alternative 12: 8.2% 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. Applied egg-rr7.7%

    \[\leadsto \color{blue}{{\left(\cos re \cdot -2\right)}^{-2}} \]
  3. Taylor expanded in re around 0 7.7%

    \[\leadsto \color{blue}{0.25} \]
  4. Final simplification7.7%

    \[\leadsto 0.25 \]

Reproduce

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