math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 6.3s
Alternatives: 13
Speedup: 1.0×

Specification

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

\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 13 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 100.0% accurate, 1.0× speedup?

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

\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\end{array}

Alternative 1: 100.0% accurate, 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.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 0.02 \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(im \cdot im + 2\right)\\ \mathbf{else}:\\ \;\;\;\;\left(e^{-im} + e^{im}\right) \cdot \sqrt[3]{0.125}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= im 0.02) (not (<= im 1.35e+154)))
   (* (* 0.5 (cos re)) (+ (* im im) 2.0))
   (* (+ (exp (- im)) (exp im)) (cbrt 0.125))))
double code(double re, double im) {
	double tmp;
	if ((im <= 0.02) || !(im <= 1.35e+154)) {
		tmp = (0.5 * cos(re)) * ((im * im) + 2.0);
	} else {
		tmp = (exp(-im) + exp(im)) * cbrt(0.125);
	}
	return tmp;
}
public static double code(double re, double im) {
	double tmp;
	if ((im <= 0.02) || !(im <= 1.35e+154)) {
		tmp = (0.5 * Math.cos(re)) * ((im * im) + 2.0);
	} else {
		tmp = (Math.exp(-im) + Math.exp(im)) * Math.cbrt(0.125);
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if ((im <= 0.02) || !(im <= 1.35e+154))
		tmp = Float64(Float64(0.5 * cos(re)) * Float64(Float64(im * im) + 2.0));
	else
		tmp = Float64(Float64(exp(Float64(-im)) + exp(im)) * cbrt(0.125));
	end
	return tmp
end
code[re_, im_] := If[Or[LessEqual[im, 0.02], N[Not[LessEqual[im, 1.35e+154]], $MachinePrecision]], N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[(im * im), $MachinePrecision] + 2.0), $MachinePrecision]), $MachinePrecision], N[(N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision] * N[Power[0.125, 1/3], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 0.02 \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\
\;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(im \cdot im + 2\right)\\

\mathbf{else}:\\
\;\;\;\;\left(e^{-im} + e^{im}\right) \cdot \sqrt[3]{0.125}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 0.0200000000000000004 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 88.2%

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

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

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

    if 0.0200000000000000004 < 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 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. Step-by-step derivation
      1. associate-*r*0.0%

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

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

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

      \[\leadsto \color{blue}{\left(e^{im} + e^{-im}\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)} \]
    5. Step-by-step derivation
      1. add-cbrt-cube78.1%

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

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

        \[\leadsto \left(e^{im} + e^{-im}\right) \cdot \sqrt[3]{{\color{blue}{\left(-0.25 \cdot \left(re \cdot re\right) + 0.5\right)}}^{3}} \]
      4. fma-def78.1%

        \[\leadsto \left(e^{im} + e^{-im}\right) \cdot \sqrt[3]{{\color{blue}{\left(\mathsf{fma}\left(-0.25, re \cdot re, 0.5\right)\right)}}^{3}} \]
    6. Applied egg-rr78.1%

      \[\leadsto \left(e^{im} + e^{-im}\right) \cdot \color{blue}{\sqrt[3]{{\left(\mathsf{fma}\left(-0.25, re \cdot re, 0.5\right)\right)}^{3}}} \]
    7. Taylor expanded in re around 0 72.6%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 0.02 \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(im \cdot im + 2\right)\\ \mathbf{else}:\\ \;\;\;\;\left(e^{-im} + e^{im}\right) \cdot \sqrt[3]{0.125}\\ \end{array} \]

Alternative 3: 84.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \cos re\\ \mathbf{if}\;im \leq 0.105:\\ \;\;\;\;\mathsf{fma}\left(im \cdot im, t_0, \cos re\right)\\ \mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\ \;\;\;\;\left(e^{-im} + e^{im}\right) \cdot \sqrt[3]{0.125}\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(im \cdot im + 2\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (cos re))))
   (if (<= im 0.105)
     (fma (* im im) t_0 (cos re))
     (if (<= im 1.35e+154)
       (* (+ (exp (- im)) (exp im)) (cbrt 0.125))
       (* t_0 (+ (* im im) 2.0))))))
