math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 8.7s
Alternatives: 15
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 15 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: 93.0% accurate, 1.4× speedup?

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

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

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

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


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

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

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

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

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

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

    if 0.94999999999999996 < im < 4.00000000000000018e48

    1. Initial program 99.8%

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

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

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

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

    if 4.00000000000000018e48 < 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 + \left({im}^{2} + \left(0.08333333333333333 \cdot {im}^{4} + 0.002777777777777778 \cdot {im}^{6}\right)\right)\right)} \]
    3. Step-by-step derivation
      1. associate-+r+100.0%

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

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

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

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

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

        \[\leadsto \color{blue}{\left(\cos re \cdot {im}^{6}\right) \cdot 0.001388888888888889} \]
      2. *-commutative100.0%

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

        \[\leadsto \color{blue}{{im}^{6} \cdot \left(\cos re \cdot 0.001388888888888889\right)} \]
    7. Simplified100.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 0.95:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(\left(2 + im \cdot im\right) + 0.08333333333333333 \cdot {im}^{4}\right)\\ \mathbf{elif}\;im \leq 4 \cdot 10^{+48}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \mathbf{else}:\\ \;\;\;\;{im}^{6} \cdot \left(\cos re \cdot 0.001388888888888889\right)\\ \end{array} \]

Alternative 3: 72.4% accurate, 1.5× speedup?

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

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

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

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


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

    1. Initial program 100.0%

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

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

    if 5.60000000000000031e-10 < im < 2.0499999999999999e152

    1. Initial program 99.8%

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

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

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

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

    if 2.0499999999999999e152 < 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 97.0%

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

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

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

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

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

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

        \[\leadsto \color{blue}{\left(im \cdot im\right)} \cdot \left(0.5 \cdot \cos re\right) \]
      4. associate-*l*97.0%

        \[\leadsto \color{blue}{im \cdot \left(im \cdot \left(0.5 \cdot \cos re\right)\right)} \]
    7. Simplified97.0%

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

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

Alternative 4: 74.6% accurate, 1.5× speedup?

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

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

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

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


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

    1. Initial program 100.0%

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

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

    if 5.60000000000000031e-10 < im < 4.00000000000000018e48

    1. Initial program 99.6%

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

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

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

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

    if 4.00000000000000018e48 < 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 + \left({im}^{2} + \left(0.08333333333333333 \cdot {im}^{4} + 0.002777777777777778 \cdot {im}^{6}\right)\right)\right)} \]
    3. Step-by-step derivation
      1. associate-+r+100.0%

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

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

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

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

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

        \[\leadsto \color{blue}{\left(\cos re \cdot {im}^{6}\right) \cdot 0.001388888888888889} \]
      2. *-commutative100.0%

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

        \[\leadsto \color{blue}{{im}^{6} \cdot \left(\cos re \cdot 0.001388888888888889\right)} \]
    7. Simplified100.0%

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

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

Alternative 5: 78.8% accurate, 2.5× speedup?

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

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

\mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\
\;\;\;\;\mathsf{fma}\left(0.5, t_1, -0.25 \cdot \left(t_1 \cdot \left(re \cdot re\right)\right)\right)\\

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


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

    1. Initial program 100.0%

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

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

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

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

    if 1020 < im < 1.35000000000000003e154

    1. Initial program 100.0%

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + \color{blue}{im \cdot im}, -0.25 \cdot \left(\left(2 + {im}^{2}\right) \cdot {re}^{2}\right)\right) \]
      3. *-commutative25.0%

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + im \cdot im, -0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(2 + \color{blue}{im \cdot im}\right)\right)\right) \]
    7. Simplified25.0%

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

    if 1.35000000000000003e154 < im

    1. Initial program 100.0%

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

      \[\leadsto \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)} \]
    5. Taylor expanded in im around inf 100.0%

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

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

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

        \[\leadsto \color{blue}{\left(im \cdot im\right)} \cdot \left(0.5 \cdot \cos re\right) \]
      4. associate-*l*100.0%

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

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

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

Alternative 6: 66.1% accurate, 2.7× speedup?

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

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

