math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 8.9s
Alternatives: 16
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 16 alternatives:

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

Initial Program: 100.0% accurate, 1.0× speedup?

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

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

Alternative 1: 100.0% accurate, 1.0× speedup?

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

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

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

Alternative 2: 76.1% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \cos re\\ \mathbf{if}\;e^{-im} + e^{im} \leq 4:\\ \;\;\;\;t\_0 \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}:\\ \;\;\;\;t\_0 \cdot \left(e^{im} + 3\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (cos re))))
   (if (<= (+ (exp (- im)) (exp im)) 4.0)
     (*
      t_0
      (+
       (exp im)
       (+ 1.0 (* im (+ (* im (+ 0.5 (* im -0.16666666666666666))) -1.0)))))
     (* t_0 (+ (exp im) 3.0)))))
double code(double re, double im) {
	double t_0 = 0.5 * cos(re);
	double tmp;
	if ((exp(-im) + exp(im)) <= 4.0) {
		tmp = t_0 * (exp(im) + (1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))));
	} else {
		tmp = t_0 * (exp(im) + 3.0);
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * cos(re)
    if ((exp(-im) + exp(im)) <= 4.0d0) then
        tmp = t_0 * (exp(im) + (1.0d0 + (im * ((im * (0.5d0 + (im * (-0.16666666666666666d0)))) + (-1.0d0)))))
    else
        tmp = t_0 * (exp(im) + 3.0d0)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.cos(re);
	double tmp;
	if ((Math.exp(-im) + Math.exp(im)) <= 4.0) {
		tmp = t_0 * (Math.exp(im) + (1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))));
	} else {
		tmp = t_0 * (Math.exp(im) + 3.0);
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.cos(re)
	tmp = 0
	if (math.exp(-im) + math.exp(im)) <= 4.0:
		tmp = t_0 * (math.exp(im) + (1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))))
	else:
		tmp = t_0 * (math.exp(im) + 3.0)
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * cos(re))
	tmp = 0.0
	if (Float64(exp(Float64(-im)) + exp(im)) <= 4.0)
		tmp = Float64(t_0 * Float64(exp(im) + Float64(1.0 + Float64(im * Float64(Float64(im * Float64(0.5 + Float64(im * -0.16666666666666666))) + -1.0)))));
	else
		tmp = Float64(t_0 * Float64(exp(im) + 3.0));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * cos(re);
	tmp = 0.0;
	if ((exp(-im) + exp(im)) <= 4.0)
		tmp = t_0 * (exp(im) + (1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))));
	else
		tmp = t_0 * (exp(im) + 3.0);
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision], 4.0], N[(t$95$0 * N[(N[Exp[im], $MachinePrecision] + N[(1.0 + N[(im * N[(N[(im * N[(0.5 + N[(im * -0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * N[(N[Exp[im], $MachinePrecision] + 3.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \cos re\\
\mathbf{if}\;e^{-im} + e^{im} \leq 4:\\
\;\;\;\;t\_0 \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}:\\
\;\;\;\;t\_0 \cdot \left(e^{im} + 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.7%

      \[\leadsto \left(0.5 \cdot \cos re\right) \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 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.6%

      \[\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 simplification73.7%

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

Alternative 3: 76.1% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \cos re\\ \mathbf{if}\;e^{-im} + e^{im} \leq 4:\\ \;\;\;\;t\_0 \cdot \left(\left(1 + im \cdot \left(im \cdot \left(0.5 + im \cdot -0.16666666666666666\right) + -1\right)\right) + \left(1 + im \cdot \left(1 + im \cdot \left(0.5 + im \cdot 0.16666666666666666\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0 \cdot \left(e^{im} + 3\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (cos re))))
   (if (<= (+ (exp (- im)) (exp im)) 4.0)
     (*
      t_0
      (+
       (+ 1.0 (* im (+ (* im (+ 0.5 (* im -0.16666666666666666))) -1.0)))
       (+ 1.0 (* im (+ 1.0 (* im (+ 0.5 (* im 0.16666666666666666))))))))
     (* t_0 (+ (exp im) 3.0)))))
double code(double re, double im) {
	double t_0 = 0.5 * cos(re);
	double tmp;
	if ((exp(-im) + exp(im)) <= 4.0) {
		tmp = t_0 * ((1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))) + (1.0 + (im * (1.0 + (im * (0.5 + (im * 0.16666666666666666)))))));
	} else {
		tmp = t_0 * (exp(im) + 3.0);
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * cos(re)
    if ((exp(-im) + exp(im)) <= 4.0d0) then
        tmp = t_0 * ((1.0d0 + (im * ((im * (0.5d0 + (im * (-0.16666666666666666d0)))) + (-1.0d0)))) + (1.0d0 + (im * (1.0d0 + (im * (0.5d0 + (im * 0.16666666666666666d0)))))))
    else
        tmp = t_0 * (exp(im) + 3.0d0)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.cos(re);
	double tmp;
	if ((Math.exp(-im) + Math.exp(im)) <= 4.0) {
		tmp = t_0 * ((1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))) + (1.0 + (im * (1.0 + (im * (0.5 + (im * 0.16666666666666666)))))));
	} else {
		tmp = t_0 * (Math.exp(im) + 3.0);
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.cos(re)
	tmp = 0
	if (math.exp(-im) + math.exp(im)) <= 4.0:
		tmp = t_0 * ((1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))) + (1.0 + (im * (1.0 + (im * (0.5 + (im * 0.16666666666666666)))))))
	else:
		tmp = t_0 * (math.exp(im) + 3.0)
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * cos(re))
	tmp = 0.0
	if (Float64(exp(Float64(-im)) + exp(im)) <= 4.0)
		tmp = Float64(t_0 * Float64(Float64(1.0 + Float64(im * Float64(Float64(im * Float64(0.5 + Float64(im * -0.16666666666666666))) + -1.0))) + Float64(1.0 + Float64(im * Float64(1.0 + Float64(im * Float64(0.5 + Float64(im * 0.16666666666666666))))))));
	else
		tmp = Float64(t_0 * Float64(exp(im) + 3.0));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * cos(re);
	tmp = 0.0;
	if ((exp(-im) + exp(im)) <= 4.0)
		tmp = t_0 * ((1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))) + (1.0 + (im * (1.0 + (im * (0.5 + (im * 0.16666666666666666)))))));
	else
		tmp = t_0 * (exp(im) + 3.0);
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision], 4.0], N[(t$95$0 * N[(N[(1.0 + N[(im * N[(N[(im * N[(0.5 + N[(im * -0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(1.0 + N[(im * N[(1.0 + N[(im * N[(0.5 + N[(im * 0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * N[(N[Exp[im], $MachinePrecision] + 3.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{else}:\\
\;\;\;\;t\_0 \cdot \left(e^{im} + 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.7%

      \[\leadsto \left(0.5 \cdot \cos re\right) \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) \]
    4. Taylor expanded in im around 0 99.7%

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\left(1 + im \cdot \left(im \cdot \left(0.5 + -0.16666666666666666 \cdot im\right) - 1\right)\right) + \left(1 + im \cdot \left(1 + im \cdot \left(0.5 + \color{blue}{im \cdot 0.16666666666666666}\right)\right)\right)\right) \]
    6. Simplified99.7%

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

      \[\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 simplification73.7%

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

Alternative 4: 73.2% accurate, 2.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := im \cdot \left(1 + im \cdot \left(0.5 + im \cdot 0.16666666666666666\right)\right)\\ t_1 := 0.5 \cdot \cos re\\ \mathbf{if}\;im \leq 4.6:\\ \;\;\;\;t\_1 \cdot \left(\left(1 + im \cdot \left(im \cdot \left(0.5 + im \cdot -0.16666666666666666\right) + -1\right)\right) + \left(1 + t\_0\right)\right)\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{+103}:\\ \;\;\;\;0.5 \cdot e^{im} + 1.5\\ \mathbf{else}:\\ \;\;\;\;t\_1 \cdot \left(4 + t\_0\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* im (+ 1.0 (* im (+ 0.5 (* im 0.16666666666666666))))))
        (t_1 (* 0.5 (cos re))))
   (if (<= im 4.6)
     (*
      t_1
      (+
       (+ 1.0 (* im (+ (* im (+ 0.5 (* im -0.16666666666666666))) -1.0)))
       (+ 1.0 t_0)))
     (if (<= im 1.05e+103) (+ (* 0.5 (exp im)) 1.5) (* t_1 (+ 4.0 t_0))))))
double code(double re, double im) {
	double t_0 = im * (1.0 + (im * (0.5 + (im * 0.16666666666666666))));
	double t_1 = 0.5 * cos(re);
	double tmp;
	if (im <= 4.6) {
		tmp = t_1 * ((1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))) + (1.0 + t_0));
	} else if (im <= 1.05e+103) {
		tmp = (0.5 * exp(im)) + 1.5;
	} else {
		tmp = t_1 * (4.0 + t_0);
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = im * (1.0d0 + (im * (0.5d0 + (im * 0.16666666666666666d0))))
    t_1 = 0.5d0 * cos(re)
    if (im <= 4.6d0) then
        tmp = t_1 * ((1.0d0 + (im * ((im * (0.5d0 + (im * (-0.16666666666666666d0)))) + (-1.0d0)))) + (1.0d0 + t_0))
    else if (im <= 1.05d+103) then
        tmp = (0.5d0 * exp(im)) + 1.5d0
    else
        tmp = t_1 * (4.0d0 + t_0)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = im * (1.0 + (im * (0.5 + (im * 0.16666666666666666))));
	double t_1 = 0.5 * Math.cos(re);
	double tmp;
	if (im <= 4.6) {
		tmp = t_1 * ((1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))) + (1.0 + t_0));
	} else if (im <= 1.05e+103) {
		tmp = (0.5 * Math.exp(im)) + 1.5;
	} else {
		tmp = t_1 * (4.0 + t_0);
	}
	return tmp;
}
def code(re, im):
	t_0 = im * (1.0 + (im * (0.5 + (im * 0.16666666666666666))))
	t_1 = 0.5 * math.cos(re)
	tmp = 0
	if im <= 4.6:
		tmp = t_1 * ((1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))) + (1.0 + t_0))
	elif im <= 1.05e+103:
		tmp = (0.5 * math.exp(im)) + 1.5
	else:
		tmp = t_1 * (4.0 + t_0)
	return tmp
function code(re, im)
	t_0 = Float64(im * Float64(1.0 + Float64(im * Float64(0.5 + Float64(im * 0.16666666666666666)))))
	t_1 = Float64(0.5 * cos(re))
	tmp = 0.0
	if (im <= 4.6)
		tmp = Float64(t_1 * Float64(Float64(1.0 + Float64(im * Float64(Float64(im * Float64(0.5 + Float64(im * -0.16666666666666666))) + -1.0))) + Float64(1.0 + t_0)));
	elseif (im <= 1.05e+103)
		tmp = Float64(Float64(0.5 * exp(im)) + 1.5);
	else
		tmp = Float64(t_1 * Float64(4.0 + t_0));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = im * (1.0 + (im * (0.5 + (im * 0.16666666666666666))));
	t_1 = 0.5 * cos(re);
	tmp = 0.0;
	if (im <= 4.6)
		tmp = t_1 * ((1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))) + (1.0 + t_0));
	elseif (im <= 1.05e+103)
		tmp = (0.5 * exp(im)) + 1.5;
	else
		tmp = t_1 * (4.0 + t_0);
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(im * N[(1.0 + N[(im * N[(0.5 + N[(im * 0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[im, 4.6], N[(t$95$1 * N[(N[(1.0 + N[(im * N[(N[(im * N[(0.5 + N[(im * -0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(1.0 + t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.05e+103], N[(N[(0.5 * N[Exp[im], $MachinePrecision]), $MachinePrecision] + 1.5), $MachinePrecision], N[(t$95$1 * N[(4.0 + t$95$0), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := im \cdot \left(1 + im \cdot \left(0.5 + im \cdot 0.16666666666666666\right)\right)\\
t_1 := 0.5 \cdot \cos re\\
\mathbf{if}\;im \leq 4.6:\\
\;\;\;\;t\_1 \cdot \left(\left(1 + im \cdot \left(im \cdot \left(0.5 + im \cdot -0.16666666666666666\right) + -1\right)\right) + \left(1 + t\_0\right)\right)\\

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

\mathbf{else}:\\
\;\;\;\;t\_1 \cdot \left(4 + t\_0\right)\\


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

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

      \[\leadsto \left(0.5 \cdot \cos re\right) \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) \]
    4. Taylor expanded in im around 0 63.0%

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

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

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

    if 4.5999999999999996 < im < 1.0500000000000001e103

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

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

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

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

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

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

    if 1.0500000000000001e103 < 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 simplification69.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 4.6:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(\left(1 + im \cdot \left(im \cdot \left(0.5 + im \cdot -0.16666666666666666\right) + -1\right)\right) + \left(1 + im \cdot \left(1 + im \cdot \left(0.5 + im \cdot 0.16666666666666666\right)\right)\right)\right)\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{+103}:\\ \;\;\;\;0.5 \cdot e^{im} + 1.5\\ \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: 90.4% accurate, 2.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \cos re\\ \mathbf{if}\;im \leq 3.1:\\ \;\;\;\;t\_0 \cdot \left(\left(1 + im \cdot \left(im \cdot \left(0.5 + im \cdot -0.16666666666666666\right) + -1\right)\right) + \left(1 + im \cdot \left(1 + 0.5 \cdot im\right)\right)\right)\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{+103}:\\ \;\;\;\;0.5 \cdot e^{im} + 1.5\\ \mathbf{else}:\\ \;\;\;\;t\_0 \cdot \left(4 + im \cdot \left(1 + im \cdot \left(0.5 + im \cdot 0.16666666666666666\right)\right)\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (cos re))))
   (if (<= im 3.1)
     (*
      t_0
      (+
       (+ 1.0 (* im (+ (* im (+ 0.5 (* im -0.16666666666666666))) -1.0)))
       (+ 1.0 (* im (+ 1.0 (* 0.5 im))))))
     (if (<= im 1.05e+103)
       (+ (* 0.5 (exp im)) 1.5)
       (*
        t_0
        (+ 4.0 (* im (+ 1.0 (* im (+ 0.5 (* im 0.16666666666666666)))))))))))
double code(double re, double im) {
	double t_0 = 0.5 * cos(re);
	double tmp;
	if (im <= 3.1) {
		tmp = t_0 * ((1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))) + (1.0 + (im * (1.0 + (0.5 * im)))));
	} else if (im <= 1.05e+103) {
		tmp = (0.5 * exp(im)) + 1.5;
	} else {
		tmp = t_0 * (4.0 + (im * (1.0 + (im * (0.5 + (im * 0.16666666666666666))))));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * cos(re)
    if (im <= 3.1d0) then
        tmp = t_0 * ((1.0d0 + (im * ((im * (0.5d0 + (im * (-0.16666666666666666d0)))) + (-1.0d0)))) + (1.0d0 + (im * (1.0d0 + (0.5d0 * im)))))
    else if (im <= 1.05d+103) then
        tmp = (0.5d0 * exp(im)) + 1.5d0
    else
        tmp = t_0 * (4.0d0 + (im * (1.0d0 + (im * (0.5d0 + (im * 0.16666666666666666d0))))))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.cos(re);
	double tmp;
	if (im <= 3.1) {
		tmp = t_0 * ((1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))) + (1.0 + (im * (1.0 + (0.5 * im)))));
	} else if (im <= 1.05e+103) {
		tmp = (0.5 * Math.exp(im)) + 1.5;
	} else {
		tmp = t_0 * (4.0 + (im * (1.0 + (im * (0.5 + (im * 0.16666666666666666))))));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.cos(re)
	tmp = 0
	if im <= 3.1:
		tmp = t_0 * ((1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))) + (1.0 + (im * (1.0 + (0.5 * im)))))
	elif im <= 1.05e+103:
		tmp = (0.5 * math.exp(im)) + 1.5
	else:
		tmp = t_0 * (4.0 + (im * (1.0 + (im * (0.5 + (im * 0.16666666666666666))))))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * cos(re))
	tmp = 0.0
	if (im <= 3.1)
		tmp = Float64(t_0 * Float64(Float64(1.0 + Float64(im * Float64(Float64(im * Float64(0.5 + Float64(im * -0.16666666666666666))) + -1.0))) + Float64(1.0 + Float64(im * Float64(1.0 + Float64(0.5 * im))))));
	elseif (im <= 1.05e+103)
		tmp = Float64(Float64(0.5 * exp(im)) + 1.5);
	else
		tmp = Float64(t_0 * Float64(4.0 + Float64(im * Float64(1.0 + Float64(im * Float64(0.5 + Float64(im * 0.16666666666666666)))))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * cos(re);
	tmp = 0.0;
	if (im <= 3.1)
		tmp = t_0 * ((1.0 + (im * ((im * (0.5 + (im * -0.16666666666666666))) + -1.0))) + (1.0 + (im * (1.0 + (0.5 * im)))));
	elseif (im <= 1.05e+103)
		tmp = (0.5 * exp(im)) + 1.5;
	else
		tmp = t_0 * (4.0 + (im * (1.0 + (im * (0.5 + (im * 0.16666666666666666))))));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[im, 3.1], N[(t$95$0 * N[(N[(1.0 + N[(im * N[(N[(im * N[(0.5 + N[(im * -0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(1.0 + N[(im * N[(1.0 + N[(0.5 * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.05e+103], N[(N[(0.5 * N[Exp[im], $MachinePrecision]), $MachinePrecision] + 1.5), $MachinePrecision], N[(t$95$0 * N[(4.0 + N[(im * N[(1.0 + N[(im * N[(0.5 + N[(im * 0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \cos re\\
\mathbf{if}\;im \leq 3.1:\\
\;\;\;\;t\_0 \cdot \left(\left(1 + im \cdot \left(im \cdot \left(0.5 + im \cdot -0.16666666666666666\right) + -1\right)\right) + \left(1 + im \cdot \left(1 + 0.5 \cdot im\right)\right)\right)\\

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

\mathbf{else}:\\
\;\;\;\;t\_0 \cdot \left(4 + im \cdot \left(1 + im \cdot \left(0.5 + im \cdot 0.16666666666666666\right)\right)\right)\\


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

      \[\leadsto \left(0.5 \cdot \cos re\right) \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) \]
    4. Taylor expanded in im around 0 91.6%

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

    if 3.10000000000000009 < im < 1.0500000000000001e103

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

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

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

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

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

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

    if 1.0500000000000001e103 < 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 simplification90.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 3.1:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(\left(1 + im \cdot \left(im \cdot \left(0.5 + im \cdot -0.16666666666666666\right) + -1\right)\right) + \left(1 + im \cdot \left(1 + 0.5 \cdot im\right)\right)\right)\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{+103}:\\ \;\;\;\;0.5 \cdot e^{im} + 1.5\\ \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 6: 86.1% accurate, 2.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \cos re\\ \mathbf{if}\;im \leq 4:\\ \;\;\;\;t\_0 \cdot \left(\left(1 + im \cdot \left(0.5 \cdot im + -1\right)\right) + \left(im + 1\right)\right)\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{+103}:\\ \;\;\;\;0.5 \cdot e^{im} + 1.5\\ \mathbf{else}:\\ \;\;\;\;t\_0 \cdot \left(4 + im \cdot \left(1 + im \cdot \left(0.5 + im \cdot 0.16666666666666666\right)\right)\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (cos re))))
   (if (<= im 4.0)
     (* t_0 (+ (+ 1.0 (* im (+ (* 0.5 im) -1.0))) (+ im 1.0)))
     (if (<= im 1.05e+103)
       (+ (* 0.5 (exp im)) 1.5)
       (*
        t_0
        (+ 4.0 (* im (+ 1.0 (* im (+ 0.5 (* im 0.16666666666666666)))))))))))
double code(double re, double im) {
	double t_0 = 0.5 * cos(re);
	double tmp;
	if (im <= 4.0) {
		tmp = t_0 * ((1.0 + (im * ((0.5 * im) + -1.0))) + (im + 1.0));
	} else if (im <= 1.05e+103) {
		tmp = (0.5 * exp(im)) + 1.5;
	} else {
		tmp = t_0 * (4.0 + (im * (1.0 + (im * (0.5 + (im * 0.16666666666666666))))));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * cos(re)
    if (im <= 4.0d0) then
        tmp = t_0 * ((1.0d0 + (im * ((0.5d0 * im) + (-1.0d0)))) + (im + 1.0d0))
    else if (im <= 1.05d+103) then
        tmp = (0.5d0 * exp(im)) + 1.5d0
    else
        tmp = t_0 * (4.0d0 + (im * (1.0d0 + (im * (0.5d0 + (im * 0.16666666666666666d0))))))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.cos(re);
	double tmp;
	if (im <= 4.0) {
		tmp = t_0 * ((1.0 + (im * ((0.5 * im) + -1.0))) + (im + 1.0));
	} else if (im <= 1.05e+103) {
		tmp = (0.5 * Math.exp(im)) + 1.5;
	} else {
		tmp = t_0 * (4.0 + (im * (1.0 + (im * (0.5 + (im * 0.16666666666666666))))));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.cos(re)
	tmp = 0
	if im <= 4.0:
		tmp = t_0 * ((1.0 + (im * ((0.5 * im) + -1.0))) + (im + 1.0))
	elif im <= 1.05e+103:
		tmp = (0.5 * math.exp(im)) + 1.5
	else:
		tmp = t_0 * (4.0 + (im * (1.0 + (im * (0.5 + (im * 0.16666666666666666))))))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * cos(re))
	tmp = 0.0
	if (im <= 4.0)
		tmp = Float64(t_0 * Float64(Float64(1.0 + Float64(im * Float64(Float64(0.5 * im) + -1.0))) + Float64(im + 1.0)));
	elseif (im <= 1.05e+103)
		tmp = Float64(Float64(0.5 * exp(im)) + 1.5);
	else
		tmp = Float64(t_0 * Float64(4.0 + Float64(im * Float64(1.0 + Float64(im * Float64(0.5 + Float64(im * 0.16666666666666666)))))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * cos(re);
	tmp = 0.0;
	if (im <= 4.0)
		tmp = t_0 * ((1.0 + (im * ((0.5 * im) + -1.0))) + (im + 1.0));
	elseif (im <= 1.05e+103)
		tmp = (0.5 * exp(im)) + 1.5;
	else
		tmp = t_0 * (4.0 + (im * (1.0 + (im * (0.5 + (im * 0.16666666666666666))))));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[im, 4.0], N[(t$95$0 * N[(N[(1.0 + N[(im * N[(N[(0.5 * im), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(im + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.05e+103], N[(N[(0.5 * N[Exp[im], $MachinePrecision]), $MachinePrecision] + 1.5), $MachinePrecision], N[(t$95$0 * N[(4.0 + N[(im * N[(1.0 + N[(im * N[(0.5 + N[(im * 0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \cos re\\
\mathbf{if}\;im \leq 4:\\
\;\;\;\;t\_0 \cdot \left(\left(1 + im \cdot \left(0.5 \cdot im + -1\right)\right) + \left(im + 1\right)\right)\\

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

\mathbf{else}:\\
\;\;\;\;t\_0 \cdot \left(4 + im \cdot \left(1 + im \cdot \left(0.5 + im \cdot 0.16666666666666666\right)\right)\right)\\


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

      \[\leadsto \left(0.5 \cdot \cos re\right) \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) \]
    4. Taylor expanded in im around 0 91.3%

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

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

    if 4 < im < 1.0500000000000001e103

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

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

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

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

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

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

    if 1.0500000000000001e103 < 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 simplification85.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 4:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(\left(1 + im \cdot \left(0.5 \cdot im + -1\right)\right) + \left(im + 1\right)\right)\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{+103}:\\ \;\;\;\;0.5 \cdot e^{im} + 1.5\\ \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 7: 73.6% accurate, 2.4× speedup?

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

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

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

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

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

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

    if 2.89999999999999991 < im < 1.0500000000000001e103

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

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

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

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

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

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

    if 1.0500000000000001e103 < 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. Add Preprocessing

Alternative 8: 72.6% accurate, 2.5× speedup?

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

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

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

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


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

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

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

    if 3.2000000000000002 < im < 1.84999999999999997e154

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

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

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

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

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

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

    if 1.84999999999999997e154 < 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 + 0.5 \cdot im\right)\right)} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 9: 69.4% accurate, 2.8× speedup?

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

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

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


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

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

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

    if 3 < 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 73.9%

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

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

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

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

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

Alternative 10: 64.1% accurate, 2.9× speedup?

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

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

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

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


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

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

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

    if 1.5e5 < im < 7.20000000000000033e103

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

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

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

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

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

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

    if 7.20000000000000033e103 < 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 78.0%

      \[\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. +-commutative78.0%

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

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

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

Alternative 11: 53.7% accurate, 13.4× speedup?

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

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

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

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


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

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

      \[\leadsto \left(0.5 \cdot \cos re\right) \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) \]
    4. Taylor expanded in im around 0 91.6%

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

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

    if 0.0184999999999999991 < im < 7.20000000000000033e103

    1. Initial program 99.9%

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

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

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

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

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

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

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

    if 7.20000000000000033e103 < 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 78.0%

      \[\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. +-commutative78.0%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 0.0185:\\ \;\;\;\;0.5 \cdot \left(2 + \left(im + im \cdot \left(im \cdot \left(0.5 + im \cdot -0.16666666666666666\right) + -1\right)\right)\right)\\ \mathbf{elif}\;im \leq 7.2 \cdot 10^{+103}:\\ \;\;\;\;1 + \left(re \cdot re\right) \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(0.5 + im \cdot \left(0.25 + im \cdot 0.08333333333333333\right)\right) + 2\\ \end{array} \]
  5. Add Preprocessing

Alternative 12: 41.3% accurate, 13.4× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 1.14 \cdot 10^{-29}:\\
\;\;\;\;1\\

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

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


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

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

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

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

    if 1.13999999999999995e-29 < im < 7.20000000000000033e103

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

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

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

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

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

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

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

    if 7.20000000000000033e103 < 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 78.0%

      \[\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. +-commutative78.0%

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

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

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

Alternative 13: 38.3% accurate, 16.2× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 1.14 \cdot 10^{-29}:\\
\;\;\;\;1\\

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

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


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

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

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

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

    if 1.13999999999999995e-29 < im < 1.13999999999999997e105

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

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

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

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

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

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

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

    if 1.13999999999999997e105 < 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 55.4%

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

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

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

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

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

Alternative 14: 31.4% accurate, 25.6× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 1.14 \cdot 10^{-29}:\\
\;\;\;\;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 < 1.13999999999999995e-29

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

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

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

    if 1.13999999999999995e-29 < 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 9.5%

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

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

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

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

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

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

Alternative 15: 28.4% accurate, 308.0× speedup?

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

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

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

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

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

Alternative 16: 9.1% accurate, 308.0× speedup?

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

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

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

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

      \[\leadsto \color{blue}{0.75} \]
  6. Applied egg-rr8.9%

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

Reproduce

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