double code(double re, double im) {
	double t_0 = 0.5 * cos(re);
	double tmp;
	if (im <= 0.105) {
		tmp = fma((im * im), t_0, cos(re));
	} else if (im <= 1.35e+154) {
		tmp = (exp(-im) + exp(im)) * cbrt(0.125);
	} else {
		tmp = t_0 * ((im * im) + 2.0);
	}
	return tmp;
}
function code(re, im)
	t_0 = Float64(0.5 * cos(re))
	tmp = 0.0
	if (im <= 0.105)
		tmp = fma(Float64(im * im), t_0, cos(re));
	elseif (im <= 1.35e+154)
		tmp = Float64(Float64(exp(Float64(-im)) + exp(im)) * cbrt(0.125));
	else
		tmp = Float64(t_0 * Float64(Float64(im * im) + 2.0));
	end
	return tmp
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[im, 0.105], N[(N[(im * im), $MachinePrecision] * t$95$0 + N[Cos[re], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.35e+154], N[(N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision] * N[Power[0.125, 1/3], $MachinePrecision]), $MachinePrecision], N[(t$95$0 * N[(N[(im * im), $MachinePrecision] + 2.0), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

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

\mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\
\;\;\;\;\left(e^{-im} + e^{im}\right) \cdot \sqrt[3]{0.125}\\

\mathbf{else}:\\
\;\;\;\;t_0 \cdot \left(im \cdot im + 2\right)\\


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

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

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

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

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

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

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

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

    if 0.104999999999999996 < 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 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. Step-by-step derivation
      1. associate-*r*0.0%

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

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

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

      \[\leadsto \color{blue}{\left(e^{im} + e^{-im}\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)} \]
    5. Step-by-step derivation
      1. add-cbrt-cube78.1%

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

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

        \[\leadsto \left(e^{im} + e^{-im}\right) \cdot \sqrt[3]{{\color{blue}{\left(-0.25 \cdot \left(re \cdot re\right) + 0.5\right)}}^{3}} \]
      4. fma-def78.1%

        \[\leadsto \left(e^{im} + e^{-im}\right) \cdot \sqrt[3]{{\color{blue}{\left(\mathsf{fma}\left(-0.25, re \cdot re, 0.5\right)\right)}}^{3}} \]
    6. Applied egg-rr78.1%

      \[\leadsto \left(e^{im} + e^{-im}\right) \cdot \color{blue}{\sqrt[3]{{\left(\mathsf{fma}\left(-0.25, re \cdot re, 0.5\right)\right)}^{3}}} \]
    7. Taylor expanded in re around 0 72.6%

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

    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 \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\left(2 + {im}^{2}\right)} \]
    3. Step-by-step derivation
      1. unpow2100.0%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 0.105:\\ \;\;\;\;\mathsf{fma}\left(im \cdot im, 0.5 \cdot \cos re, \cos re\right)\\ \mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\ \;\;\;\;\left(e^{-im} + e^{im}\right) \cdot \sqrt[3]{0.125}\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(im \cdot im + 2\right)\\ \end{array} \]

Alternative 4: 84.8% accurate, 1.5× speedup?

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

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

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

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

    if 0.017000000000000001 < 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 72.6%

      \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Step-by-step derivation
      1. *-commutative72.6%

        \[\leadsto \color{blue}{\left(e^{im} + e^{-im}\right) \cdot 0.5} \]
    4. Simplified72.6%

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

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

Alternative 5: 81.5% accurate, 2.5× speedup?

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

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

\mathbf{elif}\;im \leq 1.3 \cdot 10^{+97}:\\
\;\;\;\;t_1 \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\

\mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\
\;\;\;\;0.5 \cdot t_1\\

\mathbf{else}:\\
\;\;\;\;t_2\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if im < 7.7e12 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 87.5%

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

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

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

    if 7.7e12 < im < 1.3e97

    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. Step-by-step derivation
      1. associate-*r*0.0%

        \[\leadsto 0.5 \cdot \left(e^{im} + e^{-im}\right) + \color{blue}{\left(-0.25 \cdot {re}^{2}\right) \cdot \left(e^{im} + e^{-im}\right)} \]
      2. distribute-rgt-out88.9%

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

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

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

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

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

    if 1.3e97 < 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 75.0%

      \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Step-by-step derivation
      1. *-commutative75.0%

        \[\leadsto \color{blue}{\left(e^{im} + e^{-im}\right) \cdot 0.5} \]
    4. Simplified75.0%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 7700000000000:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(im \cdot im + 2\right)\\ \mathbf{elif}\;im \leq 1.3 \cdot 10^{+97}:\\ \;\;\;\;\left(\left(im \cdot im + 2\right) + 0.08333333333333333 \cdot {im}^{4}\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\ \;\;\;\;0.5 \cdot \left(\left(im \cdot im + 2\right) + 0.08333333333333333 \cdot {im}^{4}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(im \cdot im + 2\right)\\ \end{array} \]

Alternative 6: 80.6% accurate, 2.7× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(t_0 + 0.08333333333333333 \cdot {im}^{4}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 1.8499999999999999e25 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 86.4%

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

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

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

    if 1.8499999999999999e25 < 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 74.1%

      \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Step-by-step derivation
      1. *-commutative74.1%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1.85 \cdot 10^{+25} \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(im \cdot im + 2\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\left(im \cdot im + 2\right) + 0.08333333333333333 \cdot {im}^{4}\right)\\ \end{array} \]

Alternative 7: 76.3% accurate, 2.8× speedup?

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

\\
\left(0.5 \cdot \cos re\right) \cdot \left(im \cdot im + 2\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 im around 0 77.9%

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

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

    \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\left(2 + im \cdot im\right)} \]
  5. Final simplification77.9%

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

Alternative 8: 62.2% accurate, 3.0× speedup?

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

\\
\begin{array}{l}
t_0 := im \cdot im + 2\\
\mathbf{if}\;im \leq 10500000000000:\\
\;\;\;\;\cos re\\

\mathbf{elif}\;im \leq 5 \cdot 10^{+270} \lor \neg \left(im \leq 4 \cdot 10^{+289}\right):\\
\;\;\;\;t_0 \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot t_0\\


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

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

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

    if 1.05e13 < im < 4.99999999999999976e270 or 4.0000000000000002e289 < 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. Step-by-step derivation
      1. associate-*r*0.0%

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

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

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

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

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

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

    if 4.99999999999999976e270 < im < 4.0000000000000002e289

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

      \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Step-by-step derivation
      1. *-commutative100.0%

        \[\leadsto \color{blue}{\left(e^{im} + e^{-im}\right) \cdot 0.5} \]
    4. Simplified100.0%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 10500000000000:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 5 \cdot 10^{+270} \lor \neg \left(im \leq 4 \cdot 10^{+289}\right):\\ \;\;\;\;\left(im \cdot im + 2\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot im + 2\right)\\ \end{array} \]

