math.cos on complex, real part

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

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

Alternative 1: 100.0% accurate, 1.0× speedup?

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

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

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

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

Alternative 2: 78.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 640 \lor \neg \left(im \leq 1.1 \cdot 10^{+141}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{else}:\\ \;\;\;\;1 + \mathsf{log1p}\left(\mathsf{expm1}\left({re}^{2} \cdot -0.5\right)\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= im 640.0) (not (<= im 1.1e+141)))
   (* (* 0.5 (cos re)) (fma im im 2.0))
   (+ 1.0 (log1p (expm1 (* (pow re 2.0) -0.5))))))
double code(double re, double im) {
	double tmp;
	if ((im <= 640.0) || !(im <= 1.1e+141)) {
		tmp = (0.5 * cos(re)) * fma(im, im, 2.0);
	} else {
		tmp = 1.0 + log1p(expm1((pow(re, 2.0) * -0.5)));
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if ((im <= 640.0) || !(im <= 1.1e+141))
		tmp = Float64(Float64(0.5 * cos(re)) * fma(im, im, 2.0));
	else
		tmp = Float64(1.0 + log1p(expm1(Float64((re ^ 2.0) * -0.5))));
	end
	return tmp
end
code[re_, im_] := If[Or[LessEqual[im, 640.0], N[Not[LessEqual[im, 1.1e+141]], $MachinePrecision]], N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[Log[1 + N[(Exp[N[(N[Power[re, 2.0], $MachinePrecision] * -0.5), $MachinePrecision]] - 1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 640 \lor \neg \left(im \leq 1.1 \cdot 10^{+141}\right):\\
\;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\

\mathbf{else}:\\
\;\;\;\;1 + \mathsf{log1p}\left(\mathsf{expm1}\left({re}^{2} \cdot -0.5\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 640 or 1.1e141 < im

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define88.0%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified88.0%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]

    if 640 < im < 1.1e141

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define5.0%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified5.0%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in re around 0 17.6%

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

        \[\leadsto \color{blue}{\left(-0.25 \cdot {re}^{2}\right) \cdot \left(2 + {im}^{2}\right)} + 0.5 \cdot \left(2 + {im}^{2}\right) \]
      2. distribute-rgt-out17.6%

        \[\leadsto \color{blue}{\left(2 + {im}^{2}\right) \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right)} \]
      3. +-commutative17.6%

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

        \[\leadsto \left(\color{blue}{im \cdot im} + 2\right) \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      5. fma-undefine17.6%

        \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      6. +-commutative17.6%

        \[\leadsto \mathsf{fma}\left(im, im, 2\right) \cdot \color{blue}{\left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    8. Simplified17.6%

      \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right) \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    9. Taylor expanded in im around 0 8.3%

      \[\leadsto \color{blue}{2 \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    10. Step-by-step derivation
      1. distribute-rgt-in8.3%

        \[\leadsto \color{blue}{0.5 \cdot 2 + \left(-0.25 \cdot {re}^{2}\right) \cdot 2} \]
      2. metadata-eval8.3%

        \[\leadsto \color{blue}{1} + \left(-0.25 \cdot {re}^{2}\right) \cdot 2 \]
      3. *-commutative8.3%

        \[\leadsto 1 + \color{blue}{\left({re}^{2} \cdot -0.25\right)} \cdot 2 \]
      4. associate-*l*8.3%

        \[\leadsto 1 + \color{blue}{{re}^{2} \cdot \left(-0.25 \cdot 2\right)} \]
      5. metadata-eval8.3%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 640 \lor \neg \left(im \leq 1.1 \cdot 10^{+141}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{else}:\\ \;\;\;\;1 + \mathsf{log1p}\left(\mathsf{expm1}\left({re}^{2} \cdot -0.5\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 77.7% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \cos re\\ \mathbf{if}\;im \leq 2.3 \cdot 10^{-6}:\\ \;\;\;\;t\_0 \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\ \;\;\;\;\mathsf{fma}\left(im, im, 2\right) \cdot \left(0.5 + {re}^{2} \cdot -0.25\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0 \cdot {im}^{2}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (cos re))))
   (if (<= im 2.3e-6)
     (* t_0 (fma im im 2.0))
     (if (<= im 1.35e+154)
       (* (fma im im 2.0) (+ 0.5 (* (pow re 2.0) -0.25)))
       (* t_0 (pow im 2.0))))))
double code(double re, double im) {
	double t_0 = 0.5 * cos(re);
	double tmp;
	if (im <= 2.3e-6) {
		tmp = t_0 * fma(im, im, 2.0);
	} else if (im <= 1.35e+154) {
		tmp = fma(im, im, 2.0) * (0.5 + (pow(re, 2.0) * -0.25));
	} else {
		tmp = t_0 * pow(im, 2.0);
	}
	return tmp;
}
function code(re, im)
	t_0 = Float64(0.5 * cos(re))
	tmp = 0.0
	if (im <= 2.3e-6)
		tmp = Float64(t_0 * fma(im, im, 2.0));
	elseif (im <= 1.35e+154)
		tmp = Float64(fma(im, im, 2.0) * Float64(0.5 + Float64((re ^ 2.0) * -0.25)));
	else
		tmp = Float64(t_0 * (im ^ 2.0));
	end
	return tmp
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[im, 2.3e-6], N[(t$95$0 * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.35e+154], N[(N[(im * im + 2.0), $MachinePrecision] * N[(0.5 + N[(N[Power[re, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

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

\mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\
\;\;\;\;\mathsf{fma}\left(im, im, 2\right) \cdot \left(0.5 + {re}^{2} \cdot -0.25\right)\\

\mathbf{else}:\\
\;\;\;\;t\_0 \cdot {im}^{2}\\


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

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define87.2%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified87.2%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]

    if 2.3e-6 < im < 1.35000000000000003e154

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define9.6%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified9.6%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in re around 0 20.3%

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

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

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

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

        \[\leadsto \left(\color{blue}{im \cdot im} + 2\right) \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      5. fma-undefine20.3%

        \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      6. +-commutative20.3%

        \[\leadsto \mathsf{fma}\left(im, im, 2\right) \cdot \color{blue}{\left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    8. Simplified20.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right) \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]

    if 1.35000000000000003e154 < im

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define100.0%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified100.0%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in im around inf 100.0%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 2.3 \cdot 10^{-6}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\ \;\;\;\;\mathsf{fma}\left(im, im, 2\right) \cdot \left(0.5 + {re}^{2} \cdot -0.25\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot {im}^{2}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 77.2% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 6600000000 \lor \neg \left(im \leq 8.5 \cdot 10^{+118}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{else}:\\ \;\;\;\;1 + \sqrt{{re}^{4} \cdot 0.25}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= im 6600000000.0) (not (<= im 8.5e+118)))
   (* (* 0.5 (cos re)) (fma im im 2.0))
   (+ 1.0 (sqrt (* (pow re 4.0) 0.25)))))
double code(double re, double im) {
	double tmp;
	if ((im <= 6600000000.0) || !(im <= 8.5e+118)) {
		tmp = (0.5 * cos(re)) * fma(im, im, 2.0);
	} else {
		tmp = 1.0 + sqrt((pow(re, 4.0) * 0.25));
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if ((im <= 6600000000.0) || !(im <= 8.5e+118))
		tmp = Float64(Float64(0.5 * cos(re)) * fma(im, im, 2.0));
	else
		tmp = Float64(1.0 + sqrt(Float64((re ^ 4.0) * 0.25)));
	end
	return tmp
end
code[re_, im_] := If[Or[LessEqual[im, 6600000000.0], N[Not[LessEqual[im, 8.5e+118]], $MachinePrecision]], N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[Sqrt[N[(N[Power[re, 4.0], $MachinePrecision] * 0.25), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq 6600000000 \lor \neg \left(im \leq 8.5 \cdot 10^{+118}\right):\\
\;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\

\mathbf{else}:\\
\;\;\;\;1 + \sqrt{{re}^{4} \cdot 0.25}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 6.6e9 or 8.50000000000000033e118 < im

    1. Initial program 100.0%

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

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified84.8%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]

    if 6.6e9 < im < 8.50000000000000033e118

    1. Initial program 100.0%

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

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified4.7%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in re around 0 10.7%

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

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

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

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

        \[\leadsto \left(\color{blue}{im \cdot im} + 2\right) \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      5. fma-undefine10.7%

        \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      6. +-commutative10.7%

        \[\leadsto \mathsf{fma}\left(im, im, 2\right) \cdot \color{blue}{\left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    8. Simplified10.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right) \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    9. Taylor expanded in im around 0 6.2%

      \[\leadsto \color{blue}{2 \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    10. Step-by-step derivation
      1. distribute-rgt-in6.2%

        \[\leadsto \color{blue}{0.5 \cdot 2 + \left(-0.25 \cdot {re}^{2}\right) \cdot 2} \]
      2. metadata-eval6.2%

        \[\leadsto \color{blue}{1} + \left(-0.25 \cdot {re}^{2}\right) \cdot 2 \]
      3. *-commutative6.2%

        \[\leadsto 1 + \color{blue}{\left({re}^{2} \cdot -0.25\right)} \cdot 2 \]
      4. associate-*l*6.2%

        \[\leadsto 1 + \color{blue}{{re}^{2} \cdot \left(-0.25 \cdot 2\right)} \]
      5. metadata-eval6.2%

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

      \[\leadsto \color{blue}{1 + {re}^{2} \cdot -0.5} \]
    12. Step-by-step derivation
      1. add-sqr-sqrt0.8%

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

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

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

        \[\leadsto 1 + \sqrt{\color{blue}{{re}^{\left(2 + 2\right)}} \cdot \left(-0.5 \cdot -0.5\right)} \]
      5. metadata-eval29.8%

        \[\leadsto 1 + \sqrt{{re}^{\color{blue}{4}} \cdot \left(-0.5 \cdot -0.5\right)} \]
      6. metadata-eval29.8%

        \[\leadsto 1 + \sqrt{{re}^{4} \cdot \color{blue}{0.25}} \]
    13. Applied egg-rr29.8%

      \[\leadsto 1 + \color{blue}{\sqrt{{re}^{4} \cdot 0.25}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification79.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 6600000000 \lor \neg \left(im \leq 8.5 \cdot 10^{+118}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{else}:\\ \;\;\;\;1 + \sqrt{{re}^{4} \cdot 0.25}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 64.2% accurate, 1.4× speedup?

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

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

\mathbf{elif}\;im \leq 3.8 \cdot 10^{+133}:\\
\;\;\;\;1 + {re}^{2} \cdot -0.5\\

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


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

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define87.2%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified87.2%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in im around 0 73.1%

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

    if 2.3e-6 < im < 3.8000000000000002e133

    1. Initial program 100.0%

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

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified9.8%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in re around 0 19.9%

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

        \[\leadsto \color{blue}{\left(-0.25 \cdot {re}^{2}\right) \cdot \left(2 + {im}^{2}\right)} + 0.5 \cdot \left(2 + {im}^{2}\right) \]
      2. distribute-rgt-out19.9%

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

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

        \[\leadsto \left(\color{blue}{im \cdot im} + 2\right) \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      5. fma-undefine19.9%

        \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      6. +-commutative19.9%

        \[\leadsto \mathsf{fma}\left(im, im, 2\right) \cdot \color{blue}{\left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    8. Simplified19.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right) \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    9. Taylor expanded in im around 0 12.0%

      \[\leadsto \color{blue}{2 \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    10. Step-by-step derivation
      1. distribute-rgt-in12.0%

        \[\leadsto \color{blue}{0.5 \cdot 2 + \left(-0.25 \cdot {re}^{2}\right) \cdot 2} \]
      2. metadata-eval12.0%

        \[\leadsto \color{blue}{1} + \left(-0.25 \cdot {re}^{2}\right) \cdot 2 \]
      3. *-commutative12.0%

        \[\leadsto 1 + \color{blue}{\left({re}^{2} \cdot -0.25\right)} \cdot 2 \]
      4. associate-*l*12.0%

        \[\leadsto 1 + \color{blue}{{re}^{2} \cdot \left(-0.25 \cdot 2\right)} \]
      5. metadata-eval12.0%

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

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

    if 3.8000000000000002e133 < im

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define85.5%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified85.5%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in im around inf 85.5%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 2.3 \cdot 10^{-6}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 3.8 \cdot 10^{+133}:\\ \;\;\;\;1 + {re}^{2} \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot {im}^{2}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 64.8% accurate, 1.4× speedup?

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

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

\mathbf{elif}\;im \leq 5.2 \cdot 10^{+118}:\\
\;\;\;\;1 + \sqrt{{re}^{4} \cdot 0.25}\\

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


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

    1. Initial program 100.0%

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

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified86.8%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in im around 0 72.6%

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

    if 1.95e6 < im < 5.20000000000000032e118

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define4.6%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified4.6%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in re around 0 10.2%

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

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

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

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

        \[\leadsto \left(\color{blue}{im \cdot im} + 2\right) \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      5. fma-undefine10.2%

        \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      6. +-commutative10.2%

        \[\leadsto \mathsf{fma}\left(im, im, 2\right) \cdot \color{blue}{\left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    8. Simplified10.2%

      \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right) \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    9. Taylor expanded in im around 0 6.0%

      \[\leadsto \color{blue}{2 \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    10. Step-by-step derivation
      1. distribute-rgt-in6.0%

        \[\leadsto \color{blue}{0.5 \cdot 2 + \left(-0.25 \cdot {re}^{2}\right) \cdot 2} \]
      2. metadata-eval6.0%

        \[\leadsto \color{blue}{1} + \left(-0.25 \cdot {re}^{2}\right) \cdot 2 \]
      3. *-commutative6.0%

        \[\leadsto 1 + \color{blue}{\left({re}^{2} \cdot -0.25\right)} \cdot 2 \]
      4. associate-*l*6.0%

        \[\leadsto 1 + \color{blue}{{re}^{2} \cdot \left(-0.25 \cdot 2\right)} \]
      5. metadata-eval6.0%

        \[\leadsto 1 + {re}^{2} \cdot \color{blue}{-0.5} \]
    11. Simplified6.0%

      \[\leadsto \color{blue}{1 + {re}^{2} \cdot -0.5} \]
    12. Step-by-step derivation
      1. add-sqr-sqrt0.8%

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

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

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

        \[\leadsto 1 + \sqrt{\color{blue}{{re}^{\left(2 + 2\right)}} \cdot \left(-0.5 \cdot -0.5\right)} \]
      5. metadata-eval27.8%

        \[\leadsto 1 + \sqrt{{re}^{\color{blue}{4}} \cdot \left(-0.5 \cdot -0.5\right)} \]
      6. metadata-eval27.8%

        \[\leadsto 1 + \sqrt{{re}^{4} \cdot \color{blue}{0.25}} \]
    13. Applied egg-rr27.8%

      \[\leadsto 1 + \color{blue}{\sqrt{{re}^{4} \cdot 0.25}} \]

    if 5.20000000000000032e118 < im

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define80.5%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified80.5%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in im around inf 80.5%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 1950000:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 5.2 \cdot 10^{+118}:\\ \;\;\;\;1 + \sqrt{{re}^{4} \cdot 0.25}\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot {im}^{2}\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 61.1% accurate, 2.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 2.3 \cdot 10^{-6}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 5.2 \cdot 10^{+140}:\\ \;\;\;\;1 + {re}^{2} \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 2.3e-6)
   (cos re)
   (if (<= im 5.2e+140)
     (+ 1.0 (* (pow re 2.0) -0.5))
     (* 0.5 (fma im im 2.0)))))
double code(double re, double im) {
	double tmp;
	if (im <= 2.3e-6) {
		tmp = cos(re);
	} else if (im <= 5.2e+140) {
		tmp = 1.0 + (pow(re, 2.0) * -0.5);
	} else {
		tmp = 0.5 * fma(im, im, 2.0);
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (im <= 2.3e-6)
		tmp = cos(re);
	elseif (im <= 5.2e+140)
		tmp = Float64(1.0 + Float64((re ^ 2.0) * -0.5));
	else
		tmp = Float64(0.5 * fma(im, im, 2.0));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[im, 2.3e-6], N[Cos[re], $MachinePrecision], If[LessEqual[im, 5.2e+140], N[(1.0 + N[(N[Power[re, 2.0], $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;im \leq 5.2 \cdot 10^{+140}:\\
\;\;\;\;1 + {re}^{2} \cdot -0.5\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\


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

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define87.2%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified87.2%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in im around 0 73.1%

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

    if 2.3e-6 < im < 5.2000000000000002e140

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define9.6%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified9.6%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in re around 0 21.5%

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

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

        \[\leadsto \color{blue}{\left(2 + {im}^{2}\right) \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right)} \]
      3. +-commutative21.5%

        \[\leadsto \color{blue}{\left({im}^{2} + 2\right)} \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      4. unpow221.5%

        \[\leadsto \left(\color{blue}{im \cdot im} + 2\right) \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      5. fma-undefine21.5%

        \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      6. +-commutative21.5%

        \[\leadsto \mathsf{fma}\left(im, im, 2\right) \cdot \color{blue}{\left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    8. Simplified21.5%

      \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right) \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    9. Taylor expanded in im around 0 11.3%

      \[\leadsto \color{blue}{2 \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    10. Step-by-step derivation
      1. distribute-rgt-in11.3%

        \[\leadsto \color{blue}{0.5 \cdot 2 + \left(-0.25 \cdot {re}^{2}\right) \cdot 2} \]
      2. metadata-eval11.3%

        \[\leadsto \color{blue}{1} + \left(-0.25 \cdot {re}^{2}\right) \cdot 2 \]
      3. *-commutative11.3%

        \[\leadsto 1 + \color{blue}{\left({re}^{2} \cdot -0.25\right)} \cdot 2 \]
      4. associate-*l*11.3%

        \[\leadsto 1 + \color{blue}{{re}^{2} \cdot \left(-0.25 \cdot 2\right)} \]
      5. metadata-eval11.3%

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

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

    if 5.2000000000000002e140 < im

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define91.2%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified91.2%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in re around 0 76.3%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left({im}^{2} + 2\right)} \]
      2. unpow276.3%

        \[\leadsto 0.5 \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-undefine76.3%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    8. Simplified76.3%

      \[\leadsto \color{blue}{0.5 \cdot \mathsf{fma}\left(im, im, 2\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification65.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 2.3 \cdot 10^{-6}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 5.2 \cdot 10^{+140}:\\ \;\;\;\;1 + {re}^{2} \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 61.1% accurate, 2.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 780:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 2.35 \cdot 10^{+138}:\\ \;\;\;\;{re}^{2} \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 780.0)
   (cos re)
   (if (<= im 2.35e+138) (* (pow re 2.0) -0.5) (* 0.5 (fma im im 2.0)))))
double code(double re, double im) {
	double tmp;
	if (im <= 780.0) {
		tmp = cos(re);
	} else if (im <= 2.35e+138) {
		tmp = pow(re, 2.0) * -0.5;
	} else {
		tmp = 0.5 * fma(im, im, 2.0);
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (im <= 780.0)
		tmp = cos(re);
	elseif (im <= 2.35e+138)
		tmp = Float64((re ^ 2.0) * -0.5);
	else
		tmp = Float64(0.5 * fma(im, im, 2.0));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[im, 780.0], N[Cos[re], $MachinePrecision], If[LessEqual[im, 2.35e+138], N[(N[Power[re, 2.0], $MachinePrecision] * -0.5), $MachinePrecision], N[(0.5 * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;im \leq 2.35 \cdot 10^{+138}:\\
\;\;\;\;{re}^{2} \cdot -0.5\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \mathsf{fma}\left(im, im, 2\right)\\


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

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define87.2%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified87.2%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in im around 0 73.0%

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

    if 780 < im < 2.3499999999999999e138

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define5.0%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified5.0%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in re around 0 17.6%

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

        \[\leadsto \color{blue}{\left(-0.25 \cdot {re}^{2}\right) \cdot \left(2 + {im}^{2}\right)} + 0.5 \cdot \left(2 + {im}^{2}\right) \]
      2. distribute-rgt-out17.6%

        \[\leadsto \color{blue}{\left(2 + {im}^{2}\right) \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right)} \]
      3. +-commutative17.6%

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

        \[\leadsto \left(\color{blue}{im \cdot im} + 2\right) \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      5. fma-undefine17.6%

        \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      6. +-commutative17.6%

        \[\leadsto \mathsf{fma}\left(im, im, 2\right) \cdot \color{blue}{\left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    8. Simplified17.6%

      \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right) \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    9. Taylor expanded in re around inf 15.9%

      \[\leadsto \mathsf{fma}\left(im, im, 2\right) \cdot \color{blue}{\left(-0.25 \cdot {re}^{2}\right)} \]
    10. Taylor expanded in im around 0 7.5%

      \[\leadsto \color{blue}{-0.5 \cdot {re}^{2}} \]

    if 2.3499999999999999e138 < im

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define91.2%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified91.2%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in re around 0 76.3%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left({im}^{2} + 2\right)} \]
      2. unpow276.3%

        \[\leadsto 0.5 \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-undefine76.3%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    8. Simplified76.3%

      \[\leadsto \color{blue}{0.5 \cdot \mathsf{fma}\left(im, im, 2\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification64.8%

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

Alternative 9: 61.1% accurate, 2.7× speedup?

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

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

\mathbf{elif}\;im \leq 1.1 \cdot 10^{+141}:\\
\;\;\;\;{re}^{2} \cdot -0.5\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot {im}^{2}\\


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

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define87.2%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified87.2%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in im around 0 73.0%

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

    if 620 < im < 1.1e141

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define5.0%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified5.0%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in re around 0 17.6%

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

        \[\leadsto \color{blue}{\left(-0.25 \cdot {re}^{2}\right) \cdot \left(2 + {im}^{2}\right)} + 0.5 \cdot \left(2 + {im}^{2}\right) \]
      2. distribute-rgt-out17.6%

        \[\leadsto \color{blue}{\left(2 + {im}^{2}\right) \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right)} \]
      3. +-commutative17.6%

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

        \[\leadsto \left(\color{blue}{im \cdot im} + 2\right) \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      5. fma-undefine17.6%

        \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      6. +-commutative17.6%

        \[\leadsto \mathsf{fma}\left(im, im, 2\right) \cdot \color{blue}{\left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    8. Simplified17.6%

      \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right) \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    9. Taylor expanded in re around inf 15.9%

      \[\leadsto \mathsf{fma}\left(im, im, 2\right) \cdot \color{blue}{\left(-0.25 \cdot {re}^{2}\right)} \]
    10. Taylor expanded in im around 0 7.5%

      \[\leadsto \color{blue}{-0.5 \cdot {re}^{2}} \]

    if 1.1e141 < im

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define91.2%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified91.2%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in im around inf 91.2%

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

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

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot {im}^{2}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification64.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 620:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 1.1 \cdot 10^{+141}:\\ \;\;\;\;{re}^{2} \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot {im}^{2}\\ \end{array} \]
  5. Add Preprocessing

Alternative 10: 53.6% accurate, 2.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq 700:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;{re}^{2} \cdot -0.5\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im 700.0) (cos re) (* (pow re 2.0) -0.5)))
double code(double re, double im) {
	double tmp;
	if (im <= 700.0) {
		tmp = cos(re);
	} else {
		tmp = pow(re, 2.0) * -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 <= 700.0d0) then
        tmp = cos(re)
    else
        tmp = (re ** 2.0d0) * (-0.5d0)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= 700.0) {
		tmp = Math.cos(re);
	} else {
		tmp = Math.pow(re, 2.0) * -0.5;
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= 700.0:
		tmp = math.cos(re)
	else:
		tmp = math.pow(re, 2.0) * -0.5
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= 700.0)
		tmp = cos(re);
	else
		tmp = Float64((re ^ 2.0) * -0.5);
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 700.0)
		tmp = cos(re);
	else
		tmp = (re ^ 2.0) * -0.5;
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, 700.0], N[Cos[re], $MachinePrecision], N[(N[Power[re, 2.0], $MachinePrecision] * -0.5), $MachinePrecision]]
\begin{array}{l}

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

\mathbf{else}:\\
\;\;\;\;{re}^{2} \cdot -0.5\\


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

    1. Initial program 100.0%

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
      3. fma-define87.2%

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified87.2%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in im around 0 73.0%

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

    if 700 < im

    1. Initial program 100.0%

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

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

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

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

        \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    5. Simplified52.1%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
    6. Taylor expanded in re around 0 8.5%

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

        \[\leadsto \color{blue}{\left(-0.25 \cdot {re}^{2}\right) \cdot \left(2 + {im}^{2}\right)} + 0.5 \cdot \left(2 + {im}^{2}\right) \]
      2. distribute-rgt-out37.8%

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

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

        \[\leadsto \left(\color{blue}{im \cdot im} + 2\right) \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      5. fma-undefine37.8%

        \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \cdot \left(-0.25 \cdot {re}^{2} + 0.5\right) \]
      6. +-commutative37.8%

        \[\leadsto \mathsf{fma}\left(im, im, 2\right) \cdot \color{blue}{\left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    8. Simplified37.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(im, im, 2\right) \cdot \left(0.5 + -0.25 \cdot {re}^{2}\right)} \]
    9. Taylor expanded in re around inf 15.4%

      \[\leadsto \mathsf{fma}\left(im, im, 2\right) \cdot \color{blue}{\left(-0.25 \cdot {re}^{2}\right)} \]
    10. Taylor expanded in im around 0 5.5%

      \[\leadsto \color{blue}{-0.5 \cdot {re}^{2}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification53.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 700:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;{re}^{2} \cdot -0.5\\ \end{array} \]
  5. Add Preprocessing

Alternative 11: 50.9% accurate, 3.0× speedup?

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

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

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

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

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

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{im \cdot im} + 2\right) \]
    3. fma-define77.0%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
  5. Simplified77.0%

    \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\mathsf{fma}\left(im, im, 2\right)} \]
  6. Taylor expanded in im around 0 52.5%

    \[\leadsto \color{blue}{\cos re} \]
  7. Final simplification52.5%

    \[\leadsto \cos re \]
  8. Add Preprocessing

Reproduce

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