math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 6.0s
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, 0.8× speedup?

\[\begin{array}{l} im = |im|\\ \\ \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right) \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (* (cos re) (fma 0.5 (exp im) (/ 0.5 (exp im)))))
im = abs(im);
double code(double re, double im) {
	return cos(re) * fma(0.5, exp(im), (0.5 / exp(im)));
}
im = abs(im)
function code(re, im)
	return Float64(cos(re) * fma(0.5, exp(im), Float64(0.5 / exp(im))))
end
NOTE: im should be positive before calling this function
code[re_, im_] := N[(N[Cos[re], $MachinePrecision] * N[(0.5 * N[Exp[im], $MachinePrecision] + N[(0.5 / N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{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. Step-by-step derivation
    1. *-commutative100.0%

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

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

      \[\leadsto \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{im} + e^{-im}\right)}\right) \]
    4. distribute-lft-in100.0%

      \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot e^{im} + 0.5 \cdot e^{-im}\right)} \]
    5. distribute-lft-in100.0%

      \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot \left(e^{im} + e^{-im}\right)\right)} \]
    6. distribute-rgt-in100.0%

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

      \[\leadsto \cos re \cdot \left(\color{blue}{0.5 \cdot e^{im}} + e^{-im} \cdot 0.5\right) \]
    8. fma-def100.0%

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

    \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
  4. Final simplification100.0%

    \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right) \]

