math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 6.4s
Alternatives: 9
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 9 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: 92.6% accurate, 1.4× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 11.2 \lor \neg \left(im \leq 1.15 \cdot 10^{+77}\right):\\
\;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(\left(2 + im \cdot im\right) + 0.08333333333333333 \cdot {im}^{4}\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 11.199999999999999 or 1.14999999999999997e77 < 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 92.1%

      \[\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+92.1%

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

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

      \[\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 11.199999999999999 < im < 1.14999999999999997e77

    1. Initial program 100.0%

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

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

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

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

        \[\leadsto \left(\color{blue}{1 \cdot e^{im}} + e^{-im}\right) \cdot 0.5 \]
      2. fma-def88.9%

        \[\leadsto \color{blue}{\mathsf{fma}\left(1, e^{im}, e^{-im}\right)} \cdot 0.5 \]
      3. add-sqr-sqrt0.0%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{-im} \cdot \sqrt{-im}}}\right) \cdot 0.5 \]
      4. sqrt-unprod88.9%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{\left(-im\right) \cdot \left(-im\right)}}}\right) \cdot 0.5 \]
      5. sqr-neg88.9%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\sqrt{\color{blue}{im \cdot im}}}\right) \cdot 0.5 \]
      6. sqrt-prod88.9%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}\right) \cdot 0.5 \]
      7. add-sqr-sqrt88.9%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{im}}\right) \cdot 0.5 \]
    6. Applied egg-rr88.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(1, e^{im}, e^{im}\right)} \cdot 0.5 \]
    7. Step-by-step derivation
      1. fma-udef88.9%

        \[\leadsto \color{blue}{\left(1 \cdot e^{im} + e^{im}\right)} \cdot 0.5 \]
      2. distribute-lft1-in88.9%

        \[\leadsto \color{blue}{\left(\left(1 + 1\right) \cdot e^{im}\right)} \cdot 0.5 \]
      3. metadata-eval88.9%

        \[\leadsto \left(\color{blue}{2} \cdot e^{im}\right) \cdot 0.5 \]
    8. Simplified88.9%

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

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

Alternative 3: 85.0% accurate, 1.5× speedup?

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

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

\mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\
\;\;\;\;0.5 \cdot \left(e^{im} \cdot 2\right)\\

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


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

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

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

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

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

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

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

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

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

        \[\leadsto \left(im \cdot im\right) \cdot \color{blue}{\left(\cos re \cdot 0.5\right)} + \cos re \]
    6. Applied egg-rr81.1%

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

    if 6 < 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 79.5%

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(1, e^{im}, e^{-im}\right)} \cdot 0.5 \]
      3. add-sqr-sqrt0.0%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{-im} \cdot \sqrt{-im}}}\right) \cdot 0.5 \]
      4. sqrt-unprod79.5%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{\left(-im\right) \cdot \left(-im\right)}}}\right) \cdot 0.5 \]
      5. sqr-neg79.5%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\sqrt{\color{blue}{im \cdot im}}}\right) \cdot 0.5 \]
      6. sqrt-prod79.5%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}\right) \cdot 0.5 \]
      7. add-sqr-sqrt79.5%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{im}}\right) \cdot 0.5 \]
    6. Applied egg-rr79.5%

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

        \[\leadsto \color{blue}{\left(1 \cdot e^{im} + e^{im}\right)} \cdot 0.5 \]
      2. distribute-lft1-in79.5%

        \[\leadsto \color{blue}{\left(\left(1 + 1\right) \cdot e^{im}\right)} \cdot 0.5 \]
      3. metadata-eval79.5%

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

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

    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 simplification83.9%

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

