math.exp on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 17.5s
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. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 70.1% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{re} \leq 1\\ \mathbf{if}\;t\_0 \lor \neg t\_0:\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;\cos im\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (<= (exp re) 1.0))) (if (or t_0 (not t_0)) (exp re) (cos im))))
double code(double re, double im) {
	int t_0 = exp(re) <= 1.0;
	double tmp;
	if (t_0 || !t_0) {
		tmp = exp(re);
	} else {
		tmp = cos(im);
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    logical :: t_0
    real(8) :: tmp
    t_0 = exp(re) <= 1.0d0
    if (t_0 .or. (.not. t_0)) then
        tmp = exp(re)
    else
        tmp = cos(im)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	boolean t_0 = Math.exp(re) <= 1.0;
	double tmp;
	if (t_0 || !t_0) {
		tmp = Math.exp(re);
	} else {
		tmp = Math.cos(im);
	}
	return tmp;
}
def code(re, im):
	t_0 = math.exp(re) <= 1.0
	tmp = 0
	if t_0 or not t_0:
		tmp = math.exp(re)
	else:
		tmp = math.cos(im)
	return tmp
function code(re, im)
	t_0 = exp(re) <= 1.0
	tmp = 0.0
	if (t_0 || !t_0)
		tmp = exp(re);
	else
		tmp = cos(im);
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = exp(re) <= 1.0;
	tmp = 0.0;
	if (t_0 || ~(t_0))
		tmp = exp(re);
	else
		tmp = cos(im);
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = LessEqual[N[Exp[re], $MachinePrecision], 1.0]}, If[Or[t$95$0, N[Not[t$95$0], $MachinePrecision]], N[Exp[re], $MachinePrecision], N[Cos[im], $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{re} \leq 1\\
\mathbf{if}\;t\_0 \lor \neg t\_0:\\
\;\;\;\;e^{re}\\

\mathbf{else}:\\
\;\;\;\;\cos im\\


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

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in im around 0 73.7%

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

    if 1 < (exp.f64 re) < 1

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0 50.8%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{re} \leq 1 \lor \neg \left(e^{re} \leq 1\right):\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;\cos im\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 97.8% accurate, 1.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -0.057 \lor \neg \left(re \leq 0.04\right) \land re \leq 1.05 \cdot 10^{+103}:\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;\cos im \cdot \left(1 + re \cdot \left(1 + re \cdot \left(0.5 + re \cdot 0.16666666666666666\right)\right)\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= re -0.057) (and (not (<= re 0.04)) (<= re 1.05e+103)))
   (exp re)
   (*
    (cos im)
    (+ 1.0 (* re (+ 1.0 (* re (+ 0.5 (* re 0.16666666666666666)))))))))
