math.cos on complex, real part

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \left(\color{blue}{im \cdot \left(im \cdot 0.5\right)} + 1\right) \]
      8. fma-def84.0%

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

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

    if 1e3 < im < 2.09999999999999994e154

    1. Initial program 100.0%

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

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

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

    if 2.09999999999999994e154 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 3: 71.8% accurate, 1.4× speedup?

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

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

\mathbf{elif}\;im \leq 2.9 \cdot 10^{+100}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;im \leq 1.9 \cdot 10^{+154}:\\
\;\;\;\;t_0\\

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


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

    1. Initial program 100.0%

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

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

    if 0.17999999999999999 < im < 2.9e100 or 5.4000000000000004e133 < im < 1.8999999999999999e154

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

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

    if 2.9e100 < im < 5.4000000000000004e133

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.8999999999999999e154 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 0.18:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 2.9 \cdot 10^{+100}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \mathbf{elif}\;im \leq 5.4 \cdot 10^{+133}:\\ \;\;\;\;im \cdot \left(im \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\right)\\ \mathbf{elif}\;im \leq 1.9 \cdot 10^{+154}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(\cos re \cdot \left(0.5 \cdot im\right)\right)\\ \end{array} \]

Alternative 4: 83.8% accurate, 1.4× speedup?

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

\\
\begin{array}{l}
t_0 := 0.5 \cdot \left(e^{-im} + e^{im}\right)\\
\mathbf{if}\;im \leq 0.18:\\
\;\;\;\;\cos re \cdot \mathsf{fma}\left(im, 0.5 \cdot im, 1\right)\\

\mathbf{elif}\;im \leq 2.9 \cdot 10^{+100}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;im \leq 2.5 \cdot 10^{+154}:\\
\;\;\;\;t_0\\

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


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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \left(\color{blue}{im \cdot \left(im \cdot 0.5\right)} + 1\right) \]
      8. fma-def84.0%

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

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

    if 0.17999999999999999 < im < 2.9e100 or 5.4000000000000004e133 < im < 2.50000000000000002e154

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

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

    if 2.9e100 < im < 5.4000000000000004e133

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.50000000000000002e154 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 0.18:\\ \;\;\;\;\cos re \cdot \mathsf{fma}\left(im, 0.5 \cdot im, 1\right)\\ \mathbf{elif}\;im \leq 2.9 \cdot 10^{+100}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \mathbf{elif}\;im \leq 5.4 \cdot 10^{+133}:\\ \;\;\;\;im \cdot \left(im \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\right)\\ \mathbf{elif}\;im \leq 2.5 \cdot 10^{+154}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(\cos re \cdot \left(0.5 \cdot im\right)\right)\\ \end{array} \]