Alternative 9: 48.0% accurate, 20.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := re \cdot \left(re \cdot -0.25\right)\\ t_1 := im \cdot im + 2\\ \mathbf{if}\;re \leq 1080000000000:\\ \;\;\;\;0.5 \cdot t_1\\ \mathbf{elif}\;re \leq 5.5 \cdot 10^{+165}:\\ \;\;\;\;t_1 \cdot t_0\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot t_0\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* re (* re -0.25))) (t_1 (+ (* im im) 2.0)))
   (if (<= re 1080000000000.0)
     (* 0.5 t_1)
     (if (<= re 5.5e+165) (* t_1 t_0) (* -2.0 t_0)))))
double code(double re, double im) {
	double t_0 = re * (re * -0.25);
	double t_1 = (im * im) + 2.0;
	double tmp;
	if (re <= 1080000000000.0) {
		tmp = 0.5 * t_1;
	} else if (re <= 5.5e+165) {
		tmp = t_1 * t_0;
	} else {
		tmp = -2.0 * t_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) :: t_1
    real(8) :: tmp
    t_0 = re * (re * (-0.25d0))
    t_1 = (im * im) + 2.0d0
    if (re <= 1080000000000.0d0) then
        tmp = 0.5d0 * t_1
    else if (re <= 5.5d+165) then
        tmp = t_1 * t_0
    else
        tmp = (-2.0d0) * t_0
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = re * (re * -0.25);
	double t_1 = (im * im) + 2.0;
	double tmp;
	if (re <= 1080000000000.0) {
		tmp = 0.5 * t_1;
	} else if (re <= 5.5e+165) {
		tmp = t_1 * t_0;
	} else {
		tmp = -2.0 * t_0;
	}
	return tmp;
}
def code(re, im):
	t_0 = re * (re * -0.25)
	t_1 = (im * im) + 2.0
	tmp = 0
	if re <= 1080000000000.0:
		tmp = 0.5 * t_1
	elif re <= 5.5e+165:
		tmp = t_1 * t_0
	else:
		tmp = -2.0 * t_0
	return tmp
