math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 7.0s
Alternatives: 15
Speedup: 1.0×

Specification

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

\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 15 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 100.0% accurate, 1.0× speedup?

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

\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

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

\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Add Preprocessing
  3. Final simplification100.0%

    \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  4. Add Preprocessing

Alternative 2: 62.2% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 260:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.95 \cdot 10^{+93}:\\ \;\;\;\;{re}^{-2}\\ \mathbf{elif}\;im \leq 1.3 \cdot 10^{+154}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(re\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 260.0)
   (cos re)
   (if (<= im 1.95e+93)
     (pow re -2.0)
     (if (<= im 1.3e+154) (log1p (expm1 re)) (* 0.5 (fma im im 2.0))))))
double code(double re, double im) {
	double tmp;
	if (im <= 260.0) {
		tmp = cos(re);
	} else if (im <= 1.95e+93) {
		tmp = pow(re, -2.0);
	} else if (im <= 1.3e+154) {
		tmp = log1p(expm1(re));
	} else {
		tmp = 0.5 * fma(im, im, 2.0);
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (im <= 260.0)
		tmp = cos(re);
	elseif (im <= 1.95e+93)
		tmp = re ^ -2.0;
	elseif (im <= 1.3e+154)
		tmp = log1p(expm1(re));
	else
		tmp = Float64(0.5 * fma(im, im, 2.0));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[im, 260.0], N[Cos[re], $MachinePrecision], If[LessEqual[im, 1.95e+93], N[Power[re, -2.0], $MachinePrecision], If[LessEqual[im, 1.3e+154], N[Log[1 + N[(Exp[re] - 1), $MachinePrecision]], $MachinePrecision], N[(0.5 * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

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

\mathbf{elif}\;im \leq 1.95 \cdot 10^{+93}:\\
\;\;\;\;{re}^{-2}\\

\mathbf{elif}\;im \leq 1.3 \cdot 10^{+154}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(re\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 < 260

    1. Initial program 100.0%

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

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

    if 260 < im < 1.9500000000000001e93

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{re}^{-2}} \]

    if 1.9500000000000001e93 < im < 1.29999999999999994e154

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{im}^{2} \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    12. Applied egg-rr41.7%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(re\right)\right)} \]

    if 1.29999999999999994e154 < im

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    4. Taylor expanded in im around 0 93.5%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 260:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.95 \cdot 10^{+93}:\\ \;\;\;\;{re}^{-2}\\ \mathbf{elif}\;im \leq 1.3 \cdot 10^{+154}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(re\right)\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 71.1% accurate, 1.4× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if im < 1.9000000000000001e-4

    1. Initial program 100.0%

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

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

    if 1.9000000000000001e-4 < im < 1.35000000000000003e154

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 4: 84.3% accurate, 1.4× speedup?

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

\\
\begin{array}{l}
t_0 := 0.5 \cdot \cos re\\
\mathbf{if}\;im \leq 0.155:\\
\;\;\;\;t\_0 \cdot \mathsf{fma}\left(im, im, 2\right)\\

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

\mathbf{else}:\\
\;\;\;\;t\_0 \cdot {im}^{2}\\


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

    1. Initial program 100.0%

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

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

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

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

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

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

    if 0.154999999999999999 < im < 1.35000000000000003e154

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 5: 68.1% accurate, 1.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 9.4 \cdot 10^{-5}:\\
\;\;\;\;\cos re\\

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


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

    1. Initial program 100.0%

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

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

    if 9.39999999999999945e-5 < im

    1. Initial program 100.0%

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

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

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

Alternative 6: 61.6% accurate, 2.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 260:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.2 \cdot 10^{+93}:\\ \;\;\;\;{re}^{-2}\\ \mathbf{elif}\;im \leq 2.6 \cdot 10^{+106}:\\ \;\;\;\;\mathsf{fma}\left(re, re, -2\right)\\ \mathbf{elif}\;im \leq 6.2 \cdot 10^{+115}:\\ \;\;\;\;{re}^{-2}\\ \mathbf{elif}\;im \leq 2.7 \cdot 10^{+153}:\\ \;\;\;\;-2 - re \cdot re\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 260.0)
   (cos re)
   (if (<= im 1.2e+93)
     (pow re -2.0)
     (if (<= im 2.6e+106)
       (fma re re -2.0)
       (if (<= im 6.2e+115)
         (pow re -2.0)
         (if (<= im 2.7e+153) (- -2.0 (* re re)) (* 0.5 (fma im im 2.0))))))))
