math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 7.1s
Alternatives: 14
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 14 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.0% accurate, 1.5× speedup?

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

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

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


\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 84.5%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\left(im \cdot im + 2\right)} \]
      2. distribute-lft-in84.5%

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

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

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

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

        \[\leadsto \cos re \cdot \left(0.5 \cdot \left(im \cdot im\right)\right) + \color{blue}{\left(2 \cdot 0.5\right) \cdot \cos re} \]
      7. metadata-eval84.5%

        \[\leadsto \cos re \cdot \left(0.5 \cdot \left(im \cdot im\right)\right) + \color{blue}{1} \cdot \cos re \]
      8. *-un-lft-identity84.5%

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

      \[\leadsto \color{blue}{\cos re \cdot \left(0.5 \cdot \left(im \cdot im\right)\right) + \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-rr99.2%

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

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

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

Alternative 4: 88.0% accurate, 1.5× speedup?

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

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

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


\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 84.5%

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

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

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

    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-rr99.2%

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

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

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

Alternative 5: 85.9% accurate, 2.5× speedup?

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

\mathbf{elif}\;im \leq 1.3 \cdot 10^{+103}:\\
\;\;\;\;0.001953125 + 0.5 \cdot e^{im}\\

\mathbf{else}:\\
\;\;\;\;\cos re \cdot \left(\left(0.501953125 + 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 < 6.20000000000000018

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

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

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

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

    if 6.20000000000000018 < 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-rr100.0%

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

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

    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.001953125}\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.501953125 \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.501953125 \cdot \cos re\right)} \]
      2. +-commutative100.0%

        \[\leadsto \color{blue}{\left(0.5 \cdot \left(im \cdot \cos re\right) + 0.501953125 \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.501953125 \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.501953125 \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.501953125 + 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.501953125 + 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.501953125 + 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.501953125 + 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.501953125 + 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.501953125 + 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.501953125 + 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.501953125 + 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.501953125 + 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 simplification84.7%

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

Alternative 6: 69.6% accurate, 2.8× speedup?

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

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

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

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\


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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(2 + \color{blue}{im \cdot im}\right) \]
    4. Simplified84.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 64.6%

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

    if 6.20000000000000018 < im < 2.3e154

    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.001953125}\right) \]
    4. Taylor expanded in re around 0 65.2%

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

    if 2.3e154 < im < 4.9999999999999997e165

    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. associate-*l*100.0%

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

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

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

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

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

    if 4.9999999999999997e165 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    7. Simplified96.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 6.2:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 2.3 \cdot 10^{+154}:\\ \;\;\;\;0.001953125 + 0.5 \cdot e^{im}\\ \mathbf{elif}\;im \leq 5 \cdot 10^{+165}:\\ \;\;\;\;0.5 \cdot \left(im \cdot \left(im + -0.5 \cdot \left(im \cdot \left(re \cdot re\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\ \end{array} \]

Alternative 7: 72.6% accurate, 2.8× speedup?

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

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

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

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


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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(2 + \color{blue}{im \cdot im}\right) \]
    4. Simplified84.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 64.6%

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

    if 6.20000000000000018 < 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-rr100.0%

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

      \[\leadsto \color{blue}{0.001953125 + 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. associate-*l*100.0%

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

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

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

Alternative 8: 84.9% accurate, 2.8× speedup?

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

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

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

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


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

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

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

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

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

    if 6.20000000000000018 < im < 1.50000000000000013e154

    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.001953125}\right) \]
    4. Taylor expanded in re around 0 65.2%

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

    if 1.50000000000000013e154 < 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. associate-*l*100.0%

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

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

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

Alternative 9: 62.9% accurate, 2.8× speedup?

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

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

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\


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

    1. Initial program 100.0%

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

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

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

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

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

    if 3.05e10 < im < 1.35e165

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

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

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

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

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

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

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

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

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

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

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

    if 1.35e165 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    7. Simplified96.2%

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

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

Alternative 10: 62.9% accurate, 3.0× speedup?

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

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

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

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


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

    1. Initial program 100.0%

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

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

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

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

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

    if 1e11 < im < 9.99999999999999899e164

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

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

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

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

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

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

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

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

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

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

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

    if 9.99999999999999899e164 < 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. associate-*r*100.0%

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

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

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

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

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

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

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

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

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

Alternative 11: 40.1% accurate, 14.6× speedup?

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 1 + \color{blue}{\left(2 \cdot -0.25\right) \cdot {re}^{2}} \]
      4. metadata-eval38.9%

        \[\leadsto 1 + \color{blue}{-0.5} \cdot {re}^{2} \]
      5. unpow238.9%

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

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

    if 7.8000000000000002e58 < 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 70.3%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(im + \left(\left(-0.5 \cdot {re}^{2}\right) \cdot im + \color{blue}{\left(0.041666666666666664 \cdot {re}^{4}\right) \cdot im}\right)\right)\right) \]
      5. distribute-rgt-out46.9%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(im + im \cdot \left(-0.5 \cdot {re}^{2} + 0.041666666666666664 \cdot {re}^{\color{blue}{\left(2 \cdot 2\right)}}\right)\right)\right) \]
      7. pow-sqr46.9%

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(im + im \cdot \color{blue}{\left({re}^{2} \cdot \left(-0.5 + 0.041666666666666664 \cdot {re}^{2}\right)\right)}\right)\right) \]
      10. unpow260.5%

        \[\leadsto 0.5 \cdot \left(im \cdot \left(im + im \cdot \left(\color{blue}{\left(re \cdot re\right)} \cdot \left(-0.5 + 0.041666666666666664 \cdot {re}^{2}\right)\right)\right)\right) \]
      11. unpow260.5%

        \[\leadsto 0.5 \cdot \left(im \cdot \left(im + im \cdot \left(\left(re \cdot re\right) \cdot \left(-0.5 + 0.041666666666666664 \cdot \color{blue}{\left(re \cdot re\right)}\right)\right)\right)\right) \]
    10. Simplified60.5%

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

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