Alternative 5: 66.2% accurate, 2.0× speedup?

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

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

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

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

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


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

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

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

    if 1.22e13 < im < 4.4999999999999997e134

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

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

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

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

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

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

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

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

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

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

    if 4.4999999999999997e134 < im < 1.4e154

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto im \cdot \color{blue}{\left(im \cdot \left(-0.25 \cdot \left(re \cdot re\right) + 0.5\right)\right)} \]
    10. Step-by-step derivation
      1. associate-*r*8.4%

        \[\leadsto \color{blue}{\left(im \cdot im\right) \cdot \left(-0.25 \cdot \left(re \cdot re\right) + 0.5\right)} \]
      2. distribute-lft-in8.4%

        \[\leadsto \color{blue}{\left(im \cdot im\right) \cdot \left(-0.25 \cdot \left(re \cdot re\right)\right) + \left(im \cdot im\right) \cdot 0.5} \]
      3. flip-+80.0%

        \[\leadsto \color{blue}{\frac{\left(\left(im \cdot im\right) \cdot \left(-0.25 \cdot \left(re \cdot re\right)\right)\right) \cdot \left(\left(im \cdot im\right) \cdot \left(-0.25 \cdot \left(re \cdot re\right)\right)\right) - \left(\left(im \cdot im\right) \cdot 0.5\right) \cdot \left(\left(im \cdot im\right) \cdot 0.5\right)}{\left(im \cdot im\right) \cdot \left(-0.25 \cdot \left(re \cdot re\right)\right) - \left(im \cdot im\right) \cdot 0.5}} \]
      4. *-commutative80.0%

        \[\leadsto \frac{\color{blue}{\left(\left(-0.25 \cdot \left(re \cdot re\right)\right) \cdot \left(im \cdot im\right)\right)} \cdot \left(\left(im \cdot im\right) \cdot \left(-0.25 \cdot \left(re \cdot re\right)\right)\right) - \left(\left(im \cdot im\right) \cdot 0.5\right) \cdot \left(\left(im \cdot im\right) \cdot 0.5\right)}{\left(im \cdot im\right) \cdot \left(-0.25 \cdot \left(re \cdot re\right)\right) - \left(im \cdot im\right) \cdot 0.5} \]
      5. associate-*l*80.0%

        \[\leadsto \frac{\color{blue}{\left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right)} \cdot \left(\left(im \cdot im\right) \cdot \left(-0.25 \cdot \left(re \cdot re\right)\right)\right) - \left(\left(im \cdot im\right) \cdot 0.5\right) \cdot \left(\left(im \cdot im\right) \cdot 0.5\right)}{\left(im \cdot im\right) \cdot \left(-0.25 \cdot \left(re \cdot re\right)\right) - \left(im \cdot im\right) \cdot 0.5} \]
      6. *-commutative80.0%

        \[\leadsto \frac{\left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right) \cdot \color{blue}{\left(\left(-0.25 \cdot \left(re \cdot re\right)\right) \cdot \left(im \cdot im\right)\right)} - \left(\left(im \cdot im\right) \cdot 0.5\right) \cdot \left(\left(im \cdot im\right) \cdot 0.5\right)}{\left(im \cdot im\right) \cdot \left(-0.25 \cdot \left(re \cdot re\right)\right) - \left(im \cdot im\right) \cdot 0.5} \]
      7. associate-*l*80.0%

        \[\leadsto \frac{\left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right) \cdot \color{blue}{\left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right)} - \left(\left(im \cdot im\right) \cdot 0.5\right) \cdot \left(\left(im \cdot im\right) \cdot 0.5\right)}{\left(im \cdot im\right) \cdot \left(-0.25 \cdot \left(re \cdot re\right)\right) - \left(im \cdot im\right) \cdot 0.5} \]
      8. *-commutative80.0%

        \[\leadsto \frac{\left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right) \cdot \left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right) - \color{blue}{\left(0.5 \cdot \left(im \cdot im\right)\right)} \cdot \left(\left(im \cdot im\right) \cdot 0.5\right)}{\left(im \cdot im\right) \cdot \left(-0.25 \cdot \left(re \cdot re\right)\right) - \left(im \cdot im\right) \cdot 0.5} \]
      9. *-commutative80.0%

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

        \[\leadsto \frac{\left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right) \cdot \left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right) - \left(0.5 \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot \left(im \cdot im\right)\right)}{\color{blue}{\left(-0.25 \cdot \left(re \cdot re\right)\right) \cdot \left(im \cdot im\right)} - \left(im \cdot im\right) \cdot 0.5} \]
      11. associate-*l*80.0%

        \[\leadsto \frac{\left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right) \cdot \left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right) - \left(0.5 \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot \left(im \cdot im\right)\right)}{\color{blue}{-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)} - \left(im \cdot im\right) \cdot 0.5} \]
      12. *-commutative80.0%

        \[\leadsto \frac{\left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right) \cdot \left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right) - \left(0.5 \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot \left(im \cdot im\right)\right)}{-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right) - \color{blue}{0.5 \cdot \left(im \cdot im\right)}} \]
      13. fma-neg80.0%

        \[\leadsto \frac{\left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right) \cdot \left(-0.25 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right) - \left(0.5 \cdot \left(im \cdot im\right)\right) \cdot \left(0.5 \cdot \left(im \cdot im\right)\right)}{\color{blue}{\mathsf{fma}\left(-0.25, \left(re \cdot re\right) \cdot \left(im \cdot im\right), -0.5 \cdot \left(im \cdot im\right)\right)}} \]
    11. Applied egg-rr80.0%

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

    if 1.4e154 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 6: 65.8% accurate, 2.6× speedup?

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

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

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

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


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

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

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

    if 1.22e13 < im < 3.5e141

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

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

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

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

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

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

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

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

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

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

    if 3.5e141 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 7: 65.8% accurate, 2.8× speedup?

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

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

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

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


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

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

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

    if 1.22e13 < im < 1.55000000000000011e135

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.55000000000000011e135 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 8: 62.7% accurate, 3.0× speedup?

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

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

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

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


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

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

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

    if 1.22e13 < im < 1.55000000000000011e135

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.55000000000000011e135 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