Alternative 2: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} im = |im|\\ \\ \left(\cos re \cdot 0.5\right) \cdot \left(e^{im} + e^{-im}\right) \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (* (* (cos re) 0.5) (+ (exp im) (exp (- im)))))
im = abs(im);
double code(double re, double im) {
	return (cos(re) * 0.5) * (exp(im) + exp(-im));
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (cos(re) * 0.5d0) * (exp(im) + exp(-im))
end function
im = Math.abs(im);
public static double code(double re, double im) {
	return (Math.cos(re) * 0.5) * (Math.exp(im) + Math.exp(-im));
}
im = abs(im)
def code(re, im):
	return (math.cos(re) * 0.5) * (math.exp(im) + math.exp(-im))
im = abs(im)
function code(re, im)
	return Float64(Float64(cos(re) * 0.5) * Float64(exp(im) + exp(Float64(-im))))
end
im = abs(im)
function tmp = code(re, im)
	tmp = (cos(re) * 0.5) * (exp(im) + exp(-im));
end
NOTE: im should be positive before calling this function
code[re_, im_] := N[(N[(N[Cos[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(N[Exp[im], $MachinePrecision] + N[Exp[(-im)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
\left(\cos re \cdot 0.5\right) \cdot \left(e^{im} + e^{-im}\right)
\end{array}
Derivation
  1. Initial program 100.0%

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

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

Alternative 3: 93.3% accurate, 1.5× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;im \leq 14000 \lor \neg \left(im \leq 2 \cdot 10^{+154}\right):\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(e^{im} + e^{-im}\right)\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (or (<= im 14000.0) (not (<= im 2e+154)))
   (* (* (cos re) 0.5) (+ 2.0 (* im im)))
   (* 0.5 (+ (exp im) (exp (- im))))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if ((im <= 14000.0) || !(im <= 2e+154)) {
		tmp = (cos(re) * 0.5) * (2.0 + (im * im));
	} else {
		tmp = 0.5 * (exp(im) + exp(-im));
	}
	return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if ((im <= 14000.0d0) .or. (.not. (im <= 2d+154))) then
        tmp = (cos(re) * 0.5d0) * (2.0d0 + (im * im))
    else
        tmp = 0.5d0 * (exp(im) + exp(-im))
    end if
    code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if ((im <= 14000.0) || !(im <= 2e+154)) {
		tmp = (Math.cos(re) * 0.5) * (2.0 + (im * im));
	} else {
		tmp = 0.5 * (Math.exp(im) + Math.exp(-im));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if (im <= 14000.0) or not (im <= 2e+154):
		tmp = (math.cos(re) * 0.5) * (2.0 + (im * im))
	else:
		tmp = 0.5 * (math.exp(im) + math.exp(-im))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if ((im <= 14000.0) || !(im <= 2e+154))
		tmp = Float64(Float64(cos(re) * 0.5) * Float64(2.0 + Float64(im * im)));
	else
		tmp = Float64(0.5 * Float64(exp(im) + exp(Float64(-im))));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if ((im <= 14000.0) || ~((im <= 2e+154)))
		tmp = (cos(re) * 0.5) * (2.0 + (im * im));
	else
		tmp = 0.5 * (exp(im) + exp(-im));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[Or[LessEqual[im, 14000.0], N[Not[LessEqual[im, 2e+154]], $MachinePrecision]], N[(N[(N[Cos[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(2.0 + N[(im * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[Exp[im], $MachinePrecision] + N[Exp[(-im)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;im \leq 14000 \lor \neg \left(im \leq 2 \cdot 10^{+154}\right):\\
\;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \left(2 + im \cdot im\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 14000 or 2.00000000000000007e154 < im

    1. Initial program 100.0%

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

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

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

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

    if 14000 < im < 2.00000000000000007e154

    1. Initial program 100.0%

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

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

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

Alternative 4: 98.9% accurate, 1.5× speedup?

\[\begin{array}{l} im = |im|\\ \\ \cos re \cdot \left(0.5 + 0.5 \cdot e^{im}\right) \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im) :precision binary64 (* (cos re) (+ 0.5 (* 0.5 (exp im)))))
im = abs(im);
double code(double re, double im) {
	return cos(re) * (0.5 + (0.5 * exp(im)));
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = cos(re) * (0.5d0 + (0.5d0 * exp(im)))
end function
im = Math.abs(im);
public static double code(double re, double im) {
	return Math.cos(re) * (0.5 + (0.5 * Math.exp(im)));
}
im = abs(im)
def code(re, im):
	return math.cos(re) * (0.5 + (0.5 * math.exp(im)))
im = abs(im)
function code(re, im)
	return Float64(cos(re) * Float64(0.5 + Float64(0.5 * exp(im))))
end
im = abs(im)
function tmp = code(re, im)
	tmp = cos(re) * (0.5 + (0.5 * exp(im)));
end
NOTE: im should be positive before calling this function
code[re_, im_] := N[(N[Cos[re], $MachinePrecision] * N[(0.5 + N[(0.5 * N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
\cos re \cdot \left(0.5 + 0.5 \cdot 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. Step-by-step derivation
    1. *-commutative100.0%

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

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

      \[\leadsto \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{im} + e^{-im}\right)}\right) \]
    4. distribute-lft-in100.0%

      \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot e^{im} + 0.5 \cdot e^{-im}\right)} \]
    5. distribute-lft-in100.0%

      \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot \left(e^{im} + e^{-im}\right)\right)} \]
    6. distribute-rgt-in100.0%

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

      \[\leadsto \cos re \cdot \left(\color{blue}{0.5 \cdot e^{im}} + e^{-im} \cdot 0.5\right) \]
    8. fma-def100.0%

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

    \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
  4. Taylor expanded in im around 0 77.0%

    \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0.5}\right) \]
  5. Taylor expanded in re around inf 77.0%

    \[\leadsto \color{blue}{\cos re \cdot \left(0.5 + 0.5 \cdot e^{im}\right)} \]
  6. Final simplification77.0%

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

Alternative 5: 93.3% accurate, 2.7× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;im \leq 14000 \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 + 0.5 \cdot e^{im}\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (or (<= im 14000.0) (not (<= im 1.35e+154)))
   (* (* (cos re) 0.5) (+ 2.0 (* im im)))
   (+ 0.5 (* 0.5 (exp im)))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if ((im <= 14000.0) || !(im <= 1.35e+154)) {
		tmp = (cos(re) * 0.5) * (2.0 + (im * im));
	} else {
		tmp = 0.5 + (0.5 * exp(im));
	}
	return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if ((im <= 14000.0d0) .or. (.not. (im <= 1.35d+154))) then
        tmp = (cos(re) * 0.5d0) * (2.0d0 + (im * im))
    else
        tmp = 0.5d0 + (0.5d0 * exp(im))
    end if
    code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if ((im <= 14000.0) || !(im <= 1.35e+154)) {
		tmp = (Math.cos(re) * 0.5) * (2.0 + (im * im));
	} else {
		tmp = 0.5 + (0.5 * Math.exp(im));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if (im <= 14000.0) or not (im <= 1.35e+154):
		tmp = (math.cos(re) * 0.5) * (2.0 + (im * im))
	else:
		tmp = 0.5 + (0.5 * math.exp(im))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if ((im <= 14000.0) || !(im <= 1.35e+154))
		tmp = Float64(Float64(cos(re) * 0.5) * Float64(2.0 + Float64(im * im)));
	else
		tmp = Float64(0.5 + Float64(0.5 * exp(im)));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if ((im <= 14000.0) || ~((im <= 1.35e+154)))
		tmp = (cos(re) * 0.5) * (2.0 + (im * im));
	else
		tmp = 0.5 + (0.5 * exp(im));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[Or[LessEqual[im, 14000.0], N[Not[LessEqual[im, 1.35e+154]], $MachinePrecision]], N[(N[(N[Cos[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(2.0 + N[(im * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 + N[(0.5 * N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;im \leq 14000 \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\
\;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \left(2 + im \cdot im\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 14000 or 1.35000000000000003e154 < im

    1. Initial program 100.0%

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

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

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

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

    if 14000 < im < 1.35000000000000003e154

    1. Initial program 100.0%

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

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

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

        \[\leadsto \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{im} + e^{-im}\right)}\right) \]
      4. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot e^{im} + 0.5 \cdot e^{-im}\right)} \]
      5. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot \left(e^{im} + e^{-im}\right)\right)} \]
      6. distribute-rgt-in100.0%

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

        \[\leadsto \cos re \cdot \left(\color{blue}{0.5 \cdot e^{im}} + e^{-im} \cdot 0.5\right) \]
      8. fma-def100.0%

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    4. Taylor expanded in im around 0 100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0.5}\right) \]
    5. Taylor expanded in re around 0 83.3%

      \[\leadsto \color{blue}{0.5 + 0.5 \cdot e^{im}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification86.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 14000 \lor \neg \left(im \leq 1.35 \cdot 10^{+154}\right):\\ \;\;\;\;\left(\cos re \cdot 0.5\right) \cdot \left(2 + im \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 + 0.5 \cdot e^{im}\\ \end{array} \]

Alternative 6: 87.1% accurate, 2.9× speedup?

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

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


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

    1. Initial program 100.0%

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

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

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

        \[\leadsto \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{im} + e^{-im}\right)}\right) \]
      4. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot e^{im} + 0.5 \cdot e^{-im}\right)} \]
      5. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot \left(e^{im} + e^{-im}\right)\right)} \]
      6. distribute-rgt-in100.0%

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

        \[\leadsto \cos re \cdot \left(\color{blue}{0.5 \cdot e^{im}} + e^{-im} \cdot 0.5\right) \]
      8. fma-def100.0%

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    4. Taylor expanded in im around 0 69.8%

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

    if 14000 < im

    1. Initial program 100.0%

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

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

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

        \[\leadsto \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{im} + e^{-im}\right)}\right) \]
      4. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot e^{im} + 0.5 \cdot e^{-im}\right)} \]
      5. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot \left(e^{im} + e^{-im}\right)\right)} \]
      6. distribute-rgt-in100.0%

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

        \[\leadsto \cos re \cdot \left(\color{blue}{0.5 \cdot e^{im}} + e^{-im} \cdot 0.5\right) \]
      8. fma-def100.0%

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    4. Taylor expanded in im around 0 100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0.5}\right) \]
    5. Taylor expanded in re around 0 79.4%

      \[\leadsto \color{blue}{0.5 + 0.5 \cdot e^{im}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification72.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 14000:\\ \;\;\;\;\cos re\\ \mathbf{else}:\\ \;\;\;\;0.5 + 0.5 \cdot e^{im}\\ \end{array} \]

Alternative 7: 78.6% accurate, 3.0× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;im \leq 160000000:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 2.7 \cdot 10^{+154}:\\ \;\;\;\;1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot 0.0625\right)}{im \cdot \left(0.5 - im \cdot 0.25\right)}\\ \mathbf{else}:\\ \;\;\;\;1 + im \cdot \left(im \cdot 0.25\right)\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (<= im 160000000.0)
   (cos re)
   (if (<= im 2.7e+154)
     (+
      1.0
      (/
       (* (* im im) (- 0.25 (* (* im im) 0.0625)))
       (* im (- 0.5 (* im 0.25)))))
     (+ 1.0 (* im (* im 0.25))))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (im <= 160000000.0) {
		tmp = cos(re);
	} else if (im <= 2.7e+154) {
		tmp = 1.0 + (((im * im) * (0.25 - ((im * im) * 0.0625))) / (im * (0.5 - (im * 0.25))));
	} else {
		tmp = 1.0 + (im * (im * 0.25));
	}
	return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 160000000.0d0) then
        tmp = cos(re)
    else if (im <= 2.7d+154) then
        tmp = 1.0d0 + (((im * im) * (0.25d0 - ((im * im) * 0.0625d0))) / (im * (0.5d0 - (im * 0.25d0))))
    else
        tmp = 1.0d0 + (im * (im * 0.25d0))
    end if
    code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if (im <= 160000000.0) {
		tmp = Math.cos(re);
	} else if (im <= 2.7e+154) {
		tmp = 1.0 + (((im * im) * (0.25 - ((im * im) * 0.0625))) / (im * (0.5 - (im * 0.25))));
	} else {
		tmp = 1.0 + (im * (im * 0.25));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if im <= 160000000.0:
		tmp = math.cos(re)
	elif im <= 2.7e+154:
		tmp = 1.0 + (((im * im) * (0.25 - ((im * im) * 0.0625))) / (im * (0.5 - (im * 0.25))))
	else:
		tmp = 1.0 + (im * (im * 0.25))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (im <= 160000000.0)
		tmp = cos(re);
	elseif (im <= 2.7e+154)
		tmp = Float64(1.0 + Float64(Float64(Float64(im * im) * Float64(0.25 - Float64(Float64(im * im) * 0.0625))) / Float64(im * Float64(0.5 - Float64(im * 0.25)))));
	else
		tmp = Float64(1.0 + Float64(im * Float64(im * 0.25)));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 160000000.0)
		tmp = cos(re);
	elseif (im <= 2.7e+154)
		tmp = 1.0 + (((im * im) * (0.25 - ((im * im) * 0.0625))) / (im * (0.5 - (im * 0.25))));
	else
		tmp = 1.0 + (im * (im * 0.25));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[im, 160000000.0], N[Cos[re], $MachinePrecision], If[LessEqual[im, 2.7e+154], N[(1.0 + N[(N[(N[(im * im), $MachinePrecision] * N[(0.25 - N[(N[(im * im), $MachinePrecision] * 0.0625), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(im * N[(0.5 - N[(im * 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(im * N[(im * 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;im \leq 160000000:\\
\;\;\;\;\cos re\\

\mathbf{elif}\;im \leq 2.7 \cdot 10^{+154}:\\
\;\;\;\;1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot 0.0625\right)}{im \cdot \left(0.5 - im \cdot 0.25\right)}\\

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


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

    1. Initial program 100.0%

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

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

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

        \[\leadsto \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{im} + e^{-im}\right)}\right) \]
      4. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot e^{im} + 0.5 \cdot e^{-im}\right)} \]
      5. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot \left(e^{im} + e^{-im}\right)\right)} \]
      6. distribute-rgt-in100.0%

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

        \[\leadsto \cos re \cdot \left(\color{blue}{0.5 \cdot e^{im}} + e^{-im} \cdot 0.5\right) \]
      8. fma-def100.0%

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    4. Taylor expanded in im around 0 69.8%

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

    if 1.6e8 < im < 2.70000000000000006e154

    1. Initial program 100.0%

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

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

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

        \[\leadsto \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{im} + e^{-im}\right)}\right) \]
      4. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot e^{im} + 0.5 \cdot e^{-im}\right)} \]
      5. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot \left(e^{im} + e^{-im}\right)\right)} \]
      6. distribute-rgt-in100.0%

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

        \[\leadsto \cos re \cdot \left(\color{blue}{0.5 \cdot e^{im}} + e^{-im} \cdot 0.5\right) \]
      8. fma-def100.0%

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    4. Taylor expanded in im around 0 100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0.5}\right) \]
    5. Taylor expanded in re around 0 83.3%

      \[\leadsto \color{blue}{0.5 + 0.5 \cdot e^{im}} \]
    6. Taylor expanded in im around 0 3.8%

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

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

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

        \[\leadsto 1 + \left(0.5 \cdot im + \color{blue}{\left(0.25 \cdot im\right) \cdot im}\right) \]
      4. distribute-rgt-out3.8%

        \[\leadsto 1 + \color{blue}{im \cdot \left(0.5 + 0.25 \cdot im\right)} \]
    8. Simplified3.8%

      \[\leadsto \color{blue}{1 + im \cdot \left(0.5 + 0.25 \cdot im\right)} \]
    9. Step-by-step derivation
      1. distribute-lft-in3.8%

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

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

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

        \[\leadsto 1 + \frac{\color{blue}{\left(im \cdot 0.5\right)} \cdot \left(0.5 \cdot im\right) - \left(im \cdot \left(0.25 \cdot im\right)\right) \cdot \left(im \cdot \left(0.25 \cdot im\right)\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)} \]
      5. *-commutative27.2%

        \[\leadsto 1 + \frac{\left(im \cdot 0.5\right) \cdot \color{blue}{\left(im \cdot 0.5\right)} - \left(im \cdot \left(0.25 \cdot im\right)\right) \cdot \left(im \cdot \left(0.25 \cdot im\right)\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)} \]
      6. swap-sqr27.2%

        \[\leadsto 1 + \frac{\color{blue}{\left(im \cdot im\right) \cdot \left(0.5 \cdot 0.5\right)} - \left(im \cdot \left(0.25 \cdot im\right)\right) \cdot \left(im \cdot \left(0.25 \cdot im\right)\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)} \]
      7. metadata-eval27.2%

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot \color{blue}{0.25} - \left(im \cdot \left(0.25 \cdot im\right)\right) \cdot \left(im \cdot \left(0.25 \cdot im\right)\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)} \]
      8. associate-*r*27.2%

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

        \[\leadsto 1 + \frac{im \cdot \left(im \cdot 0.25\right) - \left(im \cdot \color{blue}{\left(im \cdot 0.25\right)}\right) \cdot \left(im \cdot \left(0.25 \cdot im\right)\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)} \]
      10. *-commutative27.2%

        \[\leadsto 1 + \frac{im \cdot \left(im \cdot 0.25\right) - \left(im \cdot \left(im \cdot 0.25\right)\right) \cdot \left(im \cdot \color{blue}{\left(im \cdot 0.25\right)}\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)} \]
      11. *-commutative27.2%

        \[\leadsto 1 + \frac{im \cdot \left(im \cdot 0.25\right) - \left(im \cdot \left(im \cdot 0.25\right)\right) \cdot \left(im \cdot \left(im \cdot 0.25\right)\right)}{0.5 \cdot im - im \cdot \color{blue}{\left(im \cdot 0.25\right)}} \]
    10. Applied egg-rr27.2%

      \[\leadsto 1 + \color{blue}{\frac{im \cdot \left(im \cdot 0.25\right) - \left(im \cdot \left(im \cdot 0.25\right)\right) \cdot \left(im \cdot \left(im \cdot 0.25\right)\right)}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)}} \]
    11. Step-by-step derivation
      1. associate-*r*27.2%

        \[\leadsto 1 + \frac{\color{blue}{\left(im \cdot im\right) \cdot 0.25} - \left(im \cdot \left(im \cdot 0.25\right)\right) \cdot \left(im \cdot \left(im \cdot 0.25\right)\right)}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)} \]
      2. swap-sqr27.2%

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

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot 0.25 - \left(im \cdot im\right) \cdot \color{blue}{{\left(im \cdot 0.25\right)}^{2}}}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)} \]
      4. distribute-lft-out--27.2%

        \[\leadsto 1 + \frac{\color{blue}{\left(im \cdot im\right) \cdot \left(0.25 - {\left(im \cdot 0.25\right)}^{2}\right)}}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)} \]
      5. unpow227.2%

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \color{blue}{\left(im \cdot 0.25\right) \cdot \left(im \cdot 0.25\right)}\right)}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)} \]
      6. swap-sqr27.2%

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \color{blue}{\left(im \cdot im\right) \cdot \left(0.25 \cdot 0.25\right)}\right)}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)} \]
      7. metadata-eval27.2%

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot \color{blue}{0.0625}\right)}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)} \]
      8. *-commutative27.2%

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot 0.0625\right)}{\color{blue}{im \cdot 0.5} - im \cdot \left(im \cdot 0.25\right)} \]
      9. distribute-lft-out--27.2%

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot 0.0625\right)}{\color{blue}{im \cdot \left(0.5 - im \cdot 0.25\right)}} \]
    12. Simplified27.2%

      \[\leadsto 1 + \color{blue}{\frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot 0.0625\right)}{im \cdot \left(0.5 - im \cdot 0.25\right)}} \]

    if 2.70000000000000006e154 < im

    1. Initial program 100.0%

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

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

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

        \[\leadsto \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{im} + e^{-im}\right)}\right) \]
      4. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot e^{im} + 0.5 \cdot e^{-im}\right)} \]
      5. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot \left(e^{im} + e^{-im}\right)\right)} \]
      6. distribute-rgt-in100.0%

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

        \[\leadsto \cos re \cdot \left(\color{blue}{0.5 \cdot e^{im}} + e^{-im} \cdot 0.5\right) \]
      8. fma-def100.0%

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    4. Taylor expanded in im around 0 100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0.5}\right) \]
    5. Taylor expanded in re around 0 76.9%

      \[\leadsto \color{blue}{0.5 + 0.5 \cdot e^{im}} \]
    6. Taylor expanded in im around 0 76.9%

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

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

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

        \[\leadsto 1 + \left(0.5 \cdot im + \color{blue}{\left(0.25 \cdot im\right) \cdot im}\right) \]
      4. distribute-rgt-out76.9%

        \[\leadsto 1 + \color{blue}{im \cdot \left(0.5 + 0.25 \cdot im\right)} \]
    8. Simplified76.9%

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

      \[\leadsto 1 + \color{blue}{0.25 \cdot {im}^{2}} \]
    10. Step-by-step derivation
      1. unpow276.9%

        \[\leadsto 1 + 0.25 \cdot \color{blue}{\left(im \cdot im\right)} \]
      2. *-commutative76.9%

        \[\leadsto 1 + \color{blue}{\left(im \cdot im\right) \cdot 0.25} \]
      3. associate-*r*76.9%

        \[\leadsto 1 + \color{blue}{im \cdot \left(im \cdot 0.25\right)} \]
    11. Simplified76.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 160000000:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 2.7 \cdot 10^{+154}:\\ \;\;\;\;1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot 0.0625\right)}{im \cdot \left(0.5 - im \cdot 0.25\right)}\\ \mathbf{else}:\\ \;\;\;\;1 + im \cdot \left(im \cdot 0.25\right)\\ \end{array} \]