\mathbf{elif}\;im \leq 1.95 \cdot 10^{+154}:\\
\;\;\;\;im \cdot \left(im \cdot \mathsf{fma}\left(-0.25, re \cdot re, 0.5\right)\right)\\

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


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

    1. Initial program 100.0%

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

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

    if 1020 < im < 1.9500000000000001e154

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + \color{blue}{im \cdot im}, -0.25 \cdot \left(\left(2 + {im}^{2}\right) \cdot {re}^{2}\right)\right) \]
      3. *-commutative25.0%

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + im \cdot im, -0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(2 + \color{blue}{im \cdot im}\right)\right)\right) \]
    7. Simplified25.0%

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

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

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

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

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

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

        \[\leadsto im \cdot \left(im \cdot \color{blue}{\left(-0.25 \cdot \left(re \cdot re\right) + 0.5\right)}\right) \]
      6. fma-def25.0%

        \[\leadsto im \cdot \left(im \cdot \color{blue}{\mathsf{fma}\left(-0.25, re \cdot re, 0.5\right)}\right) \]
    10. Simplified25.0%

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

    if 1.9500000000000001e154 < 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)} \]
    5. Taylor expanded in im around inf 100.0%

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

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

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

        \[\leadsto \color{blue}{\left(im \cdot im\right)} \cdot \left(0.5 \cdot \cos re\right) \]
      4. associate-*l*100.0%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1020:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.95 \cdot 10^{+154}:\\ \;\;\;\;im \cdot \left(im \cdot \mathsf{fma}\left(-0.25, re \cdot re, 0.5\right)\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(\left(0.5 \cdot \cos re\right) \cdot im\right)\\ \end{array} \]

Alternative 7: 78.8% accurate, 2.7× speedup?

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

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

\mathbf{elif}\;im \leq 2.7 \cdot 10^{+154}:\\
\;\;\;\;im \cdot \left(im \cdot \mathsf{fma}\left(-0.25, re \cdot re, 0.5\right)\right)\\

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


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

    1. Initial program 100.0%

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

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

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

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

    if 1100 < im < 2.70000000000000006e154

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + \color{blue}{im \cdot im}, -0.25 \cdot \left(\left(2 + {im}^{2}\right) \cdot {re}^{2}\right)\right) \]
      3. *-commutative25.0%

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + im \cdot im, -0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(2 + \color{blue}{im \cdot im}\right)\right)\right) \]
    7. Simplified25.0%

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

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

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

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

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

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

        \[\leadsto im \cdot \left(im \cdot \color{blue}{\left(-0.25 \cdot \left(re \cdot re\right) + 0.5\right)}\right) \]
      6. fma-def25.0%

        \[\leadsto im \cdot \left(im \cdot \color{blue}{\mathsf{fma}\left(-0.25, re \cdot re, 0.5\right)}\right) \]
    10. Simplified25.0%

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

    if 2.70000000000000006e154 < 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)} \]
    5. Taylor expanded in im around inf 100.0%

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

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

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

        \[\leadsto \color{blue}{\left(im \cdot im\right)} \cdot \left(0.5 \cdot \cos re\right) \]
      4. associate-*l*100.0%

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

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

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

Alternative 8: 66.1% accurate, 2.8× speedup?

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

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

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

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


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

    1. Initial program 100.0%

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

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

    if 2800 < im < 2.5600000000000001e154

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + \color{blue}{im \cdot im}, -0.25 \cdot \left(\left(2 + {im}^{2}\right) \cdot {re}^{2}\right)\right) \]
      3. *-commutative25.0%

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + im \cdot im, -0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(2 + \color{blue}{im \cdot im}\right)\right)\right) \]
    7. Simplified25.0%

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

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

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

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

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

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

    if 2.5600000000000001e154 < 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)} \]
    5. Taylor expanded in im around inf 100.0%

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

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

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

        \[\leadsto \color{blue}{\left(im \cdot im\right)} \cdot \left(0.5 \cdot \cos re\right) \]
      4. associate-*l*100.0%

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

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

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

