math.exp on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 10.0s
Alternatives: 11
Speedup: 1.0×

Specification

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

\\
e^{re} \cdot \cos im
\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 11 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} \\ e^{re} \cdot \cos im \end{array} \]
(FPCore (re im) :precision binary64 (* (exp re) (cos im)))
double code(double re, double im) {
	return exp(re) * cos(im);
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = exp(re) * cos(im)
end function
public static double code(double re, double im) {
	return Math.exp(re) * Math.cos(im);
}
def code(re, im):
	return math.exp(re) * math.cos(im)
function code(re, im)
	return Float64(exp(re) * cos(im))
end
function tmp = code(re, im)
	tmp = exp(re) * cos(im);
end
code[re_, im_] := N[(N[Exp[re], $MachinePrecision] * N[Cos[im], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
e^{re} \cdot \cos im
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

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

\\
e^{re} \cdot \cos im
\end{array}
Derivation
  1. Initial program 100.0%

    \[e^{re} \cdot \cos im \]
  2. Final simplification100.0%

    \[\leadsto e^{re} \cdot \cos im \]

Alternative 2: 93.2% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;e^{re} \leq 0:\\
\;\;\;\;e^{re}\\

\mathbf{elif}\;e^{re} \leq 2:\\
\;\;\;\;\cos im \cdot \left(re + 1\right)\\

\mathbf{else}:\\
\;\;\;\;e^{re}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (exp.f64 re) < 0.0 or 2 < (exp.f64 re)

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in im around 0 92.9%

      \[\leadsto \color{blue}{e^{re}} \]

    if 0.0 < (exp.f64 re) < 2

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 98.4%

      \[\leadsto \color{blue}{\cos im \cdot re + \cos im} \]
    3. Step-by-step derivation
      1. *-rgt-identity98.4%

        \[\leadsto \cos im \cdot re + \color{blue}{\cos im \cdot 1} \]
      2. distribute-lft-in98.4%

        \[\leadsto \color{blue}{\cos im \cdot \left(re + 1\right)} \]
    4. Simplified98.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{re} \leq 0:\\ \;\;\;\;e^{re}\\ \mathbf{elif}\;e^{re} \leq 2:\\ \;\;\;\;\cos im \cdot \left(re + 1\right)\\ \mathbf{else}:\\ \;\;\;\;e^{re}\\ \end{array} \]

Alternative 3: 92.6% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;e^{re} \leq 0:\\
\;\;\;\;e^{re}\\

\mathbf{elif}\;e^{re} \leq 2:\\
\;\;\;\;\cos im\\

\mathbf{else}:\\
\;\;\;\;e^{re}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (exp.f64 re) < 0.0 or 2 < (exp.f64 re)

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in im around 0 92.9%

      \[\leadsto \color{blue}{e^{re}} \]

    if 0.0 < (exp.f64 re) < 2

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 97.6%

      \[\leadsto \color{blue}{\cos im} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification95.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{re} \leq 0:\\ \;\;\;\;e^{re}\\ \mathbf{elif}\;e^{re} \leq 2:\\ \;\;\;\;\cos im\\ \mathbf{else}:\\ \;\;\;\;e^{re}\\ \end{array} \]

Alternative 4: 97.6% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -0.075 \lor \neg \left(re \leq 2.9\right) \land re \leq 6.5 \cdot 10^{+102}:\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;\cos im \cdot \left(\left(re + 1\right) + \left(re \cdot re\right) \cdot \left(0.5 + re \cdot 0.16666666666666666\right)\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= re -0.075) (and (not (<= re 2.9)) (<= re 6.5e+102)))
   (exp re)
   (*
    (cos im)
    (+ (+ re 1.0) (* (* re re) (+ 0.5 (* re 0.16666666666666666)))))))