Alternative 8: 56.0% accurate, 13.4× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;im \leq 2.7 \cdot 10^{+154}:\\ \;\;\;\;1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot 0.0625\right)}{im \cdot \left(0.5 - im \cdot 0.25\right)}\\ \mathbf{else}:\\ \;\;\;\;1 + im \cdot \left(im \cdot 0.25\right)\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (<= im 2.7e+154)
   (+
    1.0
    (/ (* (* im im) (- 0.25 (* (* im im) 0.0625))) (* im (- 0.5 (* im 0.25)))))
   (+ 1.0 (* im (* im 0.25)))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (im <= 2.7e+154) {
		tmp = 1.0 + (((im * im) * (0.25 - ((im * im) * 0.0625))) / (im * (0.5 - (im * 0.25))));
	} else {
		tmp = 1.0 + (im * (im * 0.25));
	}
	return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= 2.7d+154) then
        tmp = 1.0d0 + (((im * im) * (0.25d0 - ((im * im) * 0.0625d0))) / (im * (0.5d0 - (im * 0.25d0))))
    else
        tmp = 1.0d0 + (im * (im * 0.25d0))
    end if
    code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if (im <= 2.7e+154) {
		tmp = 1.0 + (((im * im) * (0.25 - ((im * im) * 0.0625))) / (im * (0.5 - (im * 0.25))));
	} else {
		tmp = 1.0 + (im * (im * 0.25));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if im <= 2.7e+154:
		tmp = 1.0 + (((im * im) * (0.25 - ((im * im) * 0.0625))) / (im * (0.5 - (im * 0.25))))
	else:
		tmp = 1.0 + (im * (im * 0.25))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (im <= 2.7e+154)
		tmp = Float64(1.0 + Float64(Float64(Float64(im * im) * Float64(0.25 - Float64(Float64(im * im) * 0.0625))) / Float64(im * Float64(0.5 - Float64(im * 0.25)))));
	else
		tmp = Float64(1.0 + Float64(im * Float64(im * 0.25)));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= 2.7e+154)
		tmp = 1.0 + (((im * im) * (0.25 - ((im * im) * 0.0625))) / (im * (0.5 - (im * 0.25))));
	else
		tmp = 1.0 + (im * (im * 0.25));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[im, 2.7e+154], N[(1.0 + N[(N[(N[(im * im), $MachinePrecision] * N[(0.25 - N[(N[(im * im), $MachinePrecision] * 0.0625), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(im * N[(0.5 - N[(im * 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(im * N[(im * 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;im \leq 2.7 \cdot 10^{+154}:\\
\;\;\;\;1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot 0.0625\right)}{im \cdot \left(0.5 - im \cdot 0.25\right)}\\

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


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

    1. Initial program 100.0%

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

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

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

        \[\leadsto \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{im} + e^{-im}\right)}\right) \]
      4. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot e^{im} + 0.5 \cdot e^{-im}\right)} \]
      5. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot \left(e^{im} + e^{-im}\right)\right)} \]
      6. distribute-rgt-in100.0%

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

        \[\leadsto \cos re \cdot \left(\color{blue}{0.5 \cdot e^{im}} + e^{-im} \cdot 0.5\right) \]
      8. fma-def100.0%

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    4. Taylor expanded in im around 0 72.8%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0.5}\right) \]
    5. Taylor expanded in re around 0 45.5%

      \[\leadsto \color{blue}{0.5 + 0.5 \cdot e^{im}} \]
    6. Taylor expanded in im around 0 47.3%

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

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

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

        \[\leadsto 1 + \left(0.5 \cdot im + \color{blue}{\left(0.25 \cdot im\right) \cdot im}\right) \]
      4. distribute-rgt-out47.3%

        \[\leadsto 1 + \color{blue}{im \cdot \left(0.5 + 0.25 \cdot im\right)} \]
    8. Simplified47.3%

      \[\leadsto \color{blue}{1 + im \cdot \left(0.5 + 0.25 \cdot im\right)} \]
    9. Step-by-step derivation
      1. distribute-lft-in47.3%

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

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

        \[\leadsto 1 + \color{blue}{\frac{\left(0.5 \cdot im\right) \cdot \left(0.5 \cdot im\right) - \left(im \cdot \left(0.25 \cdot im\right)\right) \cdot \left(im \cdot \left(0.25 \cdot im\right)\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)}} \]
      4. *-commutative44.8%

        \[\leadsto 1 + \frac{\color{blue}{\left(im \cdot 0.5\right)} \cdot \left(0.5 \cdot im\right) - \left(im \cdot \left(0.25 \cdot im\right)\right) \cdot \left(im \cdot \left(0.25 \cdot im\right)\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)} \]
      5. *-commutative44.8%

        \[\leadsto 1 + \frac{\left(im \cdot 0.5\right) \cdot \color{blue}{\left(im \cdot 0.5\right)} - \left(im \cdot \left(0.25 \cdot im\right)\right) \cdot \left(im \cdot \left(0.25 \cdot im\right)\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)} \]
      6. swap-sqr44.8%

        \[\leadsto 1 + \frac{\color{blue}{\left(im \cdot im\right) \cdot \left(0.5 \cdot 0.5\right)} - \left(im \cdot \left(0.25 \cdot im\right)\right) \cdot \left(im \cdot \left(0.25 \cdot im\right)\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)} \]
      7. metadata-eval44.8%

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot \color{blue}{0.25} - \left(im \cdot \left(0.25 \cdot im\right)\right) \cdot \left(im \cdot \left(0.25 \cdot im\right)\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)} \]
      8. associate-*r*44.8%

        \[\leadsto 1 + \frac{\color{blue}{im \cdot \left(im \cdot 0.25\right)} - \left(im \cdot \left(0.25 \cdot im\right)\right) \cdot \left(im \cdot \left(0.25 \cdot im\right)\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)} \]
      9. *-commutative44.8%

        \[\leadsto 1 + \frac{im \cdot \left(im \cdot 0.25\right) - \left(im \cdot \color{blue}{\left(im \cdot 0.25\right)}\right) \cdot \left(im \cdot \left(0.25 \cdot im\right)\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)} \]
      10. *-commutative44.8%

        \[\leadsto 1 + \frac{im \cdot \left(im \cdot 0.25\right) - \left(im \cdot \left(im \cdot 0.25\right)\right) \cdot \left(im \cdot \color{blue}{\left(im \cdot 0.25\right)}\right)}{0.5 \cdot im - im \cdot \left(0.25 \cdot im\right)} \]
      11. *-commutative44.8%

        \[\leadsto 1 + \frac{im \cdot \left(im \cdot 0.25\right) - \left(im \cdot \left(im \cdot 0.25\right)\right) \cdot \left(im \cdot \left(im \cdot 0.25\right)\right)}{0.5 \cdot im - im \cdot \color{blue}{\left(im \cdot 0.25\right)}} \]
    10. Applied egg-rr44.8%

      \[\leadsto 1 + \color{blue}{\frac{im \cdot \left(im \cdot 0.25\right) - \left(im \cdot \left(im \cdot 0.25\right)\right) \cdot \left(im \cdot \left(im \cdot 0.25\right)\right)}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)}} \]
    11. Step-by-step derivation
      1. associate-*r*44.8%

        \[\leadsto 1 + \frac{\color{blue}{\left(im \cdot im\right) \cdot 0.25} - \left(im \cdot \left(im \cdot 0.25\right)\right) \cdot \left(im \cdot \left(im \cdot 0.25\right)\right)}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)} \]
      2. swap-sqr44.8%

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot 0.25 - \color{blue}{\left(im \cdot im\right) \cdot \left(\left(im \cdot 0.25\right) \cdot \left(im \cdot 0.25\right)\right)}}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)} \]
      3. unpow244.8%

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot 0.25 - \left(im \cdot im\right) \cdot \color{blue}{{\left(im \cdot 0.25\right)}^{2}}}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)} \]
      4. distribute-lft-out--44.8%

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

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \color{blue}{\left(im \cdot 0.25\right) \cdot \left(im \cdot 0.25\right)}\right)}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)} \]
      6. swap-sqr44.8%

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \color{blue}{\left(im \cdot im\right) \cdot \left(0.25 \cdot 0.25\right)}\right)}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)} \]
      7. metadata-eval44.8%

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot \color{blue}{0.0625}\right)}{0.5 \cdot im - im \cdot \left(im \cdot 0.25\right)} \]
      8. *-commutative44.8%

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot 0.0625\right)}{\color{blue}{im \cdot 0.5} - im \cdot \left(im \cdot 0.25\right)} \]
      9. distribute-lft-out--44.8%

        \[\leadsto 1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot 0.0625\right)}{\color{blue}{im \cdot \left(0.5 - im \cdot 0.25\right)}} \]
    12. Simplified44.8%

      \[\leadsto 1 + \color{blue}{\frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot 0.0625\right)}{im \cdot \left(0.5 - im \cdot 0.25\right)}} \]

    if 2.70000000000000006e154 < im

    1. Initial program 100.0%

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

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

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

        \[\leadsto \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{im} + e^{-im}\right)}\right) \]
      4. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot e^{im} + 0.5 \cdot e^{-im}\right)} \]
      5. distribute-lft-in100.0%

        \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot \left(e^{im} + e^{-im}\right)\right)} \]
      6. distribute-rgt-in100.0%

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

        \[\leadsto \cos re \cdot \left(\color{blue}{0.5 \cdot e^{im}} + e^{-im} \cdot 0.5\right) \]
      8. fma-def100.0%

        \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
      9. exp-neg100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
      10. associate-*l/100.0%

        \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
      11. metadata-eval100.0%

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

      \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
    4. Taylor expanded in im around 0 100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0.5}\right) \]
    5. Taylor expanded in re around 0 76.9%

      \[\leadsto \color{blue}{0.5 + 0.5 \cdot e^{im}} \]
    6. Taylor expanded in im around 0 76.9%

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

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

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

        \[\leadsto 1 + \left(0.5 \cdot im + \color{blue}{\left(0.25 \cdot im\right) \cdot im}\right) \]
      4. distribute-rgt-out76.9%

        \[\leadsto 1 + \color{blue}{im \cdot \left(0.5 + 0.25 \cdot im\right)} \]
    8. Simplified76.9%

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

      \[\leadsto 1 + \color{blue}{0.25 \cdot {im}^{2}} \]
    10. Step-by-step derivation
      1. unpow276.9%

        \[\leadsto 1 + 0.25 \cdot \color{blue}{\left(im \cdot im\right)} \]
      2. *-commutative76.9%

        \[\leadsto 1 + \color{blue}{\left(im \cdot im\right) \cdot 0.25} \]
      3. associate-*r*76.9%

        \[\leadsto 1 + \color{blue}{im \cdot \left(im \cdot 0.25\right)} \]
    11. Simplified76.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 2.7 \cdot 10^{+154}:\\ \;\;\;\;1 + \frac{\left(im \cdot im\right) \cdot \left(0.25 - \left(im \cdot im\right) \cdot 0.0625\right)}{im \cdot \left(0.5 - im \cdot 0.25\right)}\\ \mathbf{else}:\\ \;\;\;\;1 + im \cdot \left(im \cdot 0.25\right)\\ \end{array} \]