Alternative 9: 49.1% accurate, 20.3× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 9.2 \cdot 10^{-7} \lor \neg \left(im \leq 1.55 \cdot 10^{+135}\right):\\
\;\;\;\;1 + im \cdot \left(0.5 \cdot im\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 9.1999999999999998e-7 or 1.55000000000000011e135 < im

    1. Initial program 100.0%

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

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

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

      \[\leadsto \color{blue}{1 + 0.5 \cdot {im}^{2}} \]
    5. Step-by-step derivation
      1. *-commutative56.0%

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

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

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

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

    if 9.1999999999999998e-7 < im < 1.55000000000000011e135

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 10: 48.9% accurate, 23.4× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 1.22e13 or 1.55000000000000011e135 < 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.0%

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

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

      \[\leadsto \color{blue}{1 + 0.5 \cdot {im}^{2}} \]
    5. Step-by-step derivation
      1. *-commutative55.0%

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

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

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

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

    if 1.22e13 < im < 1.55000000000000011e135

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto -0.25 \cdot \color{blue}{\left(re \cdot \left(re \cdot \left(im \cdot im\right)\right)\right)} \]
    12. Simplified26.1%

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

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

Alternative 11: 48.9% accurate, 23.4× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 1.22e13 or 1.05000000000000005e135 < 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.0%

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

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

      \[\leadsto \color{blue}{1 + 0.5 \cdot {im}^{2}} \]
    5. Step-by-step derivation
      1. *-commutative55.0%

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

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

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

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

    if 1.22e13 < im < 1.05000000000000005e135

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto im \cdot \left(re \cdot \left(re \cdot \color{blue}{\left(im \cdot -0.25\right)}\right)\right) \]
    12. Simplified26.1%

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

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

Alternative 12: 19.2% accurate, 27.7× speedup?

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

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

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

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


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

    1. Initial program 100.0%

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

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

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

    if 310 < im < 4.49999999999999982e149

    1. Initial program 100.0%

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

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

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

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

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

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

    if 4.49999999999999982e149 < 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 93.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 13: 47.9% accurate, 34.0× speedup?

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

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

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


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

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

    if 6.9999999999999997e166 < re

    1. Initial program 100.0%

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

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

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

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

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

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

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

Alternative 14: 17.8% accurate, 43.6× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 9.2 \cdot 10^{-7}:\\
\;\;\;\;0.25\\

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


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

    1. Initial program 100.0%

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

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

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

    if 9.1999999999999998e-7 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 15: 8.1% accurate, 308.0× speedup?

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

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

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

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

    \[\leadsto \color{blue}{0.25} \]
  4. Final simplification8.0%

    \[\leadsto 0.25 \]

Reproduce

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