math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 6.0s
Alternatives: 8
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 8 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. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 87.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 360:\\ \;\;\;\;\cos re \cdot \left(1 + 0.5 \cdot {im}^{2}\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\cos re \cdot \left(0.5 \cdot im\right)\right)\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 360.0)
   (* (cos re) (+ 1.0 (* 0.5 (pow im 2.0))))
   (* im (log1p (expm1 (* (cos re) (* 0.5 im)))))))
double code(double re, double im) {
	double tmp;
	if (im <= 360.0) {
		tmp = cos(re) * (1.0 + (0.5 * pow(im, 2.0)));
	} else {
		tmp = im * log1p(expm1((cos(re) * (0.5 * im))));
	}
	return tmp;
}
public static double code(double re, double im) {
	double tmp;
	if (im <= 360.0) {
		tmp = Math.cos(re) * (1.0 + (0.5 * Math.pow(im, 2.0)));
	} else {
		tmp = im * Math.log1p(Math.expm1((Math.cos(re) * (0.5 * im))));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 360.0:
		tmp = math.cos(re) * (1.0 + (0.5 * math.pow(im, 2.0)))
	else:
		tmp = im * math.log1p(math.expm1((math.cos(re) * (0.5 * im))))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 360.0)
		tmp = Float64(cos(re) * Float64(1.0 + Float64(0.5 * (im ^ 2.0))));
	else
		tmp = Float64(im * log1p(expm1(Float64(cos(re) * Float64(0.5 * im)))));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[im, 360.0], N[(N[Cos[re], $MachinePrecision] * N[(1.0 + N[(0.5 * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(im * N[Log[1 + N[(Exp[N[(N[Cos[re], $MachinePrecision] * N[(0.5 * im), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

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

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


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

    1. Initial program 100.0%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, {im}^{2}, 1\right)} \]
    6. Taylor expanded in re around inf 86.3%

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

    if 360 < im

    1. Initial program 100.0%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, {im}^{2}, 1\right)} \]
    6. Taylor expanded in im around inf 51.2%

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

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

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

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

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

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

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

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

        \[\leadsto \left(\color{blue}{\left(\cos re \cdot 0.5\right)} \cdot im\right) \cdot im \]
    10. Applied egg-rr51.2%

      \[\leadsto \color{blue}{\left(\left(\cos re \cdot 0.5\right) \cdot im\right) \cdot im} \]
    11. Step-by-step derivation
      1. log1p-expm1-u100.0%

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

        \[\leadsto \mathsf{log1p}\left(\mathsf{expm1}\left(\color{blue}{\cos re \cdot \left(0.5 \cdot im\right)}\right)\right) \cdot im \]
    12. Applied egg-rr100.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 360:\\ \;\;\;\;\cos re \cdot \left(1 + 0.5 \cdot {im}^{2}\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\cos re \cdot \left(0.5 \cdot im\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 84.6% accurate, 1.4× speedup?

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

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

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

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


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

    1. Initial program 100.0%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, {im}^{2}, 1\right)} \]
    6. Taylor expanded in re around inf 86.3%

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

    if 250 < im < 2.2000000000000001e154

    1. Initial program 100.0%

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

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

    if 2.2000000000000001e154 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \left(\color{blue}{\left(\cos re \cdot 0.5\right)} \cdot im\right) \cdot im \]
    10. Applied egg-rr100.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 250:\\ \;\;\;\;\cos re \cdot \left(1 + 0.5 \cdot {im}^{2}\right)\\ \mathbf{elif}\;im \leq 2.2 \cdot 10^{+154}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(\left(0.5 \cdot \cos re\right) \cdot im\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 71.4% accurate, 1.4× speedup?

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

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

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

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


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

    1. Initial program 100.0%

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

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

    if 250 < im < 2.09999999999999994e154

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\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. Add Preprocessing
    3. Taylor expanded in im around 0 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \left(\color{blue}{\left(\cos re \cdot 0.5\right)} \cdot im\right) \cdot im \]
    10. Applied egg-rr100.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 250:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 2.1 \cdot 10^{+154}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(\left(0.5 \cdot \cos re\right) \cdot im\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 64.2% accurate, 2.5× speedup?

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

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

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

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


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

    1. Initial program 100.0%

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

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

    if 2.2e14 < im < 2.79999999999999985e153

    1. Initial program 100.0%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, {im}^{2}, 1\right)} \]
    6. Taylor expanded in im around inf 5.2%

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

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

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

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

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

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

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

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

        \[\leadsto \left(\color{blue}{\left(\cos re \cdot 0.5\right)} \cdot im\right) \cdot im \]
    10. Applied egg-rr5.2%

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

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

    if 2.79999999999999985e153 < im

    1. Initial program 100.0%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, {im}^{2}, 1\right)} \]
    6. Taylor expanded in im around inf 97.2%

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

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

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

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

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

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

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

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

        \[\leadsto \left(\color{blue}{\left(\cos re \cdot 0.5\right)} \cdot im\right) \cdot im \]
    10. Applied egg-rr97.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 2.2 \cdot 10^{+14}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 2.8 \cdot 10^{+153}:\\ \;\;\;\;im \cdot \left(0.5 \cdot im + -0.25 \cdot \left(im \cdot {re}^{2}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(\left(0.5 \cdot \cos re\right) \cdot im\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 62.6% accurate, 2.7× speedup?

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

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

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


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

    1. Initial program 100.0%

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

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

    if 1.3999999999999999 < im

    1. Initial program 100.0%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, {im}^{2}, 1\right)} \]
    6. Taylor expanded in im around inf 50.4%

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

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

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

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

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

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

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

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

        \[\leadsto \left(\color{blue}{\left(\cos re \cdot 0.5\right)} \cdot im\right) \cdot im \]
    10. Applied egg-rr50.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1.4:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(\left(0.5 \cdot \cos re\right) \cdot im\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 59.2% accurate, 2.9× speedup?

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

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

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


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

    1. Initial program 100.0%

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

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

    if 2.20000000000000006e38 < im

    1. Initial program 100.0%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, {im}^{2}, 1\right)} \]
    6. Taylor expanded in im around inf 59.3%

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

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

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

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

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

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

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

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

        \[\leadsto \left(\color{blue}{\left(\cos re \cdot 0.5\right)} \cdot im\right) \cdot im \]
    10. Applied egg-rr59.3%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 2.2 \cdot 10^{+38}:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(0.5 \cdot im\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 22.6% accurate, 61.6× speedup?

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

\\
im \cdot \left(0.5 \cdot im\right)
\end{array}
Derivation
  1. Initial program 100.0%

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

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

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

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

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

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

    \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, {im}^{2}, 1\right)} \]
  6. Taylor expanded in im around inf 29.5%

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

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

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

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

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

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

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

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

      \[\leadsto \left(\color{blue}{\left(\cos re \cdot 0.5\right)} \cdot im\right) \cdot im \]
  10. Applied egg-rr29.5%

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

    \[\leadsto \color{blue}{\left(0.5 \cdot im\right)} \cdot im \]
  12. Final simplification20.5%

    \[\leadsto im \cdot \left(0.5 \cdot im\right) \]
  13. Add Preprocessing

Reproduce

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