Alternative 9: 47.7% accurate, 44.0× speedup?

\[\begin{array}{l} im = |im|\\ \\ 1 + im \cdot \left(im \cdot 0.25\right) \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im) :precision binary64 (+ 1.0 (* im (* im 0.25))))
im = abs(im);
double code(double re, double im) {
	return 1.0 + (im * (im * 0.25));
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 1.0d0 + (im * (im * 0.25d0))
end function
im = Math.abs(im);
public static double code(double re, double im) {
	return 1.0 + (im * (im * 0.25));
}
im = abs(im)
def code(re, im):
	return 1.0 + (im * (im * 0.25))
im = abs(im)
function code(re, im)
	return Float64(1.0 + Float64(im * Float64(im * 0.25)))
end
im = abs(im)
function tmp = code(re, im)
	tmp = 1.0 + (im * (im * 0.25));
end
NOTE: im should be positive before calling this function
code[re_, im_] := N[(1.0 + N[(im * N[(im * 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
1 + im \cdot \left(im \cdot 0.25\right)
\end{array}
Derivation
  1. Initial program 100.0%

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

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

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

      \[\leadsto \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{im} + e^{-im}\right)}\right) \]
    4. distribute-lft-in100.0%

      \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot e^{im} + 0.5 \cdot e^{-im}\right)} \]
    5. distribute-lft-in100.0%

      \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot \left(e^{im} + e^{-im}\right)\right)} \]
    6. distribute-rgt-in100.0%

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

      \[\leadsto \cos re \cdot \left(\color{blue}{0.5 \cdot e^{im}} + e^{-im} \cdot 0.5\right) \]
    8. fma-def100.0%

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

    \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
  4. Taylor expanded in im around 0 77.0%

    \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0.5}\right) \]
  5. Taylor expanded in re around 0 50.3%

    \[\leadsto \color{blue}{0.5 + 0.5 \cdot e^{im}} \]
  6. Taylor expanded in im around 0 51.8%

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

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

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

      \[\leadsto 1 + \left(0.5 \cdot im + \color{blue}{\left(0.25 \cdot im\right) \cdot im}\right) \]
    4. distribute-rgt-out51.8%

      \[\leadsto 1 + \color{blue}{im \cdot \left(0.5 + 0.25 \cdot im\right)} \]
  8. Simplified51.8%

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

    \[\leadsto 1 + \color{blue}{0.25 \cdot {im}^{2}} \]
  10. Step-by-step derivation
    1. unpow252.1%

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

      \[\leadsto 1 + \color{blue}{\left(im \cdot im\right) \cdot 0.25} \]
    3. associate-*r*52.1%

      \[\leadsto 1 + \color{blue}{im \cdot \left(im \cdot 0.25\right)} \]
  11. Simplified52.1%

    \[\leadsto 1 + \color{blue}{im \cdot \left(im \cdot 0.25\right)} \]
  12. Final simplification52.1%

    \[\leadsto 1 + im \cdot \left(im \cdot 0.25\right) \]