function code(re, im)
	t_0 = Float64(re * Float64(re * -0.25))
	t_1 = Float64(Float64(im * im) + 2.0)
	tmp = 0.0
	if (re <= 1080000000000.0)
		tmp = Float64(0.5 * t_1);
	elseif (re <= 5.5e+165)
		tmp = Float64(t_1 * t_0);
	else
		tmp = Float64(-2.0 * t_0);
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = re * (re * -0.25);
	t_1 = (im * im) + 2.0;
	tmp = 0.0;
	if (re <= 1080000000000.0)
		tmp = 0.5 * t_1;
	elseif (re <= 5.5e+165)
		tmp = t_1 * t_0;
	else
		tmp = -2.0 * t_0;
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(re * N[(re * -0.25), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(im * im), $MachinePrecision] + 2.0), $MachinePrecision]}, If[LessEqual[re, 1080000000000.0], N[(0.5 * t$95$1), $MachinePrecision], If[LessEqual[re, 5.5e+165], N[(t$95$1 * t$95$0), $MachinePrecision], N[(-2.0 * t$95$0), $MachinePrecision]]]]]
\begin{array}{l}

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

\mathbf{elif}\;re \leq 5.5 \cdot 10^{+165}:\\
\;\;\;\;t_1 \cdot t_0\\