double code(double re, double im) {
	double tmp;
	if ((re <= -0.057) || (!(re <= 0.04) && (re <= 1.05e+103))) {
		tmp = exp(re);
	} else {
		tmp = cos(im) * (1.0 + (re * (1.0 + (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.057d0)) .or. (.not. (re <= 0.04d0)) .and. (re <= 1.05d+103)) then
        tmp = exp(re)
    else
        tmp = cos(im) * (1.0d0 + (re * (1.0d0 + (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.057) || (!(re <= 0.04) && (re <= 1.05e+103))) {
		tmp = Math.exp(re);
	} else {
		tmp = Math.cos(im) * (1.0 + (re * (1.0 + (re * (0.5 + (re * 0.16666666666666666))))));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (re <= -0.057) or (not (re <= 0.04) and (re <= 1.05e+103)):
		tmp = math.exp(re)
	else:
		tmp = math.cos(im) * (1.0 + (re * (1.0 + (re * (0.5 + (re * 0.16666666666666666))))))
	return tmp
function code(re, im)
	tmp = 0.0
	if ((re <= -0.057) || (!(re <= 0.04) && (re <= 1.05e+103)))
		tmp = exp(re);
	else
		tmp = Float64(cos(im) * Float64(1.0 + Float64(re * Float64(1.0 + Float64(re * Float64(0.5 + Float64(re * 0.16666666666666666)))))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if ((re <= -0.057) || (~((re <= 0.04)) && (re <= 1.05e+103)))
		tmp = exp(re);
	else
		tmp = cos(im) * (1.0 + (re * (1.0 + (re * (0.5 + (re * 0.16666666666666666))))));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Or[LessEqual[re, -0.057], And[N[Not[LessEqual[re, 0.04]], $MachinePrecision], LessEqual[re, 1.05e+103]]], N[Exp[re], $MachinePrecision], N[(N[Cos[im], $MachinePrecision] * N[(1.0 + N[(re * N[(1.0 + N[(re * N[(0.5 + N[(re * 0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -0.057 \lor \neg \left(re \leq 0.04\right) \land re \leq 1.05 \cdot 10^{+103}:\\
\;\;\;\;e^{re}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -0.0570000000000000021 or 0.0400000000000000008 < re < 1.0500000000000001e103

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in im around 0 93.4%

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

    if -0.0570000000000000021 < re < 0.0400000000000000008 or 1.0500000000000001e103 < re

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0 99.8%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -0.057 \lor \neg \left(re \leq 0.04\right) \land re \leq 1.05 \cdot 10^{+103}:\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;\cos im \cdot \left(1 + re \cdot \left(1 + re \cdot \left(0.5 + re \cdot 0.16666666666666666\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 96.6% accurate, 1.6× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -0.055 \lor \neg \left(re \leq 0.0062\right) \land re \leq 1.9 \cdot 10^{+154}:\\
\;\;\;\;e^{re}\\

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


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

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in im around 0 91.0%

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

    if -0.0550000000000000003 < re < 0.00619999999999999978 or 1.8999999999999999e154 < re

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0 99.7%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -0.055 \lor \neg \left(re \leq 0.0062\right) \land re \leq 1.9 \cdot 10^{+154}:\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;\cos im \cdot \left(1 + re \cdot \left(1 + re \cdot 0.5\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 92.9% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -0.0132 \lor \neg \left(re \leq 0.0138\right):\\ \;\;\;\;e^{re}\\ \mathbf{else}:\\ \;\;\;\;\cos im \cdot \left(re + 1\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= re -0.0132) (not (<= re 0.0138)))
   (exp re)
   (* (cos im) (+ re 1.0))))
double code(double re, double im) {
	double tmp;
	if ((re <= -0.0132) || !(re <= 0.0138)) {
		tmp = exp(re);
	} else {
		tmp = cos(im) * (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 <= (-0.0132d0)) .or. (.not. (re <= 0.0138d0))) then
        tmp = exp(re)
    else
        tmp = cos(im) * (re + 1.0d0)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if ((re <= -0.0132) || !(re <= 0.0138)) {
		tmp = Math.exp(re);
	} else {
		tmp = Math.cos(im) * (re + 1.0);
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (re <= -0.0132) or not (re <= 0.0138):
		tmp = math.exp(re)
	else:
		tmp = math.cos(im) * (re + 1.0)
	return tmp
function code(re, im)
	tmp = 0.0
	if ((re <= -0.0132) || !(re <= 0.0138))
		tmp = exp(re);
	else
		tmp = Float64(cos(im) * Float64(re + 1.0));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if ((re <= -0.0132) || ~((re <= 0.0138)))
		tmp = exp(re);
	else
		tmp = cos(im) * (re + 1.0);
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Or[LessEqual[re, -0.0132], N[Not[LessEqual[re, 0.0138]], $MachinePrecision]], N[Exp[re], $MachinePrecision], N[(N[Cos[im], $MachinePrecision] * N[(re + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -0.0132 \lor \neg \left(re \leq 0.0138\right):\\
\;\;\;\;e^{re}\\

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


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

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in im around 0 85.4%

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

    if -0.0132 < re < 0.0138

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0 99.5%

      \[\leadsto \color{blue}{\cos im + re \cdot \cos im} \]
    4. Step-by-step derivation
      1. distribute-rgt1-in99.5%

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

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

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

Alternative 6: 62.6% accurate, 1.9× speedup?

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

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

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


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

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0 67.2%

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

    if 1.6e-13 < re

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in im around 0 72.0%

      \[\leadsto \color{blue}{e^{re}} \]
    4. Taylor expanded in re around 0 45.4%

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

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

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

Alternative 7: 40.3% accurate, 11.3× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 3 \cdot 10^{+59}:\\
\;\;\;\;1 + re \cdot \left(1 + re \cdot \left(0.5 + re \cdot 0.16666666666666666\right)\right)\\

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


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

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in im around 0 84.2%

      \[\leadsto \color{blue}{e^{re}} \]
    4. Taylor expanded in re around 0 55.7%

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

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

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

    if 3e59 < im

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0 40.3%

      \[\leadsto \color{blue}{\cos im + re \cdot \cos im} \]
    4. Step-by-step derivation
      1. distribute-rgt1-in40.3%

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

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

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

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

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

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

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

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

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

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

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

Alternative 8: 37.8% accurate, 14.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 3 \cdot 10^{+59}:\\
\;\;\;\;1 + re \cdot \left(1 + re \cdot 0.5\right)\\

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


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

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in im around 0 84.2%

      \[\leadsto \color{blue}{e^{re}} \]
    4. Taylor expanded in re around 0 52.3%

      \[\leadsto \color{blue}{1 + re \cdot \left(1 + 0.5 \cdot re\right)} \]
    5. Step-by-step derivation
      1. *-commutative66.8%

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

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

    if 3e59 < im

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0 40.3%

      \[\leadsto \color{blue}{\cos im + re \cdot \cos im} \]
    4. Step-by-step derivation
      1. distribute-rgt1-in40.3%

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

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

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

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

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

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

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

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

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

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

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

Alternative 9: 32.9% accurate, 14.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq 3.4 \cdot 10^{+37}:\\
\;\;\;\;1\\

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


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

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in im around 0 75.7%

      \[\leadsto \color{blue}{e^{re}} \]
    4. Taylor expanded in re around 0 39.9%

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

    if 3.40000000000000006e37 < re

    1. Initial program 100.0%

      \[e^{re} \cdot \cos im \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0 6.1%

      \[\leadsto \color{blue}{\cos im + re \cdot \cos im} \]
    4. Step-by-step derivation
      1. distribute-rgt1-in6.1%

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

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

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

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

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

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

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

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

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

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

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

Alternative 10: 28.5% 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. Add Preprocessing
  3. Taylor expanded in im around 0 73.7%

    \[\leadsto \color{blue}{e^{re}} \]
  4. Taylor expanded in re around 0 32.4%

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

      \[\leadsto \color{blue}{re + 1} \]
  6. Simplified32.4%

    \[\leadsto \color{blue}{re + 1} \]
  7. Add Preprocessing

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. Add Preprocessing
  3. Taylor expanded in im around 0 73.7%

    \[\leadsto \color{blue}{e^{re}} \]
  4. Taylor expanded in re around 0 32.1%

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

Reproduce

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