math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 11.0s
Alternatives: 20
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 20 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: 84.9% accurate, 1.4× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 0.036 \lor \neg \left(im \leq 1.32 \cdot 10^{+154}\right):\\
\;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot 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}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 0.0359999999999999973 or 1.31999999999999998e154 < 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 84.6%

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

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

    if 0.0359999999999999973 < im < 1.31999999999999998e154

    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.25 \cdot \left({re}^{2} \cdot \left(e^{im} + e^{-im}\right)\right) + 0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Simplified77.8%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 0.036 \lor \neg \left(im \leq 1.32 \cdot 10^{+154}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot 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 3: 84.4% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 0.032 \lor \neg \left(im \leq 1.55 \cdot 10^{+152}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= im 0.032) (not (<= im 1.55e+152)))
   (* (* 0.5 (cos re)) (+ 2.0 (* im im)))
   (* 0.5 (+ (exp (- im)) (exp im)))))
double code(double re, double im) {
	double tmp;
	if ((im <= 0.032) || !(im <= 1.55e+152)) {
		tmp = (0.5 * cos(re)) * (2.0 + (im * im));
	} 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 <= 0.032d0) .or. (.not. (im <= 1.55d+152))) then
        tmp = (0.5d0 * cos(re)) * (2.0d0 + (im * im))
    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 <= 0.032) || !(im <= 1.55e+152)) {
		tmp = (0.5 * Math.cos(re)) * (2.0 + (im * im));
	} else {
		tmp = 0.5 * (Math.exp(-im) + Math.exp(im));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (im <= 0.032) or not (im <= 1.55e+152):
		tmp = (0.5 * math.cos(re)) * (2.0 + (im * im))
	else:
		tmp = 0.5 * (math.exp(-im) + math.exp(im))
	return tmp
function code(re, im)
	tmp = 0.0
	if ((im <= 0.032) || !(im <= 1.55e+152))
		tmp = Float64(Float64(0.5 * cos(re)) * Float64(2.0 + Float64(im * im)));
	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 <= 0.032) || ~((im <= 1.55e+152)))
		tmp = (0.5 * cos(re)) * (2.0 + (im * im));
	else
		tmp = 0.5 * (exp(-im) + exp(im));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Or[LessEqual[im, 0.032], N[Not[LessEqual[im, 1.55e+152]], $MachinePrecision]], N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(2.0 + N[(im * im), $MachinePrecision]), $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 0.032 \lor \neg \left(im \leq 1.55 \cdot 10^{+152}\right):\\
\;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\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 < 0.032000000000000001 or 1.55e152 < 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 84.3%

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

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

    if 0.032000000000000001 < im < 1.55e152

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 0.032 \lor \neg \left(im \leq 1.55 \cdot 10^{+152}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \end{array} \]

Alternative 4: 81.7% accurate, 2.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 2 + im \cdot im\\ t_1 := \left(0.5 \cdot \cos re\right) \cdot t_0\\ \mathbf{if}\;im \leq 31000:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 10^{+77}:\\ \;\;\;\;t_0 \cdot \left(0.5 + {re}^{-2}\right)\\ \mathbf{elif}\;im \leq 1.55 \cdot 10^{+152}:\\ \;\;\;\;\frac{0.5 + re}{\frac{2 - im \cdot im}{4 - {im}^{4}}}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (+ 2.0 (* im im))) (t_1 (* (* 0.5 (cos re)) t_0)))
   (if (<= im 31000.0)
     t_1
     (if (<= im 1e+77)
       (* t_0 (+ 0.5 (pow re -2.0)))
       (if (<= im 1.55e+152)
         (/ (+ 0.5 re) (/ (- 2.0 (* im im)) (- 4.0 (pow im 4.0))))
         t_1)))))