double code(double re, double im) {
	double tmp;
	if ((re <= -0.075) || (!(re <= 2.9) && (re <= 6.5e+102))) {
		tmp = exp(re);
	} else {
		tmp = cos(im) * ((re + 1.0) + ((re * re) * (0.5 + (re * 0.16666666666666666))));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if ((re <= (-0.075d0)) .or. (.not. (re <= 2.9d0)) .and. (re <= 6.5d+102)) then
        tmp = exp(re)
    else
        tmp = cos(im) * ((re + 1.0d0) + ((re * re) * (0.5d0 + (re * 0.16666666666666666d0))))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if ((re <= -0.075) || (!(re <= 2.9) && (re <= 6.5e+102))) {
		tmp = Math.exp(re);
	} else {
		tmp = Math.cos(im) * ((re + 1.0) + ((re * re) * (0.5 + (re * 0.16666666666666666))));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (re <= -0.075) or (not (re <= 2.9) and (re <= 6.5e+102)):
		tmp = math.exp(re)
	else:
		tmp = math.cos(im) * ((re + 1.0) + ((re * re) * (0.5 + (re * 0.16666666666666666))))
	return tmp
function code(re, im)
	tmp = 0.0
	if ((re <= -0.075) || (!(re <= 2.9) && (re <= 6.5e+102)))
		tmp = exp(re);
	else
		tmp = Float64(cos(im) * Float64(Float64(re + 1.0) + Float64(Float64(re * re) * Float64(0.5 + Float64(re * 0.16666666666666666)))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if ((re <= -0.075) || (~((re <= 2.9)) && (re <= 6.5e+102)))
		tmp = exp(re);
	else
		tmp = cos(im) * ((re + 1.0) + ((re * re) * (0.5 + (re * 0.16666666666666666))));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Or[LessEqual[re, -0.075], And[N[Not[LessEqual[re, 2.9]], $MachinePrecision], LessEqual[re, 6.5e+102]]], N[Exp[re], $MachinePrecision], N[(N[Cos[im], $MachinePrecision] * N[(N[(re + 1.0), $MachinePrecision] + N[(N[(re * re), $MachinePrecision] * N[(0.5 + N[(re * 0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -0.075 \lor \neg \left(re \leq 2.9\right) \land re \leq 6.5 \cdot 10^{+102}:\\
\;\;\;\;e^{re}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -0.0749999999999999972 or 2.89999999999999991 < re < 6.5000000000000004e102

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in im around 0 95.6%

      \[\leadsto \color{blue}{e^{re}} \]

    if -0.0749999999999999972 < re < 2.89999999999999991 or 6.5000000000000004e102 < re

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 98.8%

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

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

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

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

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

        \[\leadsto \left(\left(0.16666666666666666 \cdot {re}^{3}\right) \cdot \cos im + \color{blue}{\left(0.5 \cdot {re}^{2}\right) \cdot \cos im}\right) + \left(\cos im \cdot re + \cos im\right) \]
      6. distribute-rgt-out98.8%

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

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

        \[\leadsto \left(0.16666666666666666 \cdot {re}^{3} + 0.5 \cdot {re}^{2}\right) \cdot \cos im + \left(\color{blue}{re \cdot \cos im} + \cos im\right) \]
      9. distribute-lft1-in98.8%

        \[\leadsto \left(0.16666666666666666 \cdot {re}^{3} + 0.5 \cdot {re}^{2}\right) \cdot \cos im + \color{blue}{\left(re + 1\right) \cdot \cos im} \]
      10. distribute-rgt-out98.8%

        \[\leadsto \color{blue}{\cos im \cdot \left(\left(0.16666666666666666 \cdot {re}^{3} + 0.5 \cdot {re}^{2}\right) + \left(re + 1\right)\right)} \]
      11. +-commutative98.8%

        \[\leadsto \cos im \cdot \color{blue}{\left(\left(re + 1\right) + \left(0.16666666666666666 \cdot {re}^{3} + 0.5 \cdot {re}^{2}\right)\right)} \]
      12. cube-mult98.8%

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

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

        \[\leadsto \cos im \cdot \left(\left(re + 1\right) + \left(\color{blue}{\left(0.16666666666666666 \cdot re\right) \cdot {re}^{2}} + 0.5 \cdot {re}^{2}\right)\right) \]
    4. Simplified98.3%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -0.075 \lor \neg \left(re \leq 2.9\right) \land re \leq 6.5 \cdot 10^{+102}:\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;\cos im \cdot \left(\left(re + 1\right) + \left(re \cdot re\right) \cdot \left(0.5 + re \cdot 0.16666666666666666\right)\right)\\ \end{array} \]

Alternative 5: 93.5% accurate, 1.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -0.075:\\
\;\;\;\;e^{re}\\

\mathbf{elif}\;re \leq 0.055:\\
\;\;\;\;\cos im \cdot \left(\left(re + 1\right) + re \cdot \left(re \cdot 0.5\right)\right)\\

\mathbf{else}:\\
\;\;\;\;e^{re}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -0.0749999999999999972 or 0.0550000000000000003 < re

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in im around 0 92.9%

      \[\leadsto \color{blue}{e^{re}} \]

    if -0.0749999999999999972 < re < 0.0550000000000000003

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 98.7%

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

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

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

        \[\leadsto \left(0.5 \cdot {re}^{2}\right) \cdot \cos im + \left(\color{blue}{re \cdot \cos im} + \cos im\right) \]
      4. distribute-lft1-in98.7%

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

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

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

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

        \[\leadsto \cos im \cdot \left(\left(re + 1\right) + \color{blue}{\left(re \cdot re\right)} \cdot 0.5\right) \]
      9. associate-*l*98.7%

        \[\leadsto \cos im \cdot \left(\left(re + 1\right) + \color{blue}{re \cdot \left(re \cdot 0.5\right)}\right) \]
    4. Simplified98.7%

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

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

Alternative 6: 65.9% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -550:\\
\;\;\;\;im \cdot \left(im \cdot -0.5\right)\\

\mathbf{elif}\;re \leq 1.35:\\
\;\;\;\;\cos im\\

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


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

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 2.3%

      \[\leadsto \color{blue}{\cos im \cdot re + \cos im} \]
    3. Step-by-step derivation
      1. *-rgt-identity2.3%

        \[\leadsto \cos im \cdot re + \color{blue}{\cos im \cdot 1} \]
      2. distribute-lft-in2.3%

        \[\leadsto \color{blue}{\cos im \cdot \left(re + 1\right)} \]
    4. Simplified2.3%

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

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

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

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

        \[\leadsto 1 + \left(re + -0.5 \cdot \left(\color{blue}{\left(im \cdot im\right)} \cdot \left(re + 1\right)\right)\right) \]
    7. Simplified1.9%

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

      \[\leadsto 1 + \color{blue}{-0.5 \cdot {im}^{2}} \]
    9. Step-by-step derivation
      1. unpow22.5%

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

      \[\leadsto 1 + \color{blue}{-0.5 \cdot \left(im \cdot im\right)} \]
    11. Taylor expanded in im around inf 23.5%

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

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

        \[\leadsto \color{blue}{\left(im \cdot im\right) \cdot -0.5} \]
      3. associate-*r*23.5%

        \[\leadsto \color{blue}{im \cdot \left(im \cdot -0.5\right)} \]
    13. Simplified23.5%

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

    if -550 < re < 1.3500000000000001

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 97.6%

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

    if 1.3500000000000001 < re

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 48.1%

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

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

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

        \[\leadsto \left(0.5 \cdot {re}^{2}\right) \cdot \cos im + \left(\color{blue}{re \cdot \cos im} + \cos im\right) \]
      4. distribute-lft1-in48.1%

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

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

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

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

        \[\leadsto \cos im \cdot \left(\left(re + 1\right) + \color{blue}{\left(re \cdot re\right)} \cdot 0.5\right) \]
      9. associate-*l*48.1%

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

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

      \[\leadsto \color{blue}{0.5 \cdot {re}^{2} + \left(1 + re\right)} \]
    6. Step-by-step derivation
      1. fma-def41.9%

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, {re}^{2}, 1 + re\right)} \]
      2. unpow241.9%

        \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{re \cdot re}, 1 + re\right) \]
      3. +-commutative41.9%

        \[\leadsto \mathsf{fma}\left(0.5, re \cdot re, \color{blue}{re + 1}\right) \]
    7. Simplified41.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, re \cdot re, re + 1\right)} \]
    8. Step-by-step derivation
      1. fma-udef41.9%

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

        \[\leadsto \color{blue}{\left(0.5 \cdot \left(re \cdot re\right) + re\right) + 1} \]
      3. *-commutative41.9%

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

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

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

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

Alternative 7: 44.3% accurate, 18.4× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -510:\\
\;\;\;\;im \cdot \left(im \cdot -0.5\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -510

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 2.3%

      \[\leadsto \color{blue}{\cos im \cdot re + \cos im} \]
    3. Step-by-step derivation
      1. *-rgt-identity2.3%

        \[\leadsto \cos im \cdot re + \color{blue}{\cos im \cdot 1} \]
      2. distribute-lft-in2.3%

        \[\leadsto \color{blue}{\cos im \cdot \left(re + 1\right)} \]
    4. Simplified2.3%

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

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

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

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

        \[\leadsto 1 + \left(re + -0.5 \cdot \left(\color{blue}{\left(im \cdot im\right)} \cdot \left(re + 1\right)\right)\right) \]
    7. Simplified1.9%

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

      \[\leadsto 1 + \color{blue}{-0.5 \cdot {im}^{2}} \]
    9. Step-by-step derivation
      1. unpow22.5%

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

      \[\leadsto 1 + \color{blue}{-0.5 \cdot \left(im \cdot im\right)} \]
    11. Taylor expanded in im around inf 23.5%

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

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

        \[\leadsto \color{blue}{\left(im \cdot im\right) \cdot -0.5} \]
      3. associate-*r*23.5%

        \[\leadsto \color{blue}{im \cdot \left(im \cdot -0.5\right)} \]
    13. Simplified23.5%

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

    if -510 < re

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 80.4%

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

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

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

        \[\leadsto \left(0.5 \cdot {re}^{2}\right) \cdot \cos im + \left(\color{blue}{re \cdot \cos im} + \cos im\right) \]
      4. distribute-lft1-in80.4%

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

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

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

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

        \[\leadsto \cos im \cdot \left(\left(re + 1\right) + \color{blue}{\left(re \cdot re\right)} \cdot 0.5\right) \]
      9. associate-*l*80.4%

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

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

      \[\leadsto \color{blue}{0.5 \cdot {re}^{2} + \left(1 + re\right)} \]
    6. Step-by-step derivation
      1. fma-def50.7%

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, {re}^{2}, 1 + re\right)} \]
      2. unpow250.7%

        \[\leadsto \mathsf{fma}\left(0.5, \color{blue}{re \cdot re}, 1 + re\right) \]
      3. +-commutative50.7%

        \[\leadsto \mathsf{fma}\left(0.5, re \cdot re, \color{blue}{re + 1}\right) \]
    7. Simplified50.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, re \cdot re, re + 1\right)} \]
    8. Step-by-step derivation
      1. fma-udef50.7%

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

        \[\leadsto \color{blue}{\left(0.5 \cdot \left(re \cdot re\right) + re\right) + 1} \]
      3. *-commutative50.7%

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

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

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

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

Alternative 8: 44.1% accurate, 22.2× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -145:\\
\;\;\;\;im \cdot \left(im \cdot -0.5\right)\\

\mathbf{elif}\;re \leq 2.4:\\
\;\;\;\;re + 1\\

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


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

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 2.3%

      \[\leadsto \color{blue}{\cos im \cdot re + \cos im} \]
    3. Step-by-step derivation
      1. *-rgt-identity2.3%

        \[\leadsto \cos im \cdot re + \color{blue}{\cos im \cdot 1} \]
      2. distribute-lft-in2.3%

        \[\leadsto \color{blue}{\cos im \cdot \left(re + 1\right)} \]
    4. Simplified2.3%

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

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

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

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

        \[\leadsto 1 + \left(re + -0.5 \cdot \left(\color{blue}{\left(im \cdot im\right)} \cdot \left(re + 1\right)\right)\right) \]
    7. Simplified1.9%

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

      \[\leadsto 1 + \color{blue}{-0.5 \cdot {im}^{2}} \]
    9. Step-by-step derivation
      1. unpow22.5%

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

      \[\leadsto 1 + \color{blue}{-0.5 \cdot \left(im \cdot im\right)} \]
    11. Taylor expanded in im around inf 23.5%

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

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

        \[\leadsto \color{blue}{\left(im \cdot im\right) \cdot -0.5} \]
      3. associate-*r*23.5%

        \[\leadsto \color{blue}{im \cdot \left(im \cdot -0.5\right)} \]
    13. Simplified23.5%

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

    if -145 < re < 2.39999999999999991

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 98.4%

      \[\leadsto \color{blue}{\cos im \cdot re + \cos im} \]
    3. Step-by-step derivation
      1. *-rgt-identity98.4%

        \[\leadsto \cos im \cdot re + \color{blue}{\cos im \cdot 1} \]
      2. distribute-lft-in98.4%

        \[\leadsto \color{blue}{\cos im \cdot \left(re + 1\right)} \]
    4. Simplified98.4%

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

      \[\leadsto \color{blue}{1 + re} \]
    6. Step-by-step derivation
      1. +-commutative55.7%

        \[\leadsto \color{blue}{re + 1} \]
    7. Simplified55.7%

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

    if 2.39999999999999991 < re

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 48.1%

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

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

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

        \[\leadsto \left(0.5 \cdot {re}^{2}\right) \cdot \cos im + \left(\color{blue}{re \cdot \cos im} + \cos im\right) \]
      4. distribute-lft1-in48.1%

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

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

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

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

        \[\leadsto \cos im \cdot \left(\left(re + 1\right) + \color{blue}{\left(re \cdot re\right)} \cdot 0.5\right) \]
      9. associate-*l*48.1%

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

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

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

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

        \[\leadsto \left(\cos im \cdot \color{blue}{\left(re \cdot re\right)}\right) \cdot 0.5 \]
      3. *-commutative48.1%

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

        \[\leadsto \color{blue}{\left(re \cdot re\right) \cdot \left(\cos im \cdot 0.5\right)} \]
    7. Simplified48.1%

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

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

        \[\leadsto 0.5 \cdot \left(\cos im \cdot \color{blue}{\left(re \cdot re\right)}\right) \]
      2. *-commutative48.1%

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

        \[\leadsto \color{blue}{\left(0.5 \cdot \left(re \cdot re\right)\right) \cdot \cos im} \]
      4. *-commutative48.1%

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

        \[\leadsto \color{blue}{\left(re \cdot \left(re \cdot 0.5\right)\right)} \cdot \cos im \]
      6. associate-*l*48.1%

        \[\leadsto \color{blue}{re \cdot \left(\left(re \cdot 0.5\right) \cdot \cos im\right)} \]
      7. *-commutative48.1%

        \[\leadsto re \cdot \left(\color{blue}{\left(0.5 \cdot re\right)} \cdot \cos im\right) \]
      8. associate-*l*48.1%

        \[\leadsto re \cdot \color{blue}{\left(0.5 \cdot \left(re \cdot \cos im\right)\right)} \]
    10. Simplified48.1%

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

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

        \[\leadsto \color{blue}{{re}^{2} \cdot 0.5} \]
      2. unpow241.8%

        \[\leadsto \color{blue}{\left(re \cdot re\right)} \cdot 0.5 \]
    13. Simplified41.8%

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

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

Alternative 9: 35.1% accurate, 28.7× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -72:\\
\;\;\;\;im \cdot \left(im \cdot -0.5\right)\\

\mathbf{else}:\\
\;\;\;\;re + 1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -72

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 2.3%

      \[\leadsto \color{blue}{\cos im \cdot re + \cos im} \]
    3. Step-by-step derivation
      1. *-rgt-identity2.3%

        \[\leadsto \cos im \cdot re + \color{blue}{\cos im \cdot 1} \]
      2. distribute-lft-in2.3%

        \[\leadsto \color{blue}{\cos im \cdot \left(re + 1\right)} \]
    4. Simplified2.3%

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

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

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

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

        \[\leadsto 1 + \left(re + -0.5 \cdot \left(\color{blue}{\left(im \cdot im\right)} \cdot \left(re + 1\right)\right)\right) \]
    7. Simplified1.9%

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

      \[\leadsto 1 + \color{blue}{-0.5 \cdot {im}^{2}} \]
    9. Step-by-step derivation
      1. unpow22.5%

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

      \[\leadsto 1 + \color{blue}{-0.5 \cdot \left(im \cdot im\right)} \]
    11. Taylor expanded in im around inf 23.5%

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

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

        \[\leadsto \color{blue}{\left(im \cdot im\right) \cdot -0.5} \]
      3. associate-*r*23.5%

        \[\leadsto \color{blue}{im \cdot \left(im \cdot -0.5\right)} \]
    13. Simplified23.5%

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

    if -72 < re

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Taylor expanded in re around 0 64.8%

      \[\leadsto \color{blue}{\cos im \cdot re + \cos im} \]
    3. Step-by-step derivation
      1. *-rgt-identity64.8%

        \[\leadsto \cos im \cdot re + \color{blue}{\cos im \cdot 1} \]
      2. distribute-lft-in64.8%

        \[\leadsto \color{blue}{\cos im \cdot \left(re + 1\right)} \]
    4. Simplified64.8%

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

      \[\leadsto \color{blue}{1 + re} \]
    6. Step-by-step derivation
      1. +-commutative37.4%

        \[\leadsto \color{blue}{re + 1} \]
    7. Simplified37.4%

      \[\leadsto \color{blue}{re + 1} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification33.5%

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

Alternative 10: 28.6% accurate, 67.7× speedup?

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

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

    \[e^{re} \cdot \cos im \]
  2. Taylor expanded in re around 0 47.5%

    \[\leadsto \color{blue}{\cos im \cdot re + \cos im} \]
  3. Step-by-step derivation
    1. *-rgt-identity47.5%

      \[\leadsto \cos im \cdot re + \color{blue}{\cos im \cdot 1} \]
    2. distribute-lft-in47.5%

      \[\leadsto \color{blue}{\cos im \cdot \left(re + 1\right)} \]
  4. Simplified47.5%

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

    \[\leadsto \color{blue}{1 + re} \]
  6. Step-by-step derivation
    1. +-commutative27.6%

      \[\leadsto \color{blue}{re + 1} \]
  7. Simplified27.6%

    \[\leadsto \color{blue}{re + 1} \]
  8. Final simplification27.6%

    \[\leadsto re + 1 \]

Alternative 11: 28.1% accurate, 203.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%

    \[e^{re} \cdot \cos im \]
  2. Taylor expanded in re around 0 47.5%

    \[\leadsto \color{blue}{\cos im \cdot re + \cos im} \]
  3. Step-by-step derivation
    1. *-rgt-identity47.5%

      \[\leadsto \cos im \cdot re + \color{blue}{\cos im \cdot 1} \]
    2. distribute-lft-in47.5%

      \[\leadsto \color{blue}{\cos im \cdot \left(re + 1\right)} \]
  4. Simplified47.5%

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

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

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

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

      \[\leadsto 1 + \left(re + -0.5 \cdot \left(\color{blue}{\left(im \cdot im\right)} \cdot \left(re + 1\right)\right)\right) \]
  7. Simplified27.9%

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

    \[\leadsto 1 + \color{blue}{-0.5 \cdot {im}^{2}} \]
  9. Step-by-step derivation
    1. unpow227.3%

      \[\leadsto 1 + -0.5 \cdot \color{blue}{\left(im \cdot im\right)} \]
  10. Simplified27.3%

    \[\leadsto 1 + \color{blue}{-0.5 \cdot \left(im \cdot im\right)} \]
  11. Taylor expanded in im around 0 27.4%

    \[\leadsto \color{blue}{1} \]
  12. Final simplification27.4%

    \[\leadsto 1 \]

Reproduce

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