double code(double re, double im) {
	double tmp;
	if (im <= 260.0) {
		tmp = cos(re);
	} else if (im <= 1.2e+93) {
		tmp = pow(re, -2.0);
	} else if (im <= 2.6e+106) {
		tmp = fma(re, re, -2.0);
	} else if (im <= 6.2e+115) {
		tmp = pow(re, -2.0);
	} else if (im <= 2.7e+153) {
		tmp = -2.0 - (re * re);
	} else {
		tmp = 0.5 * fma(im, im, 2.0);
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (im <= 260.0)
		tmp = cos(re);
	elseif (im <= 1.2e+93)
		tmp = re ^ -2.0;
	elseif (im <= 2.6e+106)
		tmp = fma(re, re, -2.0);
	elseif (im <= 6.2e+115)
		tmp = re ^ -2.0;
	elseif (im <= 2.7e+153)
		tmp = Float64(-2.0 - Float64(re * re));
	else
		tmp = Float64(0.5 * fma(im, im, 2.0));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[im, 260.0], N[Cos[re], $MachinePrecision], If[LessEqual[im, 1.2e+93], N[Power[re, -2.0], $MachinePrecision], If[LessEqual[im, 2.6e+106], N[(re * re + -2.0), $MachinePrecision], If[LessEqual[im, 6.2e+115], N[Power[re, -2.0], $MachinePrecision], If[LessEqual[im, 2.7e+153], N[(-2.0 - N[(re * re), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;im \leq 1.2 \cdot 10^{+93}:\\
\;\;\;\;{re}^{-2}\\

\mathbf{elif}\;im \leq 2.6 \cdot 10^{+106}:\\
\;\;\;\;\mathsf{fma}\left(re, re, -2\right)\\

\mathbf{elif}\;im \leq 6.2 \cdot 10^{+115}:\\
\;\;\;\;{re}^{-2}\\

\mathbf{elif}\;im \leq 2.7 \cdot 10^{+153}:\\
\;\;\;\;-2 - re \cdot re\\

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


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

    1. Initial program 100.0%

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

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

    if 260 < im < 1.20000000000000005e93 or 2.6000000000000002e106 < im < 6.2000000000000001e115

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{re}^{-2}} \]

    if 1.20000000000000005e93 < im < 2.6000000000000002e106

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{im}^{2} \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    12. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(re, re, -2\right)} \]

    if 6.2000000000000001e115 < im < 2.7000000000000001e153

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.7000000000000001e153 < im

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    4. Taylor expanded in im around 0 93.5%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 260:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.2 \cdot 10^{+93}:\\ \;\;\;\;{re}^{-2}\\ \mathbf{elif}\;im \leq 2.6 \cdot 10^{+106}:\\ \;\;\;\;\mathsf{fma}\left(re, re, -2\right)\\ \mathbf{elif}\;im \leq 6.2 \cdot 10^{+115}:\\ \;\;\;\;{re}^{-2}\\ \mathbf{elif}\;im \leq 2.7 \cdot 10^{+153}:\\ \;\;\;\;-2 - re \cdot re\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 61.6% accurate, 2.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 242:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 9.8 \cdot 10^{+92}:\\ \;\;\;\;{re}^{-2}\\ \mathbf{elif}\;im \leq 4.2 \cdot 10^{+108}:\\ \;\;\;\;\mathsf{fma}\left(re, re, -2\right)\\ \mathbf{elif}\;im \leq 1.4 \cdot 10^{+116}:\\ \;\;\;\;{re}^{-2}\\ \mathbf{elif}\;im \leq 7.2 \cdot 10^{+152}:\\ \;\;\;\;-2 - re \cdot re\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot {im}^{2}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 242.0)
   (cos re)
   (if (<= im 9.8e+92)
     (pow re -2.0)
     (if (<= im 4.2e+108)
       (fma re re -2.0)
       (if (<= im 1.4e+116)
         (pow re -2.0)
         (if (<= im 7.2e+152) (- -2.0 (* re re)) (* 0.5 (pow im 2.0))))))))
double code(double re, double im) {
	double tmp;
	if (im <= 242.0) {
		tmp = cos(re);
	} else if (im <= 9.8e+92) {
		tmp = pow(re, -2.0);
	} else if (im <= 4.2e+108) {
		tmp = fma(re, re, -2.0);
	} else if (im <= 1.4e+116) {
		tmp = pow(re, -2.0);
	} else if (im <= 7.2e+152) {
		tmp = -2.0 - (re * re);
	} else {
		tmp = 0.5 * pow(im, 2.0);
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (im <= 242.0)
		tmp = cos(re);
	elseif (im <= 9.8e+92)
		tmp = re ^ -2.0;
	elseif (im <= 4.2e+108)
		tmp = fma(re, re, -2.0);
	elseif (im <= 1.4e+116)
		tmp = re ^ -2.0;
	elseif (im <= 7.2e+152)
		tmp = Float64(-2.0 - Float64(re * re));
	else
		tmp = Float64(0.5 * (im ^ 2.0));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[im, 242.0], N[Cos[re], $MachinePrecision], If[LessEqual[im, 9.8e+92], N[Power[re, -2.0], $MachinePrecision], If[LessEqual[im, 4.2e+108], N[(re * re + -2.0), $MachinePrecision], If[LessEqual[im, 1.4e+116], N[Power[re, -2.0], $MachinePrecision], If[LessEqual[im, 7.2e+152], N[(-2.0 - N[(re * re), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;im \leq 9.8 \cdot 10^{+92}:\\
\;\;\;\;{re}^{-2}\\

\mathbf{elif}\;im \leq 4.2 \cdot 10^{+108}:\\
\;\;\;\;\mathsf{fma}\left(re, re, -2\right)\\

\mathbf{elif}\;im \leq 1.4 \cdot 10^{+116}:\\
\;\;\;\;{re}^{-2}\\

\mathbf{elif}\;im \leq 7.2 \cdot 10^{+152}:\\
\;\;\;\;-2 - re \cdot re\\

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


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

    1. Initial program 100.0%

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

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

    if 242 < im < 9.8000000000000003e92 or 4.20000000000000019e108 < im < 1.40000000000000002e116

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{re}^{-2}} \]

    if 9.8000000000000003e92 < im < 4.20000000000000019e108

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{im}^{2} \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    12. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(re, re, -2\right)} \]

    if 1.40000000000000002e116 < im < 7.1999999999999998e152

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 7.1999999999999998e152 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{{im}^{2} \cdot 0.5} \]
    11. Simplified93.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 242:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 9.8 \cdot 10^{+92}:\\ \;\;\;\;{re}^{-2}\\ \mathbf{elif}\;im \leq 4.2 \cdot 10^{+108}:\\ \;\;\;\;\mathsf{fma}\left(re, re, -2\right)\\ \mathbf{elif}\;im \leq 1.4 \cdot 10^{+116}:\\ \;\;\;\;{re}^{-2}\\ \mathbf{elif}\;im \leq 7.2 \cdot 10^{+152}:\\ \;\;\;\;-2 - re \cdot re\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot {im}^{2}\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 55.3% accurate, 2.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 260:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.56 \cdot 10^{+93}:\\ \;\;\;\;{re}^{-2}\\ \mathbf{elif}\;im \leq 2.5 \cdot 10^{+106}:\\ \;\;\;\;\mathsf{fma}\left(re, re, -2\right)\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{+152}:\\ \;\;\;\;-2 - re \cdot re\\ \mathbf{elif}\;im \leq 9.8 \cdot 10^{+179}:\\ \;\;\;\;\mathsf{fma}\left(re, re, -2\right)\\ \mathbf{else}:\\ \;\;\;\;{re}^{-2}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 260.0)
   (cos re)
   (if (<= im 1.56e+93)
     (pow re -2.0)
     (if (<= im 2.5e+106)
       (fma re re -2.0)
       (if (<= im 1.05e+152)
         (- -2.0 (* re re))
         (if (<= im 9.8e+179) (fma re re -2.0) (pow re -2.0)))))))
double code(double re, double im) {
	double tmp;
	if (im <= 260.0) {
		tmp = cos(re);
	} else if (im <= 1.56e+93) {
		tmp = pow(re, -2.0);
	} else if (im <= 2.5e+106) {
		tmp = fma(re, re, -2.0);
	} else if (im <= 1.05e+152) {
		tmp = -2.0 - (re * re);
	} else if (im <= 9.8e+179) {
		tmp = fma(re, re, -2.0);
	} else {
		tmp = pow(re, -2.0);
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (im <= 260.0)
		tmp = cos(re);
	elseif (im <= 1.56e+93)
		tmp = re ^ -2.0;
	elseif (im <= 2.5e+106)
		tmp = fma(re, re, -2.0);
	elseif (im <= 1.05e+152)
		tmp = Float64(-2.0 - Float64(re * re));
	elseif (im <= 9.8e+179)
		tmp = fma(re, re, -2.0);
	else
		tmp = re ^ -2.0;
	end
	return tmp
end
code[re_, im_] := If[LessEqual[im, 260.0], N[Cos[re], $MachinePrecision], If[LessEqual[im, 1.56e+93], N[Power[re, -2.0], $MachinePrecision], If[LessEqual[im, 2.5e+106], N[(re * re + -2.0), $MachinePrecision], If[LessEqual[im, 1.05e+152], N[(-2.0 - N[(re * re), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 9.8e+179], N[(re * re + -2.0), $MachinePrecision], N[Power[re, -2.0], $MachinePrecision]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;im \leq 1.56 \cdot 10^{+93}:\\
\;\;\;\;{re}^{-2}\\

\mathbf{elif}\;im \leq 2.5 \cdot 10^{+106}:\\
\;\;\;\;\mathsf{fma}\left(re, re, -2\right)\\

\mathbf{elif}\;im \leq 1.05 \cdot 10^{+152}:\\
\;\;\;\;-2 - re \cdot re\\

\mathbf{elif}\;im \leq 9.8 \cdot 10^{+179}:\\
\;\;\;\;\mathsf{fma}\left(re, re, -2\right)\\

\mathbf{else}:\\
\;\;\;\;{re}^{-2}\\


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

    1. Initial program 100.0%

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

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

    if 260 < im < 1.56000000000000004e93 or 9.7999999999999997e179 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{re}^{-2}} \]

    if 1.56000000000000004e93 < im < 2.4999999999999999e106 or 1.0500000000000001e152 < im < 9.7999999999999997e179

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(re, re, -2\right)} \]

    if 2.4999999999999999e106 < im < 1.0500000000000001e152

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 260:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.56 \cdot 10^{+93}:\\ \;\;\;\;{re}^{-2}\\ \mathbf{elif}\;im \leq 2.5 \cdot 10^{+106}:\\ \;\;\;\;\mathsf{fma}\left(re, re, -2\right)\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{+152}:\\ \;\;\;\;-2 - re \cdot re\\ \mathbf{elif}\;im \leq 9.8 \cdot 10^{+179}:\\ \;\;\;\;\mathsf{fma}\left(re, re, -2\right)\\ \mathbf{else}:\\ \;\;\;\;{re}^{-2}\\ \end{array} \]
  5. Add Preprocessing

Alternative 9: 61.6% accurate, 2.5× speedup?

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

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

\mathbf{elif}\;im \leq 8.4 \cdot 10^{+92}:\\
\;\;\;\;{re}^{-2}\\

\mathbf{elif}\;im \leq 1.4 \cdot 10^{+153}:\\
\;\;\;\;0.25 + 0.25 \cdot {re}^{2}\\

\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 < 250

    1. Initial program 100.0%

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

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

    if 250 < im < 8.39999999999999944e92

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{re}^{-2}} \]

    if 8.39999999999999944e92 < im < 1.39999999999999993e153

    1. Initial program 100.0%

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

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

      \[\leadsto \color{blue}{0.25 + 0.25 \cdot {re}^{2}} \]
    5. Step-by-step derivation
      1. *-commutative30.8%

        \[\leadsto 0.25 + \color{blue}{{re}^{2} \cdot 0.25} \]
    6. Simplified30.8%

      \[\leadsto \color{blue}{0.25 + {re}^{2} \cdot 0.25} \]

    if 1.39999999999999993e153 < im

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{0.5 \cdot \left(e^{im} + e^{-im}\right)} \]
    4. Taylor expanded in im around 0 93.5%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 250:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 8.4 \cdot 10^{+92}:\\ \;\;\;\;{re}^{-2}\\ \mathbf{elif}\;im \leq 1.4 \cdot 10^{+153}:\\ \;\;\;\;0.25 + 0.25 \cdot {re}^{2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 10: 55.5% accurate, 2.6× speedup?

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

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

\mathbf{elif}\;im \leq 1.1 \cdot 10^{+116} \lor \neg \left(im \leq 2.85 \cdot 10^{+165}\right):\\
\;\;\;\;{re}^{-2}\\

\mathbf{else}:\\
\;\;\;\;-2 - re \cdot re\\


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

    1. Initial program 100.0%

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

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

    if 260 < im < 1.1e116 or 2.85000000000000013e165 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{im}^{2} \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    12. Applied egg-rr23.0%

      \[\leadsto \color{blue}{{re}^{-2}} \]

    if 1.1e116 < im < 2.85000000000000013e165

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{im}^{2} \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    12. Applied egg-rr20.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 260:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.1 \cdot 10^{+116} \lor \neg \left(im \leq 2.85 \cdot 10^{+165}\right):\\ \;\;\;\;{re}^{-2}\\ \mathbf{else}:\\ \;\;\;\;-2 - re \cdot re\\ \end{array} \]
  5. Add Preprocessing

Alternative 11: 53.1% accurate, 2.9× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;-2 - re \cdot re\\


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

    1. Initial program 100.0%

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

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

    if 3.8e18 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{im}^{2} \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    12. Applied egg-rr8.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 3.8 \cdot 10^{+18}:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;-2 - re \cdot re\\ \end{array} \]
  5. Add Preprocessing

Alternative 12: 31.1% accurate, 30.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 4.8 \cdot 10^{+20}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;-2 - re \cdot re\\


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

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\frac{\cos re \cdot -2}{\cos re \cdot -2 + \left(\cos re \cdot -2 - \cos re \cdot -2\right)}} \]
    4. Step-by-step derivation
      1. +-inverses33.8%

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

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

        \[\leadsto \color{blue}{1} \]
    5. Simplified33.8%

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

    if 4.8e20 < im

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{im}^{2} \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    12. Applied egg-rr8.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 4.8 \cdot 10^{+20}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-2 - re \cdot re\\ \end{array} \]
  5. Add Preprocessing