double code(double re, double im) {
	double t_0 = 2.0 + (im * im);
	double t_1 = (0.5 * cos(re)) * t_0;
	double tmp;
	if (im <= 31000.0) {
		tmp = t_1;
	} else if (im <= 1e+77) {
		tmp = t_0 * (0.5 + pow(re, -2.0));
	} else if (im <= 1.55e+152) {
		tmp = (0.5 + re) / ((2.0 - (im * im)) / (4.0 - pow(im, 4.0)));
	} else {
		tmp = 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 = 2.0d0 + (im * im)
    t_1 = (0.5d0 * cos(re)) * t_0
    if (im <= 31000.0d0) then
        tmp = t_1
    else if (im <= 1d+77) then
        tmp = t_0 * (0.5d0 + (re ** (-2.0d0)))
    else if (im <= 1.55d+152) then
        tmp = (0.5d0 + re) / ((2.0d0 - (im * im)) / (4.0d0 - (im ** 4.0d0)))
    else
        tmp = t_1
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 2.0 + (im * im);
	double t_1 = (0.5 * Math.cos(re)) * t_0;
	double tmp;
	if (im <= 31000.0) {
		tmp = t_1;
	} else if (im <= 1e+77) {
		tmp = t_0 * (0.5 + Math.pow(re, -2.0));
	} else if (im <= 1.55e+152) {
		tmp = (0.5 + re) / ((2.0 - (im * im)) / (4.0 - Math.pow(im, 4.0)));
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(re, im):
	t_0 = 2.0 + (im * im)
	t_1 = (0.5 * math.cos(re)) * t_0
	tmp = 0
	if im <= 31000.0:
		tmp = t_1
	elif im <= 1e+77:
		tmp = t_0 * (0.5 + math.pow(re, -2.0))
	elif im <= 1.55e+152:
		tmp = (0.5 + re) / ((2.0 - (im * im)) / (4.0 - math.pow(im, 4.0)))
	else:
		tmp = t_1
	return tmp
function code(re, im)
	t_0 = Float64(2.0 + Float64(im * im))
	t_1 = Float64(Float64(0.5 * cos(re)) * t_0)
	tmp = 0.0
	if (im <= 31000.0)
		tmp = t_1;
	elseif (im <= 1e+77)
		tmp = Float64(t_0 * Float64(0.5 + (re ^ -2.0)));
	elseif (im <= 1.55e+152)
		tmp = Float64(Float64(0.5 + re) / Float64(Float64(2.0 - Float64(im * im)) / Float64(4.0 - (im ^ 4.0))));
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 2.0 + (im * im);
	t_1 = (0.5 * cos(re)) * t_0;
	tmp = 0.0;
	if (im <= 31000.0)
		tmp = t_1;
	elseif (im <= 1e+77)
		tmp = t_0 * (0.5 + (re ^ -2.0));
	elseif (im <= 1.55e+152)
		tmp = (0.5 + re) / ((2.0 - (im * im)) / (4.0 - (im ^ 4.0)));
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(2.0 + N[(im * im), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * t$95$0), $MachinePrecision]}, If[LessEqual[im, 31000.0], t$95$1, If[LessEqual[im, 1e+77], N[(t$95$0 * N[(0.5 + N[Power[re, -2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.55e+152], N[(N[(0.5 + re), $MachinePrecision] / N[(N[(2.0 - N[(im * im), $MachinePrecision]), $MachinePrecision] / N[(4.0 - N[Power[im, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$1]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 2 + im \cdot im\\
t_1 := \left(0.5 \cdot \cos re\right) \cdot t_0\\
\mathbf{if}\;im \leq 31000:\\
\;\;\;\;t_1\\

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

\mathbf{elif}\;im \leq 1.55 \cdot 10^{+152}:\\
\;\;\;\;\frac{0.5 + re}{\frac{2 - im \cdot im}{4 - {im}^{4}}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if im < 31000 or 1.55e152 < 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 84.3%

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

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

    if 31000 < im < 9.99999999999999983e76

    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.25 \cdot \left({re}^{2} \cdot \left(e^{im} + e^{-im}\right)\right) + 0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Simplified91.7%

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

      \[\leadsto \color{blue}{\left(2 + {im}^{2}\right)} \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right) \]
    5. Simplified43.6%

      \[\leadsto \color{blue}{\left(2 + im \cdot im\right)} \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right) \]
    6. Applied egg-rr42.9%

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

    if 9.99999999999999983e76 < im < 1.55e152

    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.25 \cdot \left({re}^{2} \cdot \left(e^{im} + e^{-im}\right)\right) + 0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Simplified69.6%

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

      \[\leadsto \color{blue}{\left(2 + {im}^{2}\right)} \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right) \]
    5. Simplified24.8%

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

      \[\leadsto \left(2 + im \cdot im\right) \cdot \left(0.5 + \color{blue}{\left|re\right|}\right) \]
    7. Step-by-step derivation
      1. *-commutative25.4%

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

        \[\leadsto \left(0.5 + \left|re\right|\right) \cdot \color{blue}{\frac{2 \cdot 2 - \left(im \cdot im\right) \cdot \left(im \cdot im\right)}{2 - im \cdot im}} \]
      3. associate-*r/78.3%

        \[\leadsto \color{blue}{\frac{\left(0.5 + \left|re\right|\right) \cdot \left(2 \cdot 2 - \left(im \cdot im\right) \cdot \left(im \cdot im\right)\right)}{2 - im \cdot im}} \]
      4. add-sqr-sqrt43.5%

        \[\leadsto \frac{\left(0.5 + \left|\color{blue}{\sqrt{re} \cdot \sqrt{re}}\right|\right) \cdot \left(2 \cdot 2 - \left(im \cdot im\right) \cdot \left(im \cdot im\right)\right)}{2 - im \cdot im} \]
      5. fabs-sqr43.5%

        \[\leadsto \frac{\left(0.5 + \color{blue}{\sqrt{re} \cdot \sqrt{re}}\right) \cdot \left(2 \cdot 2 - \left(im \cdot im\right) \cdot \left(im \cdot im\right)\right)}{2 - im \cdot im} \]
      6. add-sqr-sqrt69.6%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\frac{\left(0.5 + re\right) \cdot \left(4 - {im}^{4}\right)}{2 - im \cdot im}} \]
    9. Step-by-step derivation
      1. associate-/l*69.6%

        \[\leadsto \color{blue}{\frac{0.5 + re}{\frac{2 - im \cdot im}{4 - {im}^{4}}}} \]
    10. Simplified69.6%

      \[\leadsto \color{blue}{\frac{0.5 + re}{\frac{2 - im \cdot im}{4 - {im}^{4}}}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification81.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 31000:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{elif}\;im \leq 10^{+77}:\\ \;\;\;\;\left(2 + im \cdot im\right) \cdot \left(0.5 + {re}^{-2}\right)\\ \mathbf{elif}\;im \leq 1.55 \cdot 10^{+152}:\\ \;\;\;\;\frac{0.5 + re}{\frac{2 - im \cdot im}{4 - {im}^{4}}}\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\right)\\ \end{array} \]

Alternative 5: 80.0% accurate, 2.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 2 + im \cdot im\\ \mathbf{if}\;im \leq 31000 \lor \neg \left(im \leq 1.55 \cdot 10^{+152}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot t_0\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(0.5 + {re}^{-2}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (+ 2.0 (* im im))))
   (if (or (<= im 31000.0) (not (<= im 1.55e+152)))
     (* (* 0.5 (cos re)) t_0)
     (* t_0 (+ 0.5 (pow re -2.0))))))
double code(double re, double im) {
	double t_0 = 2.0 + (im * im);
	double tmp;
	if ((im <= 31000.0) || !(im <= 1.55e+152)) {
		tmp = (0.5 * cos(re)) * t_0;
	} else {
		tmp = t_0 * (0.5 + pow(re, -2.0));
	}
	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 = 2.0d0 + (im * im)
    if ((im <= 31000.0d0) .or. (.not. (im <= 1.55d+152))) then
        tmp = (0.5d0 * cos(re)) * t_0
    else
        tmp = t_0 * (0.5d0 + (re ** (-2.0d0)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 2.0 + (im * im);
	double tmp;
	if ((im <= 31000.0) || !(im <= 1.55e+152)) {
		tmp = (0.5 * Math.cos(re)) * t_0;
	} else {
		tmp = t_0 * (0.5 + Math.pow(re, -2.0));
	}
	return tmp;
}
def code(re, im):
	t_0 = 2.0 + (im * im)
	tmp = 0
	if (im <= 31000.0) or not (im <= 1.55e+152):
		tmp = (0.5 * math.cos(re)) * t_0
	else:
		tmp = t_0 * (0.5 + math.pow(re, -2.0))
	return tmp
function code(re, im)
	t_0 = Float64(2.0 + Float64(im * im))
	tmp = 0.0
	if ((im <= 31000.0) || !(im <= 1.55e+152))
		tmp = Float64(Float64(0.5 * cos(re)) * t_0);
	else
		tmp = Float64(t_0 * Float64(0.5 + (re ^ -2.0)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 2.0 + (im * im);
	tmp = 0.0;
	if ((im <= 31000.0) || ~((im <= 1.55e+152)))
		tmp = (0.5 * cos(re)) * t_0;
	else
		tmp = t_0 * (0.5 + (re ^ -2.0));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(2.0 + N[(im * im), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[im, 31000.0], N[Not[LessEqual[im, 1.55e+152]], $MachinePrecision]], N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * t$95$0), $MachinePrecision], N[(t$95$0 * N[(0.5 + N[Power[re, -2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 2 + im \cdot im\\
\mathbf{if}\;im \leq 31000 \lor \neg \left(im \leq 1.55 \cdot 10^{+152}\right):\\
\;\;\;\;\left(0.5 \cdot \cos re\right) \cdot t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 31000 or 1.55e152 < 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 84.3%

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

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

    if 31000 < im < 1.55e152

    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.25 \cdot \left({re}^{2} \cdot \left(e^{im} + e^{-im}\right)\right) + 0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Simplified77.1%

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

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

      \[\leadsto \color{blue}{\left(2 + im \cdot im\right)} \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right) \]
    6. Applied egg-rr45.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 31000 \lor \neg \left(im \leq 1.55 \cdot 10^{+152}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;\left(2 + im \cdot im\right) \cdot \left(0.5 + {re}^{-2}\right)\\ \end{array} \]

Alternative 6: 77.8% accurate, 2.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 2 + im \cdot im\\ \mathbf{if}\;im \leq 610 \lor \neg \left(im \leq 1.32 \cdot 10^{+154}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \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 (+ 2.0 (* im im))))
   (if (or (<= im 610.0) (not (<= im 1.32e+154)))
     (* (* 0.5 (cos re)) t_0)
     (* t_0 (+ 0.5 (* -0.25 (* re re)))))))
double code(double re, double im) {
	double t_0 = 2.0 + (im * im);
	double tmp;
	if ((im <= 610.0) || !(im <= 1.32e+154)) {
		tmp = (0.5 * cos(re)) * 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 = 2.0d0 + (im * im)
    if ((im <= 610.0d0) .or. (.not. (im <= 1.32d+154))) then
        tmp = (0.5d0 * cos(re)) * 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 = 2.0 + (im * im);
	double tmp;
	if ((im <= 610.0) || !(im <= 1.32e+154)) {
		tmp = (0.5 * Math.cos(re)) * t_0;
	} else {
		tmp = t_0 * (0.5 + (-0.25 * (re * re)));
	}
	return tmp;
}
def code(re, im):
	t_0 = 2.0 + (im * im)
	tmp = 0
	if (im <= 610.0) or not (im <= 1.32e+154):
		tmp = (0.5 * math.cos(re)) * t_0
	else:
		tmp = t_0 * (0.5 + (-0.25 * (re * re)))
	return tmp
function code(re, im)
	t_0 = Float64(2.0 + Float64(im * im))
	tmp = 0.0
	if ((im <= 610.0) || !(im <= 1.32e+154))
		tmp = Float64(Float64(0.5 * cos(re)) * 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 = 2.0 + (im * im);
	tmp = 0.0;
	if ((im <= 610.0) || ~((im <= 1.32e+154)))
		tmp = (0.5 * cos(re)) * 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[(2.0 + N[(im * im), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[im, 610.0], N[Not[LessEqual[im, 1.32e+154]], $MachinePrecision]], N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * 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 := 2 + im \cdot im\\
\mathbf{if}\;im \leq 610 \lor \neg \left(im \leq 1.32 \cdot 10^{+154}\right):\\
\;\;\;\;\left(0.5 \cdot \cos re\right) \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 2 regimes
  2. if im < 610 or 1.31999999999999998e154 < 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 84.6%

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

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

    if 610 < im < 1.31999999999999998e154

    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.25 \cdot \left({re}^{2} \cdot \left(e^{im} + e^{-im}\right)\right) + 0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Simplified77.8%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 610 \lor \neg \left(im \leq 1.32 \cdot 10^{+154}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;\left(2 + im \cdot im\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \end{array} \]

Alternative 7: 62.6% accurate, 3.0× speedup?

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

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

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


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

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

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

    if 0.070000000000000007 < 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.25 \cdot \left({re}^{2} \cdot \left(e^{im} + e^{-im}\right)\right) + 0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Simplified80.3%

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

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

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

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

Alternative 8: 39.9% accurate, 20.4× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 6.2 \cdot 10^{-5}:\\
\;\;\;\;1\\

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


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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{2} \]

    if 6.20000000000000027e-5 < 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.25 \cdot \left({re}^{2} \cdot \left(e^{im} + e^{-im}\right)\right) + 0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Simplified80.3%

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

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

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

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

Alternative 9: 48.4% accurate, 23.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 112:\\
\;\;\;\;0.5 \cdot \left(2 + im \cdot im\right)\\

\mathbf{elif}\;im \leq 8.8 \cdot 10^{+104}:\\
\;\;\;\;\left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right) \cdot 512\\

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


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

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

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

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

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

    if 112 < im < 8.80000000000000002e104

    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.25 \cdot \left({re}^{2} \cdot \left(e^{im} + e^{-im}\right)\right) + 0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Simplified90.5%

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

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

    if 8.80000000000000002e104 < 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.25 \cdot \left({re}^{2} \cdot \left(e^{im} + e^{-im}\right)\right) + 0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Simplified75.6%

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

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

      \[\leadsto \color{blue}{\left(2 + im \cdot im\right)} \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right) \]
    6. Applied egg-rr61.2%

      \[\leadsto \left(2 + im \cdot im\right) \cdot \left(0.5 + \color{blue}{\left|re\right|}\right) \]
    7. Taylor expanded in im around inf 61.2%

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

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

        \[\leadsto \left(im \cdot im\right) \cdot \color{blue}{\left(\left|re\right| + 0.5\right)} \]
      3. distribute-lft-in61.2%

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(im \cdot im, \left|re\right|, \left(im \cdot im\right) \cdot 0.5\right)} \]
      5. rem-square-sqrt27.6%

        \[\leadsto \mathsf{fma}\left(im \cdot im, \left|\color{blue}{\sqrt{re} \cdot \sqrt{re}}\right|, \left(im \cdot im\right) \cdot 0.5\right) \]
      6. fabs-sqr27.6%

        \[\leadsto \mathsf{fma}\left(im \cdot im, \color{blue}{\sqrt{re} \cdot \sqrt{re}}, \left(im \cdot im\right) \cdot 0.5\right) \]
      7. rem-square-sqrt32.3%

        \[\leadsto \mathsf{fma}\left(im \cdot im, \color{blue}{re}, \left(im \cdot im\right) \cdot 0.5\right) \]
      8. fma-def32.3%

        \[\leadsto \color{blue}{\left(im \cdot im\right) \cdot re + \left(im \cdot im\right) \cdot 0.5} \]
      9. distribute-lft-in65.6%

        \[\leadsto \color{blue}{\left(im \cdot im\right) \cdot \left(re + 0.5\right)} \]
      10. +-commutative65.6%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 112:\\ \;\;\;\;0.5 \cdot \left(2 + im \cdot im\right)\\ \mathbf{elif}\;im \leq 8.8 \cdot 10^{+104}:\\ \;\;\;\;\left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right) \cdot 512\\ \mathbf{else}:\\ \;\;\;\;\left(im \cdot im\right) \cdot \left(0.5 + re\right)\\ \end{array} \]

Alternative 10: 48.4% accurate, 27.7× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 155:\\
\;\;\;\;0.5 \cdot \left(2 + im \cdot im\right)\\

\mathbf{elif}\;im \leq 1.45 \cdot 10^{+105}:\\
\;\;\;\;1 + \left(re \cdot re\right) \cdot -0.5\\

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


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

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

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

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

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

    if 155 < im < 1.45000000000000005e105

    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.25 \cdot \left({re}^{2} \cdot \left(e^{im} + e^{-im}\right)\right) + 0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Simplified90.5%

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

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

      \[\leadsto \color{blue}{\left(2 + im \cdot im\right)} \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right) \]
    6. Taylor expanded in im around 0 39.7%

      \[\leadsto \color{blue}{2 \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    7. Step-by-step derivation
      1. distribute-rgt-in39.7%

        \[\leadsto \color{blue}{0.5 \cdot 2 + \left(-0.25 \cdot {re}^{2}\right) \cdot 2} \]
      2. metadata-eval39.7%

        \[\leadsto \color{blue}{1} + \left(-0.25 \cdot {re}^{2}\right) \cdot 2 \]
      3. unpow239.7%

        \[\leadsto 1 + \left(-0.25 \cdot \color{blue}{\left(re \cdot re\right)}\right) \cdot 2 \]
      4. *-commutative39.7%

        \[\leadsto 1 + \color{blue}{\left(\left(re \cdot re\right) \cdot -0.25\right)} \cdot 2 \]
      5. associate-*l*39.7%

        \[\leadsto 1 + \color{blue}{\left(re \cdot re\right) \cdot \left(-0.25 \cdot 2\right)} \]
      6. metadata-eval39.7%

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

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

    if 1.45000000000000005e105 < 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.25 \cdot \left({re}^{2} \cdot \left(e^{im} + e^{-im}\right)\right) + 0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Simplified75.6%

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

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

      \[\leadsto \color{blue}{\left(2 + im \cdot im\right)} \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right) \]
    6. Applied egg-rr61.2%

      \[\leadsto \left(2 + im \cdot im\right) \cdot \left(0.5 + \color{blue}{\left|re\right|}\right) \]
    7. Taylor expanded in im around inf 61.2%

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

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

        \[\leadsto \left(im \cdot im\right) \cdot \color{blue}{\left(\left|re\right| + 0.5\right)} \]
      3. distribute-lft-in61.2%

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(im \cdot im, \left|re\right|, \left(im \cdot im\right) \cdot 0.5\right)} \]
      5. rem-square-sqrt27.6%

        \[\leadsto \mathsf{fma}\left(im \cdot im, \left|\color{blue}{\sqrt{re} \cdot \sqrt{re}}\right|, \left(im \cdot im\right) \cdot 0.5\right) \]
      6. fabs-sqr27.6%

        \[\leadsto \mathsf{fma}\left(im \cdot im, \color{blue}{\sqrt{re} \cdot \sqrt{re}}, \left(im \cdot im\right) \cdot 0.5\right) \]
      7. rem-square-sqrt32.3%

        \[\leadsto \mathsf{fma}\left(im \cdot im, \color{blue}{re}, \left(im \cdot im\right) \cdot 0.5\right) \]
      8. fma-def32.3%

        \[\leadsto \color{blue}{\left(im \cdot im\right) \cdot re + \left(im \cdot im\right) \cdot 0.5} \]
      9. distribute-lft-in65.6%

        \[\leadsto \color{blue}{\left(im \cdot im\right) \cdot \left(re + 0.5\right)} \]
      10. +-commutative65.6%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 155:\\ \;\;\;\;0.5 \cdot \left(2 + im \cdot im\right)\\ \mathbf{elif}\;im \leq 1.45 \cdot 10^{+105}:\\ \;\;\;\;1 + \left(re \cdot re\right) \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;\left(im \cdot im\right) \cdot \left(0.5 + re\right)\\ \end{array} \]

Alternative 11: 48.1% accurate, 34.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 78000000000000:\\
\;\;\;\;0.5 \cdot \left(2 + im \cdot im\right)\\

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


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

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

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

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

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

    if 7.8e13 < 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.25 \cdot \left({re}^{2} \cdot \left(e^{im} + e^{-im}\right)\right) + 0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Simplified80.0%

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

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

      \[\leadsto \color{blue}{\left(2 + im \cdot im\right)} \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right) \]
    6. Applied egg-rr44.7%

      \[\leadsto \left(2 + im \cdot im\right) \cdot \left(0.5 + \color{blue}{\left|re\right|}\right) \]
    7. Taylor expanded in im around inf 44.7%

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

        \[\leadsto \color{blue}{\left(im \cdot im\right)} \cdot \left(0.5 + \left|re\right|\right) \]
      2. +-commutative44.7%

        \[\leadsto \left(im \cdot im\right) \cdot \color{blue}{\left(\left|re\right| + 0.5\right)} \]
      3. distribute-lft-in44.7%

        \[\leadsto \color{blue}{\left(im \cdot im\right) \cdot \left|re\right| + \left(im \cdot im\right) \cdot 0.5} \]
      4. fma-def44.7%

        \[\leadsto \color{blue}{\mathsf{fma}\left(im \cdot im, \left|re\right|, \left(im \cdot im\right) \cdot 0.5\right)} \]
      5. rem-square-sqrt20.9%

        \[\leadsto \mathsf{fma}\left(im \cdot im, \left|\color{blue}{\sqrt{re} \cdot \sqrt{re}}\right|, \left(im \cdot im\right) \cdot 0.5\right) \]
      6. fabs-sqr20.9%

        \[\leadsto \mathsf{fma}\left(im \cdot im, \color{blue}{\sqrt{re} \cdot \sqrt{re}}, \left(im \cdot im\right) \cdot 0.5\right) \]
      7. rem-square-sqrt31.1%

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

        \[\leadsto \color{blue}{\left(im \cdot im\right) \cdot re + \left(im \cdot im\right) \cdot 0.5} \]
      9. distribute-lft-in54.1%

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

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

        \[\leadsto \color{blue}{\left(0.5 + re\right) \cdot \left(im \cdot im\right)} \]
    9. Simplified54.1%

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

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

Alternative 12: 46.5% accurate, 44.0× speedup?

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

\\
0.5 \cdot \left(2 + im \cdot 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. Taylor expanded in re around 0 60.3%

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

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

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

    \[\leadsto 0.5 \cdot \left(2 + im \cdot im\right) \]

Alternative 13: 3.9% accurate, 308.0× speedup?

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

\\
-1
\end{array}
Derivation
  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 60.3%

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

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

    \[\leadsto -1 \]

Alternative 14: 6.9% accurate, 308.0× speedup?

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

\\
0.0078125
\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 60.3%

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

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

    \[\leadsto 0.0078125 \]

Alternative 15: 7.7% 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 60.3%

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

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

    \[\leadsto 0.125 \]

Alternative 16: 8.1% 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 60.3%

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

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

    \[\leadsto 0.25 \]

Alternative 17: 8.4% accurate, 308.0× speedup?

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

\\
0.375
\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 60.3%

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

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

    \[\leadsto 0.375 \]

Alternative 18: 8.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 60.3%

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

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

    \[\leadsto 0.5 \]

Alternative 19: 9.2% 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 60.3%

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

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

    \[\leadsto 0.75 \]

Alternative 20: 28.6% 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 60.3%

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

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

    \[\leadsto 1 \]

Reproduce

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