Alternative 10: 29.4% accurate, 61.6× speedup?

\[\begin{array}{l} im = |im|\\ \\ 1 + 0.5 \cdot im \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im) :precision binary64 (+ 1.0 (* 0.5 im)))
im = abs(im);
double code(double re, double im) {
	return 1.0 + (0.5 * im);
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 1.0d0 + (0.5d0 * im)
end function
im = Math.abs(im);
public static double code(double re, double im) {
	return 1.0 + (0.5 * im);
}
im = abs(im)
def code(re, im):
	return 1.0 + (0.5 * im)
im = abs(im)
function code(re, im)
	return Float64(1.0 + Float64(0.5 * im))
end
im = abs(im)
function tmp = code(re, im)
	tmp = 1.0 + (0.5 * im);
end
NOTE: im should be positive before calling this function
code[re_, im_] := N[(1.0 + N[(0.5 * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
1 + 0.5 \cdot im
\end{array}
Derivation
  1. Initial program 100.0%

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

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

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

      \[\leadsto \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{im} + e^{-im}\right)}\right) \]
    4. distribute-lft-in100.0%

      \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot e^{im} + 0.5 \cdot e^{-im}\right)} \]
    5. distribute-lft-in100.0%

      \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot \left(e^{im} + e^{-im}\right)\right)} \]
    6. distribute-rgt-in100.0%

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

      \[\leadsto \cos re \cdot \left(\color{blue}{0.5 \cdot e^{im}} + e^{-im} \cdot 0.5\right) \]
    8. fma-def100.0%

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

    \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
  4. Taylor expanded in im around 0 77.0%

    \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0.5}\right) \]
  5. Taylor expanded in re around 0 50.3%

    \[\leadsto \color{blue}{0.5 + 0.5 \cdot e^{im}} \]
  6. Taylor expanded in im around 0 31.6%

    \[\leadsto \color{blue}{1 + 0.5 \cdot im} \]
  7. Final simplification31.6%

    \[\leadsto 1 + 0.5 \cdot im \]