Alternative 13: 2.3% accurate, 308.0× speedup?

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

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

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

    \[\leadsto \color{blue}{\log \left({1}^{\cos re}\right)} \]
  4. Step-by-step derivation
    1. pow-base-12.2%

      \[\leadsto \log \color{blue}{1} \]
    2. metadata-eval2.2%

      \[\leadsto \color{blue}{0} \]
  5. Simplified2.2%

    \[\leadsto \color{blue}{0} \]
  6. Final simplification2.2%

    \[\leadsto 0 \]
  7. Add Preprocessing

Alternative 14: 8.0% accurate, 308.0× speedup?

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

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

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

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

    \[\leadsto \color{blue}{0.25} \]
  5. Final simplification7.6%

    \[\leadsto 0.25 \]
  6. Add Preprocessing

Alternative 15: 28.3% accurate, 308.0× speedup?

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

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

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

    \[\leadsto \color{blue}{\frac{\cos re \cdot -2}{\cos re \cdot -2 + \left(\cos re \cdot -2 - \cos re \cdot -2\right)}} \]
  4. Step-by-step derivation
    1. +-inverses26.0%

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

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

      \[\leadsto \color{blue}{1} \]
  5. Simplified26.0%

    \[\leadsto \color{blue}{1} \]
  6. Final simplification26.0%

    \[\leadsto 1 \]
  7. Add Preprocessing

Reproduce

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