Alternative 4: 85.0% accurate, 2.7× speedup?

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

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

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


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

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

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

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

    if 7.20000000000000018 < 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 79.5%

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(1, e^{im}, e^{-im}\right)} \cdot 0.5 \]
      3. add-sqr-sqrt0.0%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{-im} \cdot \sqrt{-im}}}\right) \cdot 0.5 \]
      4. sqrt-unprod79.5%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{\left(-im\right) \cdot \left(-im\right)}}}\right) \cdot 0.5 \]
      5. sqr-neg79.5%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\sqrt{\color{blue}{im \cdot im}}}\right) \cdot 0.5 \]
      6. sqrt-prod79.5%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}\right) \cdot 0.5 \]
      7. add-sqr-sqrt79.5%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{im}}\right) \cdot 0.5 \]
    6. Applied egg-rr79.5%

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

        \[\leadsto \color{blue}{\left(1 \cdot e^{im} + e^{im}\right)} \cdot 0.5 \]
      2. distribute-lft1-in79.5%

        \[\leadsto \color{blue}{\left(\left(1 + 1\right) \cdot e^{im}\right)} \cdot 0.5 \]
      3. metadata-eval79.5%

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

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

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

Alternative 5: 78.2% accurate, 2.8× speedup?

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

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

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


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

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

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

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

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

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

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

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

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

        \[\leadsto \left(im \cdot im\right) \cdot \color{blue}{\left(\cos re \cdot 0.5\right)} + \cos re \]
    6. Applied egg-rr81.1%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot im\right)} + \cos re \]
      2. associate-*r*77.9%

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

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

    if 3.7000000000000002 < 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 76.3%

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(1, e^{im}, e^{-im}\right)} \cdot 0.5 \]
      3. add-sqr-sqrt0.0%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{-im} \cdot \sqrt{-im}}}\right) \cdot 0.5 \]
      4. sqrt-unprod76.3%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{\left(-im\right) \cdot \left(-im\right)}}}\right) \cdot 0.5 \]
      5. sqr-neg76.3%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\sqrt{\color{blue}{im \cdot im}}}\right) \cdot 0.5 \]
      6. sqrt-prod76.3%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}\right) \cdot 0.5 \]
      7. add-sqr-sqrt76.3%

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(1, e^{im}, e^{im}\right)} \cdot 0.5 \]
    7. Step-by-step derivation
      1. fma-udef76.3%

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

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

        \[\leadsto \left(\color{blue}{2} \cdot e^{im}\right) \cdot 0.5 \]
    8. Simplified76.3%

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

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

Alternative 6: 68.9% accurate, 2.9× speedup?

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

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

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


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

    1. Initial program 100.0%

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

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

    if 3.60000000000000009 < 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 76.3%

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(1, e^{im}, e^{-im}\right)} \cdot 0.5 \]
      3. add-sqr-sqrt0.0%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{-im} \cdot \sqrt{-im}}}\right) \cdot 0.5 \]
      4. sqrt-unprod76.3%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{\left(-im\right) \cdot \left(-im\right)}}}\right) \cdot 0.5 \]
      5. sqr-neg76.3%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\sqrt{\color{blue}{im \cdot im}}}\right) \cdot 0.5 \]
      6. sqrt-prod76.3%

        \[\leadsto \mathsf{fma}\left(1, e^{im}, e^{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}\right) \cdot 0.5 \]
      7. add-sqr-sqrt76.3%

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(1, e^{im}, e^{im}\right)} \cdot 0.5 \]
    7. Step-by-step derivation
      1. fma-udef76.3%

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

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

        \[\leadsto \left(\color{blue}{2} \cdot e^{im}\right) \cdot 0.5 \]
    8. Simplified76.3%

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

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

Alternative 7: 60.4% accurate, 3.0× speedup?

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

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

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


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

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

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

    if 4.10000000000000004e39 < 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 61.9%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot im\right)} + 1 \]
      3. associate-*r*45.3%

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

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

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

Alternative 8: 47.3% accurate, 44.0× speedup?

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

\\
im \cdot \left(0.5 \cdot im\right) + 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 72.6%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot im\right)} + 1 \]
    3. associate-*r*46.1%

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

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

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

Alternative 9: 28.6% accurate, 308.0× speedup?

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

\\
1
\end{array}
Derivation
  1. Initial program 100.0%

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

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

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

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

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

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

    \[\leadsto 1 \]

Reproduce

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