math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 8.1s
Alternatives: 18
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 18 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} im_m = \left|im\right| \\ \cos re \cdot \mathsf{fma}\left(0.5, e^{im\_m}, \frac{0.5}{e^{im\_m}}\right) \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (* (cos re) (fma 0.5 (exp im_m) (/ 0.5 (exp im_m)))))
im_m = fabs(im);
double code(double re, double im_m) {
	return cos(re) * fma(0.5, exp(im_m), (0.5 / exp(im_m)));
}
im_m = abs(im)
function code(re, im_m)
	return Float64(cos(re) * fma(0.5, exp(im_m), Float64(0.5 / exp(im_m))))
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := N[(N[Cos[re], $MachinePrecision] * N[(0.5 * N[Exp[im$95$m], $MachinePrecision] + N[(0.5 / N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|

\\
\cos re \cdot \mathsf{fma}\left(0.5, e^{im\_m}, \frac{0.5}{e^{im\_m}}\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Step-by-step derivation
    1. cos-neg100.0%

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

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

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

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

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

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

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

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

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

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

Alternative 2: 100.0% accurate, 1.0× speedup?

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

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

Alternative 3: 92.6% accurate, 1.4× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im\_m \leq 3.5 \lor \neg \left(im\_m \leq 5.6 \cdot 10^{+149}\right):\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \mathsf{fma}\left(im\_m, im\_m, 2\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 + 0.5 \cdot e^{im\_m}\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (or (<= im_m 3.5) (not (<= im_m 5.6e+149)))
   (* (* (cos re) 0.5) (fma im_m im_m 2.0))
   (+ 0.5 (* 0.5 (exp im_m)))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if ((im_m <= 3.5) || !(im_m <= 5.6e+149)) {
		tmp = (cos(re) * 0.5) * fma(im_m, im_m, 2.0);
	} else {
		tmp = 0.5 + (0.5 * exp(im_m));
	}
	return tmp;
}
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if ((im_m <= 3.5) || !(im_m <= 5.6e+149))
		tmp = Float64(Float64(cos(re) * 0.5) * fma(im_m, im_m, 2.0));
	else
		tmp = Float64(0.5 + Float64(0.5 * exp(im_m)));
	end
	return tmp
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[Or[LessEqual[im$95$m, 3.5], N[Not[LessEqual[im$95$m, 5.6e+149]], $MachinePrecision]], N[(N[(N[Cos[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(im$95$m * im$95$m + 2.0), $MachinePrecision]), $MachinePrecision], N[(0.5 + N[(0.5 * N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;im\_m \leq 3.5 \lor \neg \left(im\_m \leq 5.6 \cdot 10^{+149}\right):\\
\;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \mathsf{fma}\left(im\_m, im\_m, 2\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 3.5 or 5.5999999999999998e149 < 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 89.0%

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

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

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

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

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

    if 3.5 < im < 5.5999999999999998e149

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Step-by-step derivation
      1. cos-neg100.0%

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 3.5 \lor \neg \left(im \leq 5.6 \cdot 10^{+149}\right):\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 + 0.5 \cdot e^{im}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 92.5% accurate, 1.4× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im\_m \leq 1.6 \cdot 10^{-5}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im\_m \leq 5.6 \cdot 10^{+149}:\\ \;\;\;\;0.5 \cdot \left(e^{im\_m} + e^{-im\_m}\right)\\ \mathbf{else}:\\ \;\;\;\;\cos re \cdot \left(1 + im\_m \cdot \left(0.5 + im\_m \cdot 0.25\right)\right)\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= im_m 1.6e-5)
   (cos re)
   (if (<= im_m 5.6e+149)
     (* 0.5 (+ (exp im_m) (exp (- im_m))))
     (* (cos re) (+ 1.0 (* im_m (+ 0.5 (* im_m 0.25))))))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (im_m <= 1.6e-5) {
		tmp = cos(re);
	} else if (im_m <= 5.6e+149) {
		tmp = 0.5 * (exp(im_m) + exp(-im_m));
	} else {
		tmp = cos(re) * (1.0 + (im_m * (0.5 + (im_m * 0.25))));
	}
	return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    real(8) :: tmp
    if (im_m <= 1.6d-5) then
        tmp = cos(re)
    else if (im_m <= 5.6d+149) then
        tmp = 0.5d0 * (exp(im_m) + exp(-im_m))
    else
        tmp = cos(re) * (1.0d0 + (im_m * (0.5d0 + (im_m * 0.25d0))))
    end if
    code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if (im_m <= 1.6e-5) {
		tmp = Math.cos(re);
	} else if (im_m <= 5.6e+149) {
		tmp = 0.5 * (Math.exp(im_m) + Math.exp(-im_m));
	} else {
		tmp = Math.cos(re) * (1.0 + (im_m * (0.5 + (im_m * 0.25))));
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if im_m <= 1.6e-5:
		tmp = math.cos(re)
	elif im_m <= 5.6e+149:
		tmp = 0.5 * (math.exp(im_m) + math.exp(-im_m))
	else:
		tmp = math.cos(re) * (1.0 + (im_m * (0.5 + (im_m * 0.25))))
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (im_m <= 1.6e-5)
		tmp = cos(re);
	elseif (im_m <= 5.6e+149)
		tmp = Float64(0.5 * Float64(exp(im_m) + exp(Float64(-im_m))));
	else
		tmp = Float64(cos(re) * Float64(1.0 + Float64(im_m * Float64(0.5 + Float64(im_m * 0.25)))));
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (im_m <= 1.6e-5)
		tmp = cos(re);
	elseif (im_m <= 5.6e+149)
		tmp = 0.5 * (exp(im_m) + exp(-im_m));
	else
		tmp = cos(re) * (1.0 + (im_m * (0.5 + (im_m * 0.25))));
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[im$95$m, 1.6e-5], N[Cos[re], $MachinePrecision], If[LessEqual[im$95$m, 5.6e+149], N[(0.5 * N[(N[Exp[im$95$m], $MachinePrecision] + N[Exp[(-im$95$m)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[re], $MachinePrecision] * N[(1.0 + N[(im$95$m * N[(0.5 + N[(im$95$m * 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|

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

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

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


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

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Step-by-step derivation
      1. cos-neg100.0%

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

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

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

    if 1.59999999999999993e-5 < im < 5.5999999999999998e149

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

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

    if 5.5999999999999998e149 < im

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Step-by-step derivation
      1. cos-neg100.0%

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \left(1 + \left(0.5 \cdot im + \color{blue}{\left(0.25 \cdot im\right) \cdot im}\right)\right) \]
      11. distribute-rgt-out94.0%

        \[\leadsto \cos re \cdot \left(1 + \color{blue}{im \cdot \left(0.5 + 0.25 \cdot im\right)}\right) \]
      12. *-commutative94.0%

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

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

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

Alternative 5: 98.7% accurate, 1.5× speedup?

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

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

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Step-by-step derivation
    1. cos-neg100.0%

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

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

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

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

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

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

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

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

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

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

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

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

    \[\leadsto \color{blue}{\left(0.5 + 0.5 \cdot e^{im}\right) \cdot \cos re} \]
  9. Final simplification76.7%

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

Alternative 6: 92.3% accurate, 2.5× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im\_m \leq 2:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im\_m \leq 5.6 \cdot 10^{+149}:\\ \;\;\;\;0.5 + 0.5 \cdot e^{im\_m}\\ \mathbf{else}:\\ \;\;\;\;\cos re \cdot \left(1 + im\_m \cdot \left(0.5 + im\_m \cdot 0.25\right)\right)\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= im_m 2.0)
   (cos re)
   (if (<= im_m 5.6e+149)
     (+ 0.5 (* 0.5 (exp im_m)))
     (* (cos re) (+ 1.0 (* im_m (+ 0.5 (* im_m 0.25))))))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (im_m <= 2.0) {
		tmp = cos(re);
	} else if (im_m <= 5.6e+149) {
		tmp = 0.5 + (0.5 * exp(im_m));
	} else {
		tmp = cos(re) * (1.0 + (im_m * (0.5 + (im_m * 0.25))));
	}
	return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    real(8) :: tmp
    if (im_m <= 2.0d0) then
        tmp = cos(re)
    else if (im_m <= 5.6d+149) then
        tmp = 0.5d0 + (0.5d0 * exp(im_m))
    else
        tmp = cos(re) * (1.0d0 + (im_m * (0.5d0 + (im_m * 0.25d0))))
    end if
    code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if (im_m <= 2.0) {
		tmp = Math.cos(re);
	} else if (im_m <= 5.6e+149) {
		tmp = 0.5 + (0.5 * Math.exp(im_m));
	} else {
		tmp = Math.cos(re) * (1.0 + (im_m * (0.5 + (im_m * 0.25))));
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if im_m <= 2.0:
		tmp = math.cos(re)
	elif im_m <= 5.6e+149:
		tmp = 0.5 + (0.5 * math.exp(im_m))
	else:
		tmp = math.cos(re) * (1.0 + (im_m * (0.5 + (im_m * 0.25))))
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (im_m <= 2.0)
		tmp = cos(re);
	elseif (im_m <= 5.6e+149)
		tmp = Float64(0.5 + Float64(0.5 * exp(im_m)));
	else
		tmp = Float64(cos(re) * Float64(1.0 + Float64(im_m * Float64(0.5 + Float64(im_m * 0.25)))));
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (im_m <= 2.0)
		tmp = cos(re);
	elseif (im_m <= 5.6e+149)
		tmp = 0.5 + (0.5 * exp(im_m));
	else
		tmp = cos(re) * (1.0 + (im_m * (0.5 + (im_m * 0.25))));
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[im$95$m, 2.0], N[Cos[re], $MachinePrecision], If[LessEqual[im$95$m, 5.6e+149], N[(0.5 + N[(0.5 * N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[re], $MachinePrecision] * N[(1.0 + N[(im$95$m * N[(0.5 + N[(im$95$m * 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|

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

\mathbf{elif}\;im\_m \leq 5.6 \cdot 10^{+149}:\\
\;\;\;\;0.5 + 0.5 \cdot e^{im\_m}\\

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


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

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Step-by-step derivation
      1. cos-neg100.0%

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

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

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

    if 2 < im < 5.5999999999999998e149

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Step-by-step derivation
      1. cos-neg100.0%

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

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

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

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

    if 5.5999999999999998e149 < im

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Step-by-step derivation
      1. cos-neg100.0%

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \left(1 + \left(0.5 \cdot im + \color{blue}{\left(0.25 \cdot im\right) \cdot im}\right)\right) \]
      11. distribute-rgt-out94.0%

        \[\leadsto \cos re \cdot \left(1 + \color{blue}{im \cdot \left(0.5 + 0.25 \cdot im\right)}\right) \]
      12. *-commutative94.0%

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

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

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

Alternative 7: 69.8% accurate, 2.8× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im\_m \leq 1.6 \cdot 10^{-5}:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(im\_m, im\_m, 2\right)\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= im_m 1.6e-5) (cos re) (* 0.5 (fma im_m im_m 2.0))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (im_m <= 1.6e-5) {
		tmp = cos(re);
	} else {
		tmp = 0.5 * fma(im_m, im_m, 2.0);
	}
	return tmp;
}
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (im_m <= 1.6e-5)
		tmp = cos(re);
	else
		tmp = Float64(0.5 * fma(im_m, im_m, 2.0));
	end
	return tmp
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[im$95$m, 1.6e-5], N[Cos[re], $MachinePrecision], N[(0.5 * N[(im$95$m * im$95$m + 2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

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

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


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

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Step-by-step derivation
      1. cos-neg100.0%

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

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

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

    if 1.59999999999999993e-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 73.9%

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

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

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

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

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

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

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

Alternative 8: 87.2% accurate, 2.8× speedup?

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

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

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


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

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Step-by-step derivation
      1. cos-neg100.0%

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

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

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

    if 2.39999999999999991 < im

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Step-by-step derivation
      1. cos-neg100.0%

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 2.4:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;0.5 + 0.5 \cdot e^{im}\\ \end{array} \]
  5. Add Preprocessing

Alternative 9: 69.8% accurate, 2.9× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im\_m \leq 2.5 \cdot 10^{+32}:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;1 + im\_m \cdot \left(0.5 + im\_m \cdot 0.25\right)\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= im_m 2.5e+32) (cos re) (+ 1.0 (* im_m (+ 0.5 (* im_m 0.25))))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (im_m <= 2.5e+32) {
		tmp = cos(re);
	} else {
		tmp = 1.0 + (im_m * (0.5 + (im_m * 0.25)));
	}
	return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    real(8) :: tmp
    if (im_m <= 2.5d+32) then
        tmp = cos(re)
    else
        tmp = 1.0d0 + (im_m * (0.5d0 + (im_m * 0.25d0)))
    end if
    code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if (im_m <= 2.5e+32) {
		tmp = Math.cos(re);
	} else {
		tmp = 1.0 + (im_m * (0.5 + (im_m * 0.25)));
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if im_m <= 2.5e+32:
		tmp = math.cos(re)
	else:
		tmp = 1.0 + (im_m * (0.5 + (im_m * 0.25)))
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (im_m <= 2.5e+32)
		tmp = cos(re);
	else
		tmp = Float64(1.0 + Float64(im_m * Float64(0.5 + Float64(im_m * 0.25))));
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (im_m <= 2.5e+32)
		tmp = cos(re);
	else
		tmp = 1.0 + (im_m * (0.5 + (im_m * 0.25)));
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[im$95$m, 2.5e+32], N[Cos[re], $MachinePrecision], N[(1.0 + N[(im$95$m * N[(0.5 + N[(im$95$m * 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;im\_m \leq 2.5 \cdot 10^{+32}:\\
\;\;\;\;\cos re\\

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


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

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Step-by-step derivation
      1. cos-neg100.0%

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

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

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

    if 2.4999999999999999e32 < im

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Step-by-step derivation
      1. cos-neg100.0%

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

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

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

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

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

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

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

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

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

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

      \[\leadsto \color{blue}{0.5 + 0.5 \cdot e^{im}} \]
    7. Taylor expanded in im around 0 33.9%

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

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

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

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

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

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

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

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

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

Alternative 10: 47.4% accurate, 34.2× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ 1 + im\_m \cdot \left(0.5 + im\_m \cdot 0.25\right) \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 (+ 1.0 (* im_m (+ 0.5 (* im_m 0.25)))))
im_m = fabs(im);
double code(double re, double im_m) {
	return 1.0 + (im_m * (0.5 + (im_m * 0.25)));
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = 1.0d0 + (im_m * (0.5d0 + (im_m * 0.25d0)))
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return 1.0 + (im_m * (0.5 + (im_m * 0.25)));
}
im_m = math.fabs(im)
def code(re, im_m):
	return 1.0 + (im_m * (0.5 + (im_m * 0.25)))
im_m = abs(im)
function code(re, im_m)
	return Float64(1.0 + Float64(im_m * Float64(0.5 + Float64(im_m * 0.25))))
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = 1.0 + (im_m * (0.5 + (im_m * 0.25)));
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := N[(1.0 + N[(im$95$m * N[(0.5 + N[(im$95$m * 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|

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

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Step-by-step derivation
    1. cos-neg100.0%

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

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

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

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

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

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

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

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

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

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

    \[\leadsto \color{blue}{0.5 + 0.5 \cdot e^{im}} \]
  7. Taylor expanded in im around 0 48.5%

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{1 + im \cdot \left(0.5 + im \cdot 0.25\right)} \]
  10. Final simplification48.5%

    \[\leadsto 1 + im \cdot \left(0.5 + im \cdot 0.25\right) \]
  11. Add Preprocessing

Alternative 11: 29.2% accurate, 61.6× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ 1 + 0.5 \cdot im\_m \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 (+ 1.0 (* 0.5 im_m)))
im_m = fabs(im);
double code(double re, double im_m) {
	return 1.0 + (0.5 * im_m);
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = 1.0d0 + (0.5d0 * im_m)
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return 1.0 + (0.5 * im_m);
}
im_m = math.fabs(im)
def code(re, im_m):
	return 1.0 + (0.5 * im_m)
im_m = abs(im)
function code(re, im_m)
	return Float64(1.0 + Float64(0.5 * im_m))
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = 1.0 + (0.5 * im_m);
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := N[(1.0 + N[(0.5 * im$95$m), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|

\\
1 + 0.5 \cdot im\_m
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Step-by-step derivation
    1. cos-neg100.0%

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

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

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

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

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

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

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

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

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

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

    \[\leadsto \color{blue}{0.5 + 0.5 \cdot e^{im}} \]
  7. Taylor expanded in im around 0 31.0%

    \[\leadsto \color{blue}{1 + 0.5 \cdot im} \]
  8. Final simplification31.0%

    \[\leadsto 1 + 0.5 \cdot im \]
  9. Add Preprocessing

Alternative 12: 3.3% accurate, 308.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ -2 \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 -2.0)
im_m = fabs(im);
double code(double re, double im_m) {
	return -2.0;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = -2.0d0
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return -2.0;
}
im_m = math.fabs(im)
def code(re, im_m):
	return -2.0
im_m = abs(im)
function code(re, im_m)
	return -2.0
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = -2.0;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := -2.0
\begin{array}{l}
im_m = \left|im\right|

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

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Step-by-step derivation
    1. cos-neg100.0%

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

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

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

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

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

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

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

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

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

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

    \[\leadsto \color{blue}{-2} \]
  7. Final simplification3.3%

    \[\leadsto -2 \]
  8. Add Preprocessing

Alternative 13: 3.7% accurate, 308.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ -1 \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 -1.0)
im_m = fabs(im);
double code(double re, double im_m) {
	return -1.0;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = -1.0d0
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return -1.0;
}
im_m = math.fabs(im)
def code(re, im_m):
	return -1.0
im_m = abs(im)
function code(re, im_m)
	return -1.0
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = -1.0;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := -1.0
\begin{array}{l}
im_m = \left|im\right|

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

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Step-by-step derivation
    1. cos-neg100.0%

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

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

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

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

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

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

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

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

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

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

    \[\leadsto \color{blue}{-1} \]
  7. Final simplification3.7%

    \[\leadsto -1 \]
  8. Add Preprocessing

Alternative 14: 7.4% accurate, 308.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ 0.041666666666666664 \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 0.041666666666666664)
im_m = fabs(im);
double code(double re, double im_m) {
	return 0.041666666666666664;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = 0.041666666666666664d0
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return 0.041666666666666664;
}
im_m = math.fabs(im)
def code(re, im_m):
	return 0.041666666666666664
im_m = abs(im)
function code(re, im_m)
	return 0.041666666666666664
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = 0.041666666666666664;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := 0.041666666666666664
\begin{array}{l}
im_m = \left|im\right|

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

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Step-by-step derivation
    1. cos-neg100.0%

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

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

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

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

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

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

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

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

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

    \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0.5}\right) \]
  6. Applied egg-rr7.9%

    \[\leadsto \color{blue}{0.041666666666666664} \]
  7. Final simplification7.9%

    \[\leadsto 0.041666666666666664 \]
  8. Add Preprocessing

Alternative 15: 7.8% accurate, 308.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ 0.125 \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 0.125)
im_m = fabs(im);
double code(double re, double im_m) {
	return 0.125;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = 0.125d0
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return 0.125;
}
im_m = math.fabs(im)
def code(re, im_m):
	return 0.125
im_m = abs(im)
function code(re, im_m)
	return 0.125
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = 0.125;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := 0.125
\begin{array}{l}
im_m = \left|im\right|

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

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Step-by-step derivation
    1. cos-neg100.0%

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

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

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

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

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

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

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

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

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

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

    \[\leadsto \color{blue}{0.125} \]
  7. Final simplification8.3%

    \[\leadsto 0.125 \]
  8. Add Preprocessing

Alternative 16: 8.2% accurate, 308.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ 0.25 \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 0.25)
im_m = fabs(im);
double code(double re, double im_m) {
	return 0.25;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = 0.25d0
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return 0.25;
}
im_m = math.fabs(im)
def code(re, im_m):
	return 0.25
im_m = abs(im)
function code(re, im_m)
	return 0.25
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = 0.25;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := 0.25
\begin{array}{l}
im_m = \left|im\right|

\\
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. Step-by-step derivation
    1. cos-neg100.0%

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

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

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

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

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

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

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

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

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

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

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

    \[\leadsto 0.25 \]
  8. Add Preprocessing

Alternative 17: 8.7% accurate, 308.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ 0.5 \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 0.5)
im_m = fabs(im);
double code(double re, double im_m) {
	return 0.5;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = 0.5d0
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return 0.5;
}
im_m = math.fabs(im)
def code(re, im_m):
	return 0.5
im_m = abs(im)
function code(re, im_m)
	return 0.5
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = 0.5;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := 0.5
\begin{array}{l}
im_m = \left|im\right|

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

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Step-by-step derivation
    1. cos-neg100.0%

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

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

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

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

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

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

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

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

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

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

    \[\leadsto \color{blue}{0.5} \]
  7. Final simplification9.3%

    \[\leadsto 0.5 \]
  8. Add Preprocessing

Alternative 18: 28.6% accurate, 308.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ 1 \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 1.0)
im_m = fabs(im);
double code(double re, double im_m) {
	return 1.0;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = 1.0d0
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return 1.0;
}
im_m = math.fabs(im)
def code(re, im_m):
	return 1.0
im_m = abs(im)
function code(re, im_m)
	return 1.0
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = 1.0;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := 1.0
\begin{array}{l}
im_m = \left|im\right|

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

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Step-by-step derivation
    1. cos-neg100.0%

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

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

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

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

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

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

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

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

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

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

    \[\leadsto \color{blue}{0.5 + 0.5 \cdot e^{im}} \]
  7. Taylor expanded in im around 0 31.3%

    \[\leadsto \color{blue}{1} \]
  8. Final simplification31.3%

    \[\leadsto 1 \]
  9. Add Preprocessing

Reproduce

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