math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 7.0s
Alternatives: 9
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 9 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. Final simplification100.0%

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

Alternative 2: 73.6% accurate, 1.5× speedup?

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

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

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

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


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

    1. Initial program 100.0%

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

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

    if 0.025000000000000001 < im < 1.1999999999999999e77

    1. Initial program 100.0%

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

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

    if 1.1999999999999999e77 < im

    1. Initial program 100.0%

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

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

      \[\leadsto \color{blue}{0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)} \]
    4. Step-by-step derivation
      1. associate-*r*100.0%

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

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

        \[\leadsto \color{blue}{{im}^{4} \cdot \left(0.041666666666666664 \cdot \cos re\right)} \]
    5. Simplified100.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 0.025:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.2 \cdot 10^{+77}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \mathbf{else}:\\ \;\;\;\;{im}^{4} \cdot \left(\cos re \cdot 0.041666666666666664\right)\\ \end{array} \]

Alternative 3: 86.4% accurate, 1.5× speedup?

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

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

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

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


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

    1. Initial program 100.0%

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

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

    if 0.094 < im < 1.1999999999999999e77

    1. Initial program 100.0%

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

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

    if 1.1999999999999999e77 < im

    1. Initial program 100.0%

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

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

      \[\leadsto \color{blue}{0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)} \]
    4. Step-by-step derivation
      1. associate-*r*100.0%

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

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

        \[\leadsto \color{blue}{{im}^{4} \cdot \left(0.041666666666666664 \cdot \cos re\right)} \]
    5. Simplified100.0%

      \[\leadsto \color{blue}{{im}^{4} \cdot \left(0.041666666666666664 \cdot \cos re\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification87.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 0.094:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + {im}^{2}\right)\\ \mathbf{elif}\;im \leq 1.2 \cdot 10^{+77}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \mathbf{else}:\\ \;\;\;\;{im}^{4} \cdot \left(\cos re \cdot 0.041666666666666664\right)\\ \end{array} \]

Alternative 4: 68.5% accurate, 1.5× speedup?

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

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

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


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

    1. Initial program 100.0%

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

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

    if 0.0369999999999999982 < im

    1. Initial program 100.0%

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

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

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

Alternative 5: 64.5% accurate, 2.8× speedup?

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

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

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

\mathbf{else}:\\
\;\;\;\;{im}^{4} \cdot 0.041666666666666664\\


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

    1. Initial program 100.0%

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

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

    if 12500 < im < 1.0999999999999999e79

    1. Initial program 100.0%

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

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

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

      \[\leadsto \color{blue}{1 + {re}^{2} \cdot -0.5} \]
    5. Step-by-step derivation
      1. log1p-expm1-u43.2%

        \[\leadsto 1 + \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left({re}^{2} \cdot -0.5\right)\right)} \]
    6. Applied egg-rr43.2%

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

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

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

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\left(re \cdot re\right)} \]
      4. associate-*r*35.4%

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

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

    if 1.0999999999999999e79 < im

    1. Initial program 100.0%

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

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

      \[\leadsto \color{blue}{0.041666666666666664 \cdot \left({im}^{4} \cdot \cos re\right)} \]
    4. Step-by-step derivation
      1. associate-*r*100.0%

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

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

        \[\leadsto \color{blue}{{im}^{4} \cdot \left(0.041666666666666664 \cdot \cos re\right)} \]
    5. Simplified100.0%

      \[\leadsto \color{blue}{{im}^{4} \cdot \left(0.041666666666666664 \cdot \cos re\right)} \]
    6. Taylor expanded in re around 0 68.5%

      \[\leadsto {im}^{4} \cdot \color{blue}{0.041666666666666664} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification62.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 12500:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.1 \cdot 10^{+79}:\\ \;\;\;\;1 + re \cdot \left(re \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;{im}^{4} \cdot 0.041666666666666664\\ \end{array} \]

Alternative 6: 53.3% accurate, 3.0× speedup?

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

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

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


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

    1. Initial program 100.0%

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

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

    if 10500 < im

    1. Initial program 100.0%

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

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

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

      \[\leadsto \color{blue}{1 + {re}^{2} \cdot -0.5} \]
    5. Step-by-step derivation
      1. log1p-expm1-u34.9%

        \[\leadsto 1 + \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left({re}^{2} \cdot -0.5\right)\right)} \]
    6. Applied egg-rr34.9%

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

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

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

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\left(re \cdot re\right)} \]
      4. associate-*r*16.4%

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

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

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

Alternative 7: 30.9% accurate, 34.0× speedup?

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

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

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


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

    1. Initial program 100.0%

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

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

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

    if 10500 < im

    1. Initial program 100.0%

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

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

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

      \[\leadsto \color{blue}{1 + {re}^{2} \cdot -0.5} \]
    5. Step-by-step derivation
      1. log1p-expm1-u34.9%

        \[\leadsto 1 + \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left({re}^{2} \cdot -0.5\right)\right)} \]
    6. Applied egg-rr34.9%

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

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

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

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\left(re \cdot re\right)} \]
      4. associate-*r*16.4%

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

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

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

Alternative 8: 8.0% accurate, 308.0× speedup?

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

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

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

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

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

    \[\leadsto 0.25 \]

Alternative 9: 27.9% 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. Taylor expanded in im around 0 46.8%

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

    \[\leadsto \color{blue}{1} \]
  4. Final simplification23.5%

    \[\leadsto 1 \]

Reproduce

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