Alternative 11: 28.8% accurate, 308.0× speedup?

\[\begin{array}{l} im = |im|\\ \\ 1 \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im) :precision binary64 1.0)
im = abs(im);
double code(double re, double im) {
	return 1.0;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 1.0d0
end function
im = Math.abs(im);
public static double code(double re, double im) {
	return 1.0;
}
im = abs(im)
def code(re, im):
	return 1.0
im = abs(im)
function code(re, im)
	return 1.0
end
im = abs(im)
function tmp = code(re, im)
	tmp = 1.0;
end
NOTE: im should be positive before calling this function
code[re_, im_] := 1.0
\begin{array}{l}
im = |im|\\
\\
1
\end{array}
Derivation
  1. Initial program 100.0%

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

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

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

      \[\leadsto \cos re \cdot \left(0.5 \cdot \color{blue}{\left(e^{im} + e^{-im}\right)}\right) \]
    4. distribute-lft-in100.0%

      \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot e^{im} + 0.5 \cdot e^{-im}\right)} \]
    5. distribute-lft-in100.0%

      \[\leadsto \cos re \cdot \color{blue}{\left(0.5 \cdot \left(e^{im} + e^{-im}\right)\right)} \]
    6. distribute-rgt-in100.0%

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

      \[\leadsto \cos re \cdot \left(\color{blue}{0.5 \cdot e^{im}} + e^{-im} \cdot 0.5\right) \]
    8. fma-def100.0%

      \[\leadsto \cos re \cdot \color{blue}{\mathsf{fma}\left(0.5, e^{im}, e^{-im} \cdot 0.5\right)} \]
    9. exp-neg100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1}{e^{im}}} \cdot 0.5\right) \]
    10. associate-*l/100.0%

      \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{\frac{1 \cdot 0.5}{e^{im}}}\right) \]
    11. metadata-eval100.0%

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

    \[\leadsto \color{blue}{\cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \frac{0.5}{e^{im}}\right)} \]
  4. Taylor expanded in im around 0 77.0%

    \[\leadsto \cos re \cdot \mathsf{fma}\left(0.5, e^{im}, \color{blue}{0.5}\right) \]
  5. Taylor expanded in re around 0 50.3%

    \[\leadsto \color{blue}{0.5 + 0.5 \cdot e^{im}} \]
  6. Taylor expanded in im around 0 31.7%

    \[\leadsto \color{blue}{1} \]
  7. Final simplification31.7%

    \[\leadsto 1 \]

Reproduce

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