\mathbf{else}:\\
\;\;\;\;-2 \cdot t_0\\


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

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

      \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    3. Step-by-step derivation
      1. *-commutative76.9%

        \[\leadsto \color{blue}{\left(e^{im} + e^{-im}\right) \cdot 0.5} \]
    4. Simplified76.9%

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

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

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

    if 1.08e12 < re < 5.4999999999999998e165

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

      \[\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. Step-by-step derivation
      1. associate-*r*1.1%

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

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

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

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

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

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

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

        \[\leadsto -2 \cdot \left(-0.25 \cdot \color{blue}{\left(re \cdot re\right)}\right) \]
      2. associate-*r*2.1%

        \[\leadsto -2 \cdot \color{blue}{\left(\left(-0.25 \cdot re\right) \cdot re\right)} \]
      3. *-commutative2.1%

        \[\leadsto -2 \cdot \color{blue}{\left(re \cdot \left(-0.25 \cdot re\right)\right)} \]
    9. Simplified38.5%

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

    if 5.4999999999999998e165 < re

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

      \[\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. Step-by-step derivation
      1. associate-*r*0.6%

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

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

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

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

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

      \[\leadsto -2 \cdot \color{blue}{\left(-0.25 \cdot {re}^{2}\right)} \]
    7. Step-by-step derivation
      1. unpow226.5%

        \[\leadsto -2 \cdot \left(-0.25 \cdot \color{blue}{\left(re \cdot re\right)}\right) \]
      2. associate-*r*26.5%

        \[\leadsto -2 \cdot \color{blue}{\left(\left(-0.25 \cdot re\right) \cdot re\right)} \]
      3. *-commutative26.5%

        \[\leadsto -2 \cdot \color{blue}{\left(re \cdot \left(-0.25 \cdot re\right)\right)} \]
    8. Simplified26.5%

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

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

Alternative 10: 49.5% accurate, 20.4× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq 5.5 \cdot 10^{+165}:\\
\;\;\;\;\left(im \cdot im + 2\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\

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


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

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

      \[\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. Step-by-step derivation
      1. associate-*r*28.3%

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

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

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

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

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

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

    if 5.4999999999999998e165 < re

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

      \[\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. Step-by-step derivation
      1. associate-*r*0.6%

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

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

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

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

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

      \[\leadsto -2 \cdot \color{blue}{\left(-0.25 \cdot {re}^{2}\right)} \]
    7. Step-by-step derivation
      1. unpow226.5%

        \[\leadsto -2 \cdot \left(-0.25 \cdot \color{blue}{\left(re \cdot re\right)}\right) \]
      2. associate-*r*26.5%

        \[\leadsto -2 \cdot \color{blue}{\left(\left(-0.25 \cdot re\right) \cdot re\right)} \]
      3. *-commutative26.5%

        \[\leadsto -2 \cdot \color{blue}{\left(re \cdot \left(-0.25 \cdot re\right)\right)} \]
    8. Simplified26.5%

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

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

Alternative 11: 31.3% accurate, 34.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 3600000000:\\
\;\;\;\;1\\

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


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

    1. Initial program 100.0%

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

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

        \[\leadsto \frac{-2 \cdot \cos re}{-2 \cdot \cos re + \color{blue}{0}} \]
      2. +-rgt-identity37.3%

        \[\leadsto \frac{-2 \cdot \cos re}{\color{blue}{-2 \cdot \cos re}} \]
      3. *-inverses37.3%

        \[\leadsto \color{blue}{1} \]
    4. Simplified37.3%

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

    if 3.6e9 < 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. Step-by-step derivation
      1. associate-*r*0.0%

        \[\leadsto 0.5 \cdot \left(e^{im} + e^{-im}\right) + \color{blue}{\left(-0.25 \cdot {re}^{2}\right) \cdot \left(e^{im} + e^{-im}\right)} \]
      2. distribute-rgt-out76.7%

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

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

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

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

      \[\leadsto -2 \cdot \color{blue}{\left(-0.25 \cdot {re}^{2}\right)} \]
    7. Step-by-step derivation
      1. unpow216.6%

        \[\leadsto -2 \cdot \left(-0.25 \cdot \color{blue}{\left(re \cdot re\right)}\right) \]
      2. associate-*r*16.6%

        \[\leadsto -2 \cdot \color{blue}{\left(\left(-0.25 \cdot re\right) \cdot re\right)} \]
      3. *-commutative16.6%

        \[\leadsto -2 \cdot \color{blue}{\left(re \cdot \left(-0.25 \cdot re\right)\right)} \]
    8. Simplified16.6%

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

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

Alternative 12: 47.5% accurate, 44.0× speedup?

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

\\
0.5 \cdot \left(im \cdot im + 2\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 65.4%

    \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
  3. Step-by-step derivation
    1. *-commutative65.4%

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

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

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

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

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

Alternative 13: 28.7% 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. Applied egg-rr29.1%

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

      \[\leadsto \frac{-2 \cdot \cos re}{-2 \cdot \cos re + \color{blue}{0}} \]
    2. +-rgt-identity29.1%

      \[\leadsto \frac{-2 \cdot \cos re}{\color{blue}{-2 \cdot \cos re}} \]
    3. *-inverses29.1%

      \[\leadsto \color{blue}{1} \]
  4. Simplified29.1%

    \[\leadsto \color{blue}{1} \]
  5. Final simplification29.1%

    \[\leadsto 1 \]

Reproduce

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