math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 11.7s
Alternatives: 10
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 10 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, 0.8× speedup?

\[\begin{array}{l} \\ \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (cos re) (fma 0.5 (exp im) (/ 0.5 (exp im)))))
double code(double re, double im) {
	return cos(re) * fma(0.5, exp(im), (0.5 / exp(im)));
}
function code(re, im)
	return Float64(cos(re) * fma(0.5, exp(im), Float64(0.5 / exp(im))))
end
code[re_, im_] := N[(N[Cos[re], $MachinePrecision] * N[(0.5 * N[Exp[im], $MachinePrecision] + N[(0.5 / N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{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. Simplified100.0%

    \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
  3. Final simplification100.0%

    \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right) \]

Alternative 2: 100.0% accurate, 1.0× speedup?

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

\\
\left(\cos re \cdot 0.5\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(\cos re \cdot 0.5\right) \cdot \left(e^{im} + e^{-im}\right) \]

Alternative 3: 88.3% accurate, 1.5× speedup?

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

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

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


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

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

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

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

    if 1.30000000000000004 < im

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    3. Applied egg-rr98.7%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0}\right) \]
    4. Taylor expanded in re around inf 98.7%

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

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

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

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

Alternative 4: 86.2% accurate, 2.5× speedup?

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

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

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

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


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

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

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

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

    if 1.80000000000000004 < im < 1.3000000000000001e103

    1. Initial program 100.0%

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

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

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0}\right) \]
    4. Taylor expanded in re around 0 6.8%

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

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

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

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

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

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

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

    if 1.3000000000000001e103 < im

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    3. Applied egg-rr100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0.0625}\right) \]
    4. Taylor expanded in im around 0 100.0%

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

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

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

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

        \[\leadsto \left(0.5625 \cdot \cos re + \color{blue}{\left(0.5 \cdot im\right) \cdot \cos re}\right) + \left(0.08333333333333333 \cdot \left({im}^{3} \cdot \cos re\right) + 0.25 \cdot \left({im}^{2} \cdot \cos re\right)\right) \]
      5. distribute-rgt-out100.0%

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

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

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

        \[\leadsto \cos re \cdot \left(0.5625 + 0.5 \cdot im\right) + \left(\left(0.08333333333333333 \cdot {im}^{3}\right) \cdot \cos re + \color{blue}{\left(0.25 \cdot \left(im \cdot im\right)\right) \cdot \cos re}\right) \]
      9. distribute-rgt-out100.0%

        \[\leadsto \cos re \cdot \left(0.5625 + 0.5 \cdot im\right) + \color{blue}{\cos re \cdot \left(0.08333333333333333 \cdot {im}^{3} + 0.25 \cdot \left(im \cdot im\right)\right)} \]
      10. distribute-lft-out100.0%

        \[\leadsto \color{blue}{\cos re \cdot \left(\left(0.5625 + 0.5 \cdot im\right) + \left(0.08333333333333333 \cdot {im}^{3} + 0.25 \cdot \left(im \cdot im\right)\right)\right)} \]
      11. cube-mult100.0%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1.8:\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{elif}\;im \leq 1.3 \cdot 10^{+103}:\\ \;\;\;\;e^{im} \cdot \left(0.5 + re \cdot \left(re \cdot -0.25\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\cos re \cdot \left(\left(0.5625 + 0.5 \cdot im\right) + \left(im \cdot im\right) \cdot \left(im \cdot 0.08333333333333333 + 0.25\right)\right)\\ \end{array} \]

Alternative 5: 72.4% accurate, 2.7× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 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. Taylor expanded in im around 0 83.0%

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

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

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

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

    if 1.3999999999999999 < im < 1.35000000000000003e154

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    3. Applied egg-rr97.8%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0}\right) \]
    4. Taylor expanded in re around 0 3.8%

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

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

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

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

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

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

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

    if 1.35000000000000003e154 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

Alternative 6: 85.1% accurate, 2.7× speedup?

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

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

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

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


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

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

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

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

    if 1.80000000000000004 < im < 1.35000000000000003e154

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    3. Applied egg-rr97.8%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0}\right) \]
    4. Taylor expanded in re around 0 3.8%

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

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

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

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

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

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

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

    if 1.35000000000000003e154 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

Alternative 7: 72.3% accurate, 2.8× speedup?

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

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

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

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


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

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

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

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

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

    if 1.6000000000000001 < im < 1.35000000000000003e154

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    3. Applied egg-rr97.8%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0}\right) \]
    4. Taylor expanded in re around 0 62.6%

      \[\leadsto \color{blue}{0.5 \cdot e^{im}} \]

    if 1.35000000000000003e154 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

Alternative 8: 69.0% accurate, 2.9× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot e^{im}\\


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

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

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

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

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

    if 1.3500000000000001 < im

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    3. Applied egg-rr98.7%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0}\right) \]
    4. Taylor expanded in re around 0 74.6%

      \[\leadsto \color{blue}{0.5 \cdot e^{im}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification68.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1.35:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot e^{im}\\ \end{array} \]

Alternative 9: 60.3% accurate, 3.0× speedup?

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

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

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


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

    1. Initial program 100.0%

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

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

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

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

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

    if 5.39999999999999969e104 < 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 64.4%

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
    10. Simplified58.3%

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

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

Alternative 10: 21.8% accurate, 61.6× speedup?

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
  10. Simplified21.5%

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

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

Reproduce

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