Alternative 12: 40.0% accurate, 18.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 6.2:\\ \;\;\;\;1 + -0.5 \cdot \left(re \cdot re\right)\\ \mathbf{elif}\;im \leq 2 \cdot 10^{+165}:\\ \;\;\;\;0.5 \cdot \left(im \cdot \left(im + -0.5 \cdot \left(im \cdot \left(re \cdot re\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(0.5 \cdot im\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 6.2)
   (+ 1.0 (* -0.5 (* re re)))
   (if (<= im 2e+165)
     (* 0.5 (* im (+ im (* -0.5 (* im (* re re))))))
     (* im (* 0.5 im)))))
double code(double re, double im) {
	double tmp;
	if (im <= 6.2) {
		tmp = 1.0 + (-0.5 * (re * re));
	} else if (im <= 2e+165) {
		tmp = 0.5 * (im * (im + (-0.5 * (im * (re * 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 <= 6.2d0) then
        tmp = 1.0d0 + ((-0.5d0) * (re * re))
    else if (im <= 2d+165) then
        tmp = 0.5d0 * (im * (im + ((-0.5d0) * (im * (re * 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 <= 6.2) {
		tmp = 1.0 + (-0.5 * (re * re));
	} else if (im <= 2e+165) {
		tmp = 0.5 * (im * (im + (-0.5 * (im * (re * re)))));
	} else {
		tmp = im * (0.5 * im);
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 6.2:
		tmp = 1.0 + (-0.5 * (re * re))
	elif im <= 2e+165:
		tmp = 0.5 * (im * (im + (-0.5 * (im * (re * re)))))
	else:
		tmp = im * (0.5 * im)
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 6.2)
		tmp = Float64(1.0 + Float64(-0.5 * Float64(re * re)));
	elseif (im <= 2e+165)
		tmp = Float64(0.5 * Float64(im * Float64(im + Float64(-0.5 * Float64(im * Float64(re * re))))));
	else
		tmp = Float64(im * Float64(0.5 * im));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 6.2)
		tmp = 1.0 + (-0.5 * (re * re));
	elseif (im <= 2e+165)
		tmp = 0.5 * (im * (im + (-0.5 * (im * (re * re)))));
	else
		tmp = im * (0.5 * im);
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 6.2], N[(1.0 + N[(-0.5 * N[(re * re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 2e+165], N[(0.5 * N[(im * N[(im + N[(-0.5 * N[(im * N[(re * re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(im * N[(0.5 * im), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(im, im, 2\right) + \left(-0.25 \cdot {re}^{2}\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
      9. distribute-rgt-out53.1%

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

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

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

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

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

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

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

        \[\leadsto 1 + \color{blue}{\left(2 \cdot -0.25\right) \cdot {re}^{2}} \]
      4. metadata-eval40.6%

        \[\leadsto 1 + \color{blue}{-0.5} \cdot {re}^{2} \]
      5. unpow240.6%

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

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

    if 6.20000000000000018 < im < 1.9999999999999998e165

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(2 + \color{blue}{im \cdot im}\right) \]
    4. Simplified15.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 15.4%

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

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

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

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

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

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

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

    if 1.9999999999999998e165 < 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. associate-*r*100.0%

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

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

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

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

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

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

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

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

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

Alternative 13: 38.5% accurate, 34.0× speedup?

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

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

\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.99999999999999899e164

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(im, im, 2\right) + \left(-0.25 \cdot {re}^{2}\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
      9. distribute-rgt-out49.7%

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

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

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

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

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

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

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

        \[\leadsto 1 + \color{blue}{\left(2 \cdot -0.25\right) \cdot {re}^{2}} \]
      4. metadata-eval37.4%

        \[\leadsto 1 + \color{blue}{-0.5} \cdot {re}^{2} \]
      5. unpow237.4%

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

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

    if 9.99999999999999899e164 < 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. associate-*r*100.0%

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

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

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

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

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

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

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

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

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

Alternative 14: 21.0% 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. Taylor expanded in im around 0 75.6%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\left(0.5 \cdot im\right) \cdot im} \]
    3. *-commutative24.9%

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

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

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

Reproduce

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