Alternative 9: 63.1% accurate, 3.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 1020:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 2 \cdot 10^{+154} \lor \neg \left(im \leq 5 \cdot 10^{+240}\right) \land im \leq 1.16 \cdot 10^{+275}:\\ \;\;\;\;\left(im \cdot im\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \mathbf{else}:\\ \;\;\;\;1 + 0.5 \cdot \left(im \cdot im\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 1020.0)
   (cos re)
   (if (or (<= im 2e+154) (and (not (<= im 5e+240)) (<= im 1.16e+275)))
     (* (* im im) (+ 0.5 (* -0.25 (* re re))))
     (+ 1.0 (* 0.5 (* im im))))))
double code(double re, double im) {
	double tmp;
	if (im <= 1020.0) {
		tmp = cos(re);
	} else if ((im <= 2e+154) || (!(im <= 5e+240) && (im <= 1.16e+275))) {
		tmp = (im * im) * (0.5 + (-0.25 * (re * re)));
	} else {
		tmp = 1.0 + (0.5 * (im * im));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 1020.0d0) then
        tmp = cos(re)
    else if ((im <= 2d+154) .or. (.not. (im <= 5d+240)) .and. (im <= 1.16d+275)) then
        tmp = (im * im) * (0.5d0 + ((-0.25d0) * (re * re)))
    else
        tmp = 1.0d0 + (0.5d0 * (im * im))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 1020.0) {
		tmp = Math.cos(re);
	} else if ((im <= 2e+154) || (!(im <= 5e+240) && (im <= 1.16e+275))) {
		tmp = (im * im) * (0.5 + (-0.25 * (re * re)));
	} else {
		tmp = 1.0 + (0.5 * (im * im));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 1020.0:
		tmp = math.cos(re)
	elif (im <= 2e+154) or (not (im <= 5e+240) and (im <= 1.16e+275)):
		tmp = (im * im) * (0.5 + (-0.25 * (re * re)))
	else:
		tmp = 1.0 + (0.5 * (im * im))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 1020.0)
		tmp = cos(re);
	elseif ((im <= 2e+154) || (!(im <= 5e+240) && (im <= 1.16e+275)))
		tmp = Float64(Float64(im * im) * Float64(0.5 + Float64(-0.25 * Float64(re * re))));
	else
		tmp = Float64(1.0 + Float64(0.5 * Float64(im * im)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 1020.0)
		tmp = cos(re);
	elseif ((im <= 2e+154) || (~((im <= 5e+240)) && (im <= 1.16e+275)))
		tmp = (im * im) * (0.5 + (-0.25 * (re * re)));
	else
		tmp = 1.0 + (0.5 * (im * im));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 1020.0], N[Cos[re], $MachinePrecision], If[Or[LessEqual[im, 2e+154], And[N[Not[LessEqual[im, 5e+240]], $MachinePrecision], LessEqual[im, 1.16e+275]]], N[(N[(im * im), $MachinePrecision] * N[(0.5 + N[(-0.25 * N[(re * re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(0.5 * N[(im * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;im \leq 2 \cdot 10^{+154} \lor \neg \left(im \leq 5 \cdot 10^{+240}\right) \land im \leq 1.16 \cdot 10^{+275}:\\
\;\;\;\;\left(im \cdot im\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\

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


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

    1. Initial program 100.0%

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

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

    if 1020 < im < 2.00000000000000007e154 or 5.0000000000000003e240 < im < 1.16e275

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + \color{blue}{im \cdot im}, -0.25 \cdot \left(\left(2 + {im}^{2}\right) \cdot {re}^{2}\right)\right) \]
      3. *-commutative18.0%

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + im \cdot im, -0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(2 + \color{blue}{im \cdot im}\right)\right)\right) \]
    7. Simplified18.0%

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

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

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

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

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

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

    if 2.00000000000000007e154 < im < 5.0000000000000003e240 or 1.16e275 < 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)} \]
    5. Taylor expanded in re around 0 84.2%

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

        \[\leadsto \color{blue}{2 \cdot 0.5 + {im}^{2} \cdot 0.5} \]
      2. metadata-eval84.2%

        \[\leadsto \color{blue}{1} + {im}^{2} \cdot 0.5 \]
      3. unpow284.2%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1020:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 2 \cdot 10^{+154} \lor \neg \left(im \leq 5 \cdot 10^{+240}\right) \land im \leq 1.16 \cdot 10^{+275}:\\ \;\;\;\;\left(im \cdot im\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \mathbf{else}:\\ \;\;\;\;1 + 0.5 \cdot \left(im \cdot im\right)\\ \end{array} \]

Alternative 10: 50.1% accurate, 16.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 1100 \lor \neg \left(im \leq 1.5 \cdot 10^{+154} \lor \neg \left(im \leq 3.2 \cdot 10^{+241}\right) \land im \leq 10^{+275}\right):\\ \;\;\;\;1 + 0.5 \cdot \left(im \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;\left(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 (or (<= im 1100.0)
         (not
          (or (<= im 1.5e+154) (and (not (<= im 3.2e+241)) (<= im 1e+275)))))
   (+ 1.0 (* 0.5 (* im im)))
   (* (* im im) (+ 0.5 (* -0.25 (* re re))))))
double code(double re, double im) {
	double tmp;
	if ((im <= 1100.0) || !((im <= 1.5e+154) || (!(im <= 3.2e+241) && (im <= 1e+275)))) {
		tmp = 1.0 + (0.5 * (im * im));
	} else {
		tmp = (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 <= 1100.0d0) .or. (.not. (im <= 1.5d+154) .or. (.not. (im <= 3.2d+241)) .and. (im <= 1d+275))) then
        tmp = 1.0d0 + (0.5d0 * (im * im))
    else
        tmp = (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 <= 1100.0) || !((im <= 1.5e+154) || (!(im <= 3.2e+241) && (im <= 1e+275)))) {
		tmp = 1.0 + (0.5 * (im * im));
	} else {
		tmp = (im * im) * (0.5 + (-0.25 * (re * re)));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (im <= 1100.0) or not ((im <= 1.5e+154) or (not (im <= 3.2e+241) and (im <= 1e+275))):
		tmp = 1.0 + (0.5 * (im * im))
	else:
		tmp = (im * im) * (0.5 + (-0.25 * (re * re)))
	return tmp
function code(re, im)
	tmp = 0.0
	if ((im <= 1100.0) || !((im <= 1.5e+154) || (!(im <= 3.2e+241) && (im <= 1e+275))))
		tmp = Float64(1.0 + Float64(0.5 * Float64(im * im)));
	else
		tmp = Float64(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 <= 1100.0) || ~(((im <= 1.5e+154) || (~((im <= 3.2e+241)) && (im <= 1e+275)))))
		tmp = 1.0 + (0.5 * (im * im));
	else
		tmp = (im * im) * (0.5 + (-0.25 * (re * re)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Or[LessEqual[im, 1100.0], N[Not[Or[LessEqual[im, 1.5e+154], And[N[Not[LessEqual[im, 3.2e+241]], $MachinePrecision], LessEqual[im, 1e+275]]]], $MachinePrecision]], N[(1.0 + N[(0.5 * N[(im * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(im * im), $MachinePrecision] * N[(0.5 + N[(-0.25 * N[(re * re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 1100 \lor \neg \left(im \leq 1.5 \cdot 10^{+154} \lor \neg \left(im \leq 3.2 \cdot 10^{+241}\right) \land im \leq 10^{+275}\right):\\
\;\;\;\;1 + 0.5 \cdot \left(im \cdot im\right)\\

\mathbf{else}:\\
\;\;\;\;\left(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 < 1100 or 1.50000000000000013e154 < im < 3.20000000000000004e241 or 9.9999999999999996e274 < 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 82.6%

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

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

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

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

        \[\leadsto \color{blue}{2 \cdot 0.5 + {im}^{2} \cdot 0.5} \]
      2. metadata-eval49.1%

        \[\leadsto \color{blue}{1} + {im}^{2} \cdot 0.5 \]
      3. unpow249.1%

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

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

    if 1100 < im < 1.50000000000000013e154 or 3.20000000000000004e241 < im < 9.9999999999999996e274

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + \color{blue}{im \cdot im}, -0.25 \cdot \left(\left(2 + {im}^{2}\right) \cdot {re}^{2}\right)\right) \]
      3. *-commutative18.0%

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + im \cdot im, -0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(2 + \color{blue}{im \cdot im}\right)\right)\right) \]
    7. Simplified18.0%

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

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

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

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

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

      \[\leadsto \color{blue}{\left(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 simplification48.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1100 \lor \neg \left(im \leq 1.5 \cdot 10^{+154} \lor \neg \left(im \leq 3.2 \cdot 10^{+241}\right) \land im \leq 10^{+275}\right):\\ \;\;\;\;1 + 0.5 \cdot \left(im \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;\left(im \cdot im\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \end{array} \]

Alternative 11: 49.3% accurate, 23.4× speedup?

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

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

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


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

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot \left(2 + {im}^{2}\right)} \]
    6. Step-by-step derivation
      1. distribute-rgt-in50.3%

        \[\leadsto \color{blue}{2 \cdot 0.5 + {im}^{2} \cdot 0.5} \]
      2. metadata-eval50.3%

        \[\leadsto \color{blue}{1} + {im}^{2} \cdot 0.5 \]
      3. unpow250.3%

        \[\leadsto 1 + \color{blue}{\left(im \cdot im\right)} \cdot 0.5 \]
    7. Simplified50.3%

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

    if 1020 < 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-out69.6%

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

        \[\leadsto \left(e^{im} + e^{-im}\right) \cdot \left(0.5 + -0.25 \cdot \color{blue}{\left(re \cdot re\right)}\right) \]
    4. Simplified69.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-rr23.3%

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

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

Alternative 12: 49.3% accurate, 27.6× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 2800 \lor \neg \left(im \leq 1.3 \cdot 10^{+154}\right):\\
\;\;\;\;1 + 0.5 \cdot \left(im \cdot im\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 2800 or 1.29999999999999994e154 < 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 83.3%

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot \left(2 + {im}^{2}\right)} \]
    6. Step-by-step derivation
      1. distribute-rgt-in50.3%

        \[\leadsto \color{blue}{2 \cdot 0.5 + {im}^{2} \cdot 0.5} \]
      2. metadata-eval50.3%

        \[\leadsto \color{blue}{1} + {im}^{2} \cdot 0.5 \]
      3. unpow250.3%

        \[\leadsto 1 + \color{blue}{\left(im \cdot im\right)} \cdot 0.5 \]
    7. Simplified50.3%

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

    if 2800 < im < 1.29999999999999994e154

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + \color{blue}{im \cdot im}, -0.25 \cdot \left(\left(2 + {im}^{2}\right) \cdot {re}^{2}\right)\right) \]
      3. *-commutative25.0%

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

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

        \[\leadsto \mathsf{fma}\left(0.5, 2 + im \cdot im, -0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(2 + \color{blue}{im \cdot im}\right)\right)\right) \]
    7. Simplified25.0%

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

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

        \[\leadsto \color{blue}{-0.5 \cdot {re}^{2} + 1} \]
      2. unpow223.3%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 2800 \lor \neg \left(im \leq 1.3 \cdot 10^{+154}\right):\\ \;\;\;\;1 + 0.5 \cdot \left(im \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;1 + \left(re \cdot re\right) \cdot -0.5\\ \end{array} \]

Alternative 13: 47.8% accurate, 44.0× speedup?

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

\\
1 + 0.5 \cdot \left(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 im around 0 76.4%

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

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

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

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

      \[\leadsto \color{blue}{2 \cdot 0.5 + {im}^{2} \cdot 0.5} \]
    2. metadata-eval46.2%

      \[\leadsto \color{blue}{1} + {im}^{2} \cdot 0.5 \]
    3. unpow246.2%

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

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

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

Alternative 14: 3.8% 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 im around 0 76.4%

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

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

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

    \[\leadsto \color{blue}{-2 + \cos re} \]
  6. Step-by-step derivation
    1. +-commutative3.4%

      \[\leadsto \color{blue}{\cos re + -2} \]
  7. Simplified3.4%

    \[\leadsto \color{blue}{\cos re + -2} \]
  8. Taylor expanded in re around 0 3.9%

    \[\leadsto \color{blue}{-1} \]
  9. Final simplification3.9%

    \[\leadsto -1 \]

Alternative 15: 29.2% 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-rr24.9%

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

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

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

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

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

    \[\leadsto 1 \]

Reproduce

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