math.cos on complex, real part

Percentage Accurate: 100.0% → 99.7%
Time: 7.0s
Alternatives: 13
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 13 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: 99.7% accurate, 0.7× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;t\_0 \cdot \left(e^{im\_m} + 3\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)) < 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 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)} \]

    if 4 < (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))

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

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

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

Alternative 2: 99.4% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;e^{-im\_m} + e^{im\_m} \leq 4:\\
\;\;\;\;\cos re\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)) < 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 99.8%

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

    if 4 < (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))

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

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

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

Alternative 3: 100.0% accurate, 1.0× speedup?

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

Alternative 4: 94.8% accurate, 2.4× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im\_m \leq 1.26 \cdot 10^{-6}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im\_m \leq 1.02 \cdot 10^{+103}:\\ \;\;\;\;0.5 \cdot \left(e^{im\_m} + \left(1 + im\_m \cdot \left(im\_m \cdot \left(0.5 + im\_m \cdot -0.16666666666666666\right) + -1\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(4 + im\_m \cdot \left(1 + im\_m \cdot \left(0.5 + im\_m \cdot 0.16666666666666666\right)\right)\right)\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= im_m 1.26e-6)
   (cos re)
   (if (<= im_m 1.02e+103)
     (*
      0.5
      (+
       (exp im_m)
       (+
        1.0
        (* im_m (+ (* im_m (+ 0.5 (* im_m -0.16666666666666666))) -1.0)))))
     (*
      (* 0.5 (cos re))
      (+
       4.0
       (* im_m (+ 1.0 (* im_m (+ 0.5 (* im_m 0.16666666666666666))))))))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (im_m <= 1.26e-6) {
		tmp = cos(re);
	} else if (im_m <= 1.02e+103) {
		tmp = 0.5 * (exp(im_m) + (1.0 + (im_m * ((im_m * (0.5 + (im_m * -0.16666666666666666))) + -1.0))));
	} else {
		tmp = (0.5 * cos(re)) * (4.0 + (im_m * (1.0 + (im_m * (0.5 + (im_m * 0.16666666666666666))))));
	}
	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.26d-6) then
        tmp = cos(re)
    else if (im_m <= 1.02d+103) then
        tmp = 0.5d0 * (exp(im_m) + (1.0d0 + (im_m * ((im_m * (0.5d0 + (im_m * (-0.16666666666666666d0)))) + (-1.0d0)))))
    else
        tmp = (0.5d0 * cos(re)) * (4.0d0 + (im_m * (1.0d0 + (im_m * (0.5d0 + (im_m * 0.16666666666666666d0))))))
    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.26e-6) {
		tmp = Math.cos(re);
	} else if (im_m <= 1.02e+103) {
		tmp = 0.5 * (Math.exp(im_m) + (1.0 + (im_m * ((im_m * (0.5 + (im_m * -0.16666666666666666))) + -1.0))));
	} else {
		tmp = (0.5 * Math.cos(re)) * (4.0 + (im_m * (1.0 + (im_m * (0.5 + (im_m * 0.16666666666666666))))));
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if im_m <= 1.26e-6:
		tmp = math.cos(re)
	elif im_m <= 1.02e+103:
		tmp = 0.5 * (math.exp(im_m) + (1.0 + (im_m * ((im_m * (0.5 + (im_m * -0.16666666666666666))) + -1.0))))
	else:
		tmp = (0.5 * math.cos(re)) * (4.0 + (im_m * (1.0 + (im_m * (0.5 + (im_m * 0.16666666666666666))))))
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (im_m <= 1.26e-6)
		tmp = cos(re);
	elseif (im_m <= 1.02e+103)
		tmp = Float64(0.5 * Float64(exp(im_m) + Float64(1.0 + Float64(im_m * Float64(Float64(im_m * Float64(0.5 + Float64(im_m * -0.16666666666666666))) + -1.0)))));
	else
		tmp = Float64(Float64(0.5 * cos(re)) * Float64(4.0 + Float64(im_m * Float64(1.0 + Float64(im_m * Float64(0.5 + Float64(im_m * 0.16666666666666666)))))));
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (im_m <= 1.26e-6)
		tmp = cos(re);
	elseif (im_m <= 1.02e+103)
		tmp = 0.5 * (exp(im_m) + (1.0 + (im_m * ((im_m * (0.5 + (im_m * -0.16666666666666666))) + -1.0))));
	else
		tmp = (0.5 * cos(re)) * (4.0 + (im_m * (1.0 + (im_m * (0.5 + (im_m * 0.16666666666666666))))));
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[im$95$m, 1.26e-6], N[Cos[re], $MachinePrecision], If[LessEqual[im$95$m, 1.02e+103], N[(0.5 * N[(N[Exp[im$95$m], $MachinePrecision] + N[(1.0 + N[(im$95$m * N[(N[(im$95$m * N[(0.5 + N[(im$95$m * -0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(4.0 + N[(im$95$m * N[(1.0 + N[(im$95$m * N[(0.5 + N[(im$95$m * 0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|

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

\mathbf{elif}\;im\_m \leq 1.02 \cdot 10^{+103}:\\
\;\;\;\;0.5 \cdot \left(e^{im\_m} + \left(1 + im\_m \cdot \left(im\_m \cdot \left(0.5 + im\_m \cdot -0.16666666666666666\right) + -1\right)\right)\right)\\

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


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

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

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

    if 1.26000000000000001e-6 < im < 1.01999999999999991e103

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

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

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

    if 1.01999999999999991e103 < im

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1.26 \cdot 10^{-6}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.02 \cdot 10^{+103}:\\ \;\;\;\;0.5 \cdot \left(e^{im} + \left(1 + im \cdot \left(im \cdot \left(0.5 + im \cdot -0.16666666666666666\right) + -1\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(4 + im \cdot \left(1 + im \cdot \left(0.5 + im \cdot 0.16666666666666666\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 92.6% accurate, 2.6× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im\_m \leq 2.4:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im\_m \leq 2.7 \cdot 10^{+154}:\\ \;\;\;\;0.5 \cdot e^{im\_m} + 1.5\\ \mathbf{else}:\\ \;\;\;\;\cos re \cdot \left(2 + im\_m \cdot \left(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.4)
   (cos re)
   (if (<= im_m 2.7e+154)
     (+ (* 0.5 (exp im_m)) 1.5)
     (* (cos re) (+ 2.0 (* im_m (* im_m 0.25)))))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (im_m <= 2.4) {
		tmp = cos(re);
	} else if (im_m <= 2.7e+154) {
		tmp = (0.5 * exp(im_m)) + 1.5;
	} else {
		tmp = cos(re) * (2.0 + (im_m * (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.4d0) then
        tmp = cos(re)
    else if (im_m <= 2.7d+154) then
        tmp = (0.5d0 * exp(im_m)) + 1.5d0
    else
        tmp = cos(re) * (2.0d0 + (im_m * (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.4) {
		tmp = Math.cos(re);
	} else if (im_m <= 2.7e+154) {
		tmp = (0.5 * Math.exp(im_m)) + 1.5;
	} else {
		tmp = Math.cos(re) * (2.0 + (im_m * (im_m * 0.25)));
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if im_m <= 2.4:
		tmp = math.cos(re)
	elif im_m <= 2.7e+154:
		tmp = (0.5 * math.exp(im_m)) + 1.5
	else:
		tmp = math.cos(re) * (2.0 + (im_m * (im_m * 0.25)))
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (im_m <= 2.4)
		tmp = cos(re);
	elseif (im_m <= 2.7e+154)
		tmp = Float64(Float64(0.5 * exp(im_m)) + 1.5);
	else
		tmp = Float64(cos(re) * Float64(2.0 + Float64(im_m * 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.4)
		tmp = cos(re);
	elseif (im_m <= 2.7e+154)
		tmp = (0.5 * exp(im_m)) + 1.5;
	else
		tmp = cos(re) * (2.0 + (im_m * (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.4], N[Cos[re], $MachinePrecision], If[LessEqual[im$95$m, 2.7e+154], N[(N[(0.5 * N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision] + 1.5), $MachinePrecision], N[(N[Cos[re], $MachinePrecision] * N[(2.0 + N[(im$95$m * 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.4:\\
\;\;\;\;\cos re\\

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

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


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

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

    if 2.39999999999999991 < im < 2.70000000000000006e154

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

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

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

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

        \[\leadsto \color{blue}{0.5 \cdot e^{im} + 0.5 \cdot 3} \]
      3. metadata-eval85.3%

        \[\leadsto 0.5 \cdot e^{im} + \color{blue}{1.5} \]
    6. Simplified85.3%

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

    if 2.70000000000000006e154 < im

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos re \cdot \left(2 + im \cdot \color{blue}{\left(im \cdot 0.25\right)}\right) \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 6: 86.9% accurate, 2.8× speedup?

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

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

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


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

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

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

    if 2.10000000000000009 < im

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{im} + 3\right)} \]
      2. distribute-lft-in76.7%

        \[\leadsto \color{blue}{0.5 \cdot e^{im} + 0.5 \cdot 3} \]
      3. metadata-eval76.7%

        \[\leadsto 0.5 \cdot e^{im} + \color{blue}{1.5} \]
    6. Simplified76.7%

      \[\leadsto \color{blue}{0.5 \cdot e^{im} + 1.5} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 7: 74.8% accurate, 2.9× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im\_m \leq 1.55 \cdot 10^{+37}:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;2 + im\_m \cdot \left(0.5 + im\_m \cdot \left(0.25 + im\_m \cdot 0.08333333333333333\right)\right)\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= im_m 1.55e+37)
   (cos re)
   (+ 2.0 (* im_m (+ 0.5 (* im_m (+ 0.25 (* im_m 0.08333333333333333))))))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (im_m <= 1.55e+37) {
		tmp = cos(re);
	} else {
		tmp = 2.0 + (im_m * (0.5 + (im_m * (0.25 + (im_m * 0.08333333333333333)))));
	}
	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.55d+37) then
        tmp = cos(re)
    else
        tmp = 2.0d0 + (im_m * (0.5d0 + (im_m * (0.25d0 + (im_m * 0.08333333333333333d0)))))
    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.55e+37) {
		tmp = Math.cos(re);
	} else {
		tmp = 2.0 + (im_m * (0.5 + (im_m * (0.25 + (im_m * 0.08333333333333333)))));
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if im_m <= 1.55e+37:
		tmp = math.cos(re)
	else:
		tmp = 2.0 + (im_m * (0.5 + (im_m * (0.25 + (im_m * 0.08333333333333333)))))
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (im_m <= 1.55e+37)
		tmp = cos(re);
	else
		tmp = Float64(2.0 + Float64(im_m * Float64(0.5 + Float64(im_m * Float64(0.25 + Float64(im_m * 0.08333333333333333))))));
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (im_m <= 1.55e+37)
		tmp = cos(re);
	else
		tmp = 2.0 + (im_m * (0.5 + (im_m * (0.25 + (im_m * 0.08333333333333333)))));
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[im$95$m, 1.55e+37], N[Cos[re], $MachinePrecision], N[(2.0 + N[(im$95$m * N[(0.5 + N[(im$95$m * N[(0.25 + N[(im$95$m * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

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

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


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

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

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

    if 1.5500000000000001e37 < im

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{im} + 3\right)} \]
      2. distribute-lft-in78.0%

        \[\leadsto \color{blue}{0.5 \cdot e^{im} + 0.5 \cdot 3} \]
      3. metadata-eval78.0%

        \[\leadsto 0.5 \cdot e^{im} + \color{blue}{1.5} \]
    6. Simplified78.0%

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

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

        \[\leadsto \color{blue}{im \cdot \left(0.5 + im \cdot \left(0.25 + 0.08333333333333333 \cdot im\right)\right) + 2} \]
      2. *-commutative58.8%

        \[\leadsto im \cdot \left(0.5 + im \cdot \left(0.25 + \color{blue}{im \cdot 0.08333333333333333}\right)\right) + 2 \]
    9. Simplified58.8%

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

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

Alternative 8: 52.7% accurate, 17.1× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im\_m \leq 1.5:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;2 + im\_m \cdot \left(0.5 + im\_m \cdot \left(0.25 + im\_m \cdot 0.08333333333333333\right)\right)\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= im_m 1.5)
   1.0
   (+ 2.0 (* im_m (+ 0.5 (* im_m (+ 0.25 (* im_m 0.08333333333333333))))))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (im_m <= 1.5) {
		tmp = 1.0;
	} else {
		tmp = 2.0 + (im_m * (0.5 + (im_m * (0.25 + (im_m * 0.08333333333333333)))));
	}
	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.5d0) then
        tmp = 1.0d0
    else
        tmp = 2.0d0 + (im_m * (0.5d0 + (im_m * (0.25d0 + (im_m * 0.08333333333333333d0)))))
    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.5) {
		tmp = 1.0;
	} else {
		tmp = 2.0 + (im_m * (0.5 + (im_m * (0.25 + (im_m * 0.08333333333333333)))));
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if im_m <= 1.5:
		tmp = 1.0
	else:
		tmp = 2.0 + (im_m * (0.5 + (im_m * (0.25 + (im_m * 0.08333333333333333)))))
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (im_m <= 1.5)
		tmp = 1.0;
	else
		tmp = Float64(2.0 + Float64(im_m * Float64(0.5 + Float64(im_m * Float64(0.25 + Float64(im_m * 0.08333333333333333))))));
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (im_m <= 1.5)
		tmp = 1.0;
	else
		tmp = 2.0 + (im_m * (0.5 + (im_m * (0.25 + (im_m * 0.08333333333333333)))));
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[im$95$m, 1.5], 1.0, N[(2.0 + N[(im$95$m * N[(0.5 + N[(im$95$m * N[(0.25 + N[(im$95$m * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;im\_m \leq 1.5:\\
\;\;\;\;1\\

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


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

      \[\leadsto \color{blue}{\cos re} \]
    4. Taylor expanded in re around 0 35.6%

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

    if 1.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. Applied egg-rr100.0%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{im} + 3\right)} \]
      2. distribute-lft-in76.7%

        \[\leadsto \color{blue}{0.5 \cdot e^{im} + 0.5 \cdot 3} \]
      3. metadata-eval76.7%

        \[\leadsto 0.5 \cdot e^{im} + \color{blue}{1.5} \]
    6. Simplified76.7%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1.5:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;2 + im \cdot \left(0.5 + im \cdot \left(0.25 + im \cdot 0.08333333333333333\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 9: 47.1% accurate, 22.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im\_m \leq 1.2:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;2 + 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 1.2) 1.0 (+ 2.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.2) {
		tmp = 1.0;
	} else {
		tmp = 2.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.2d0) then
        tmp = 1.0d0
    else
        tmp = 2.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.2) {
		tmp = 1.0;
	} else {
		tmp = 2.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.2:
		tmp = 1.0
	else:
		tmp = 2.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.2)
		tmp = 1.0;
	else
		tmp = Float64(2.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.2)
		tmp = 1.0;
	else
		tmp = 2.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.2], 1.0, N[(2.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 1.2:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;2 + 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 < 1.19999999999999996

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

      \[\leadsto \color{blue}{\cos re} \]
    4. Taylor expanded in re around 0 35.6%

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

    if 1.19999999999999996 < im

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{im} + 3\right)} \]
      2. distribute-lft-in76.7%

        \[\leadsto \color{blue}{0.5 \cdot e^{im} + 0.5 \cdot 3} \]
      3. metadata-eval76.7%

        \[\leadsto 0.5 \cdot e^{im} + \color{blue}{1.5} \]
    6. Simplified76.7%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1.2:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;2 + im \cdot \left(0.5 + im \cdot 0.25\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 10: 35.1% accurate, 25.6× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im\_m \leq 310:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;1 + \left(re \cdot re\right) \cdot -0.5\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= im_m 310.0) 1.0 (+ 1.0 (* (* re re) -0.5))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (im_m <= 310.0) {
		tmp = 1.0;
	} else {
		tmp = 1.0 + ((re * re) * -0.5);
	}
	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 <= 310.0d0) then
        tmp = 1.0d0
    else
        tmp = 1.0d0 + ((re * re) * (-0.5d0))
    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 <= 310.0) {
		tmp = 1.0;
	} else {
		tmp = 1.0 + ((re * re) * -0.5);
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if im_m <= 310.0:
		tmp = 1.0
	else:
		tmp = 1.0 + ((re * re) * -0.5)
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (im_m <= 310.0)
		tmp = 1.0;
	else
		tmp = Float64(1.0 + Float64(Float64(re * re) * -0.5));
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (im_m <= 310.0)
		tmp = 1.0;
	else
		tmp = 1.0 + ((re * re) * -0.5);
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[im$95$m, 310.0], 1.0, N[(1.0 + N[(N[(re * re), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;im\_m \leq 310:\\
\;\;\;\;1\\

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


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

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

      \[\leadsto \color{blue}{\cos re} \]
    4. Taylor expanded in re around 0 35.5%

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

    if 310 < 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 3.1%

      \[\leadsto \color{blue}{\cos re} \]
    4. Taylor expanded in re around 0 12.8%

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

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

      \[\leadsto \color{blue}{1 + {re}^{2} \cdot -0.5} \]
    7. Step-by-step derivation
      1. unpow212.8%

        \[\leadsto 1 + \color{blue}{\left(re \cdot re\right)} \cdot -0.5 \]
    8. Applied egg-rr12.8%

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

Alternative 11: 28.9% 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. Add Preprocessing
  3. Taylor expanded in im around 0 48.8%

    \[\leadsto \color{blue}{\cos re} \]
  4. Taylor expanded in re around 0 26.9%

    \[\leadsto \color{blue}{1} \]
  5. Add Preprocessing

Alternative 12: 9.1% accurate, 308.0× speedup?

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

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

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

    \[\leadsto \color{blue}{0.5} \cdot \left(e^{-im} + e^{im}\right) \]
  4. Applied egg-rr9.0%

    \[\leadsto 0.5 \cdot \color{blue}{1.5} \]
  5. Step-by-step derivation
    1. metadata-eval9.0%

      \[\leadsto \color{blue}{0.75} \]
  6. Applied egg-rr9.0%

    \[\leadsto \color{blue}{0.75} \]
  7. Add Preprocessing

Alternative 13: 7.7% 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. Add Preprocessing
  3. Taylor expanded in re around 0 66.6%

    \[\leadsto \color{blue}{0.5} \cdot \left(e^{-im} + e^{im}\right) \]
  4. Applied egg-rr7.6%

    \[\leadsto 0.5 \cdot \color{blue}{0.25} \]
  5. Step-by-step derivation
    1. metadata-eval7.6%

      \[\leadsto \color{blue}{0.125} \]
  6. Applied egg-rr7.6%

    \[\leadsto \color{blue}{0.125} \]
  7. Add Preprocessing

Reproduce

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