math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 13.0s
Alternatives: 24
Speedup: 1.5×

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 24 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.4× speedup?

\[\begin{array}{l} \\ \frac{0.5 \cdot \cos re}{\frac{1}{2 \cdot \cosh im}} \end{array} \]
(FPCore (re im)
 :precision binary64
 (/ (* 0.5 (cos re)) (/ 1.0 (* 2.0 (cosh im)))))
double code(double re, double im) {
	return (0.5 * cos(re)) / (1.0 / (2.0 * cosh(im)));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (0.5d0 * cos(re)) / (1.0d0 / (2.0d0 * cosh(im)))
end function
public static double code(double re, double im) {
	return (0.5 * Math.cos(re)) / (1.0 / (2.0 * Math.cosh(im)));
}
def code(re, im):
	return (0.5 * math.cos(re)) / (1.0 / (2.0 * math.cosh(im)))
function code(re, im)
	return Float64(Float64(0.5 * cos(re)) / Float64(1.0 / Float64(2.0 * cosh(im))))
end
function tmp = code(re, im)
	tmp = (0.5 * cos(re)) / (1.0 / (2.0 * cosh(im)));
end
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] / N[(1.0 / N[(2.0 * N[Cosh[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{0.5 \cdot \cos re}{\frac{1}{2 \cdot \cosh im}}
\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. Step-by-step derivation
    1. lift-cos.f64N/A

      \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{\cos re}\right) \cdot \left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \]
    2. lift-*.f64N/A

      \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot \cos re\right)} \cdot \left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \]
    3. lift-neg.f64N/A

      \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(e^{\color{blue}{\mathsf{neg}\left(im\right)}} + e^{im}\right) \]
    4. lift-exp.f64N/A

      \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(\color{blue}{e^{\mathsf{neg}\left(im\right)}} + e^{im}\right) \]
    5. lift-exp.f64N/A

      \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(e^{\mathsf{neg}\left(im\right)} + \color{blue}{e^{im}}\right) \]
    6. flip-+N/A

      \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \color{blue}{\frac{e^{\mathsf{neg}\left(im\right)} \cdot e^{\mathsf{neg}\left(im\right)} - e^{im} \cdot e^{im}}{e^{\mathsf{neg}\left(im\right)} - e^{im}}} \]
    7. clear-numN/A

      \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \color{blue}{\frac{1}{\frac{e^{\mathsf{neg}\left(im\right)} - e^{im}}{e^{\mathsf{neg}\left(im\right)} \cdot e^{\mathsf{neg}\left(im\right)} - e^{im} \cdot e^{im}}}} \]
    8. un-div-invN/A

      \[\leadsto \color{blue}{\frac{\frac{1}{2} \cdot \cos re}{\frac{e^{\mathsf{neg}\left(im\right)} - e^{im}}{e^{\mathsf{neg}\left(im\right)} \cdot e^{\mathsf{neg}\left(im\right)} - e^{im} \cdot e^{im}}}} \]
    9. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{\frac{1}{2} \cdot \cos re}{\frac{e^{\mathsf{neg}\left(im\right)} - e^{im}}{e^{\mathsf{neg}\left(im\right)} \cdot e^{\mathsf{neg}\left(im\right)} - e^{im} \cdot e^{im}}}} \]
    10. clear-numN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \cos re}{\color{blue}{\frac{1}{\frac{e^{\mathsf{neg}\left(im\right)} \cdot e^{\mathsf{neg}\left(im\right)} - e^{im} \cdot e^{im}}{e^{\mathsf{neg}\left(im\right)} - e^{im}}}}} \]
    11. flip-+N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \cos re}{\frac{1}{\color{blue}{e^{\mathsf{neg}\left(im\right)} + e^{im}}}} \]
    12. lift-+.f64N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \cos re}{\frac{1}{\color{blue}{e^{\mathsf{neg}\left(im\right)} + e^{im}}}} \]
  4. Applied egg-rr100.0%

    \[\leadsto \color{blue}{\frac{0.5 \cdot \cos re}{\frac{1}{2 \cdot \cosh im}}} \]
  5. Add Preprocessing

Alternative 2: 99.7% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)\\ \mathbf{if}\;t\_0 \leq -\infty:\\ \;\;\;\;\cosh im \cdot \mathsf{fma}\left(re \cdot re, -0.5, 1\right)\\ \mathbf{elif}\;t\_0 \leq 0.9999999999971182:\\ \;\;\;\;\cos re \cdot \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im \cdot im, 0.001388888888888889, 0.041666666666666664\right), 0.5\right), 1\right)\\ \mathbf{else}:\\ \;\;\;\;\cosh im\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im)))))
   (if (<= t_0 (- INFINITY))
     (* (cosh im) (fma (* re re) -0.5 1.0))
     (if (<= t_0 0.9999999999971182)
       (*
        (cos re)
        (fma
         (* im im)
         (fma
          (* im im)
          (fma (* im im) 0.001388888888888889 0.041666666666666664)
          0.5)
         1.0))
       (cosh im)))))
double code(double re, double im) {
	double t_0 = (0.5 * cos(re)) * (exp(-im) + exp(im));
	double tmp;
	if (t_0 <= -((double) INFINITY)) {
		tmp = cosh(im) * fma((re * re), -0.5, 1.0);
	} else if (t_0 <= 0.9999999999971182) {
		tmp = cos(re) * fma((im * im), fma((im * im), fma((im * im), 0.001388888888888889, 0.041666666666666664), 0.5), 1.0);
	} else {
		tmp = cosh(im);
	}
	return tmp;
}
function code(re, im)
	t_0 = Float64(Float64(0.5 * cos(re)) * Float64(exp(Float64(-im)) + exp(im)))
	tmp = 0.0
	if (t_0 <= Float64(-Inf))
		tmp = Float64(cosh(im) * fma(Float64(re * re), -0.5, 1.0));
	elseif (t_0 <= 0.9999999999971182)
		tmp = Float64(cos(re) * fma(Float64(im * im), fma(Float64(im * im), fma(Float64(im * im), 0.001388888888888889, 0.041666666666666664), 0.5), 1.0));
	else
		tmp = cosh(im);
	end
	return tmp
end
code[re_, im_] := Block[{t$95$0 = N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, (-Infinity)], N[(N[Cosh[im], $MachinePrecision] * N[(N[(re * re), $MachinePrecision] * -0.5 + 1.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 0.9999999999971182], N[(N[Cos[re], $MachinePrecision] * N[(N[(im * im), $MachinePrecision] * N[(N[(im * im), $MachinePrecision] * N[(N[(im * im), $MachinePrecision] * 0.001388888888888889 + 0.041666666666666664), $MachinePrecision] + 0.5), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[Cosh[im], $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)\\
\mathbf{if}\;t\_0 \leq -\infty:\\
\;\;\;\;\cosh im \cdot \mathsf{fma}\left(re \cdot re, -0.5, 1\right)\\

\mathbf{elif}\;t\_0 \leq 0.9999999999971182:\\
\;\;\;\;\cos re \cdot \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im \cdot im, 0.001388888888888889, 0.041666666666666664\right), 0.5\right), 1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))) < -inf.0

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-cos.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{\cos re}\right) \cdot \left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \]
      2. lift-*.f64N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot \cos re\right)} \cdot \left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \]
      3. lift-neg.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(e^{\color{blue}{\mathsf{neg}\left(im\right)}} + e^{im}\right) \]
      4. lift-exp.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(\color{blue}{e^{\mathsf{neg}\left(im\right)}} + e^{im}\right) \]
      5. lift-exp.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(e^{\mathsf{neg}\left(im\right)} + \color{blue}{e^{im}}\right) \]
      6. flip-+N/A

        \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \color{blue}{\frac{e^{\mathsf{neg}\left(im\right)} \cdot e^{\mathsf{neg}\left(im\right)} - e^{im} \cdot e^{im}}{e^{\mathsf{neg}\left(im\right)} - e^{im}}} \]
      7. clear-numN/A

        \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \color{blue}{\frac{1}{\frac{e^{\mathsf{neg}\left(im\right)} - e^{im}}{e^{\mathsf{neg}\left(im\right)} \cdot e^{\mathsf{neg}\left(im\right)} - e^{im} \cdot e^{im}}}} \]
      8. un-div-invN/A

        \[\leadsto \color{blue}{\frac{\frac{1}{2} \cdot \cos re}{\frac{e^{\mathsf{neg}\left(im\right)} - e^{im}}{e^{\mathsf{neg}\left(im\right)} \cdot e^{\mathsf{neg}\left(im\right)} - e^{im} \cdot e^{im}}}} \]
      9. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{\frac{1}{2} \cdot \cos re}{\frac{e^{\mathsf{neg}\left(im\right)} - e^{im}}{e^{\mathsf{neg}\left(im\right)} \cdot e^{\mathsf{neg}\left(im\right)} - e^{im} \cdot e^{im}}}} \]
      10. clear-numN/A

        \[\leadsto \frac{\frac{1}{2} \cdot \cos re}{\color{blue}{\frac{1}{\frac{e^{\mathsf{neg}\left(im\right)} \cdot e^{\mathsf{neg}\left(im\right)} - e^{im} \cdot e^{im}}{e^{\mathsf{neg}\left(im\right)} - e^{im}}}}} \]
      11. flip-+N/A

        \[\leadsto \frac{\frac{1}{2} \cdot \cos re}{\frac{1}{\color{blue}{e^{\mathsf{neg}\left(im\right)} + e^{im}}}} \]
      12. lift-+.f64N/A

        \[\leadsto \frac{\frac{1}{2} \cdot \cos re}{\frac{1}{\color{blue}{e^{\mathsf{neg}\left(im\right)} + e^{im}}}} \]
    4. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\frac{0.5 \cdot \cos re}{\frac{1}{2 \cdot \cosh im}}} \]
    5. Step-by-step derivation
      1. lift-cos.f64N/A

        \[\leadsto \frac{\frac{1}{2} \cdot \color{blue}{\cos re}}{\frac{1}{2 \cdot \cosh im}} \]
      2. lift-*.f64N/A

        \[\leadsto \frac{\color{blue}{\frac{1}{2} \cdot \cos re}}{\frac{1}{2 \cdot \cosh im}} \]
      3. lift-cosh.f64N/A

        \[\leadsto \frac{\frac{1}{2} \cdot \cos re}{\frac{1}{2 \cdot \color{blue}{\cosh im}}} \]
      4. associate-/r*N/A

        \[\leadsto \frac{\frac{1}{2} \cdot \cos re}{\color{blue}{\frac{\frac{1}{2}}{\cosh im}}} \]
      5. metadata-evalN/A

        \[\leadsto \frac{\frac{1}{2} \cdot \cos re}{\frac{\color{blue}{\frac{1}{2}}}{\cosh im}} \]
      6. associate-/r/N/A

        \[\leadsto \color{blue}{\frac{\frac{1}{2} \cdot \cos re}{\frac{1}{2}} \cdot \cosh im} \]
      7. div-invN/A

        \[\leadsto \color{blue}{\left(\left(\frac{1}{2} \cdot \cos re\right) \cdot \frac{1}{\frac{1}{2}}\right)} \cdot \cosh im \]
      8. metadata-evalN/A

        \[\leadsto \left(\left(\frac{1}{2} \cdot \cos re\right) \cdot \color{blue}{2}\right) \cdot \cosh im \]
      9. lift-*.f64N/A

        \[\leadsto \left(\color{blue}{\left(\frac{1}{2} \cdot \cos re\right)} \cdot 2\right) \cdot \cosh im \]
      10. *-commutativeN/A

        \[\leadsto \left(\color{blue}{\left(\cos re \cdot \frac{1}{2}\right)} \cdot 2\right) \cdot \cosh im \]
      11. associate-*l*N/A

        \[\leadsto \color{blue}{\left(\cos re \cdot \left(\frac{1}{2} \cdot 2\right)\right)} \cdot \cosh im \]
      12. metadata-evalN/A

        \[\leadsto \left(\cos re \cdot \color{blue}{1}\right) \cdot \cosh im \]
      13. *-rgt-identityN/A

        \[\leadsto \color{blue}{\cos re} \cdot \cosh im \]
      14. *-commutativeN/A

        \[\leadsto \color{blue}{\cosh im \cdot \cos re} \]
      15. lower-*.f64100.0

        \[\leadsto \color{blue}{\cosh im \cdot \cos re} \]
    6. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\cosh im \cdot \cos re} \]
    7. Taylor expanded in re around 0

      \[\leadsto \cosh im \cdot \color{blue}{\left(1 + \frac{-1}{2} \cdot {re}^{2}\right)} \]
    8. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cosh im \cdot \color{blue}{\left(\frac{-1}{2} \cdot {re}^{2} + 1\right)} \]
      2. *-commutativeN/A

        \[\leadsto \cosh im \cdot \left(\color{blue}{{re}^{2} \cdot \frac{-1}{2}} + 1\right) \]
      3. lower-fma.f64N/A

        \[\leadsto \cosh im \cdot \color{blue}{\mathsf{fma}\left({re}^{2}, \frac{-1}{2}, 1\right)} \]
      4. unpow2N/A

        \[\leadsto \cosh im \cdot \mathsf{fma}\left(\color{blue}{re \cdot re}, \frac{-1}{2}, 1\right) \]
      5. lower-*.f64100.0

        \[\leadsto \cosh im \cdot \mathsf{fma}\left(\color{blue}{re \cdot re}, -0.5, 1\right) \]
    9. Simplified100.0%

      \[\leadsto \cosh im \cdot \color{blue}{\mathsf{fma}\left(re \cdot re, -0.5, 1\right)} \]

    if -inf.0 < (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))) < 0.999999999997118194

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-cos.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{\cos re}\right) \cdot \left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \]
      2. lift-*.f64N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot \cos re\right)} \cdot \left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \]
      3. lift-neg.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(e^{\color{blue}{\mathsf{neg}\left(im\right)}} + e^{im}\right) \]
      4. lift-exp.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(\color{blue}{e^{\mathsf{neg}\left(im\right)}} + e^{im}\right) \]
      5. lift-exp.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(e^{\mathsf{neg}\left(im\right)} + \color{blue}{e^{im}}\right) \]
      6. lift-+.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \color{blue}{\left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right)} \]
      7. *-commutativeN/A

        \[\leadsto \color{blue}{\left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \cdot \left(\frac{1}{2} \cdot \cos re\right)} \]
      8. lift-*.f64N/A

        \[\leadsto \left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \cdot \color{blue}{\left(\frac{1}{2} \cdot \cos re\right)} \]
      9. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \cdot \frac{1}{2}\right) \cdot \cos re} \]
      10. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \cdot \frac{1}{2}\right) \cdot \cos re} \]
    4. Applied egg-rr100.0%

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

      \[\leadsto \color{blue}{\left(1 + {im}^{2} \cdot \left(\frac{1}{2} + {im}^{2} \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {im}^{2}\right)\right)\right)} \cdot \cos re \]
    6. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\left({im}^{2} \cdot \left(\frac{1}{2} + {im}^{2} \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {im}^{2}\right)\right) + 1\right)} \cdot \cos re \]
      2. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left({im}^{2}, \frac{1}{2} + {im}^{2} \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {im}^{2}\right), 1\right)} \cdot \cos re \]
      3. unpow2N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{im \cdot im}, \frac{1}{2} + {im}^{2} \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {im}^{2}\right), 1\right) \cdot \cos re \]
      4. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{im \cdot im}, \frac{1}{2} + {im}^{2} \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {im}^{2}\right), 1\right) \cdot \cos re \]
      5. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(im \cdot im, \color{blue}{{im}^{2} \cdot \left(\frac{1}{24} + \frac{1}{720} \cdot {im}^{2}\right) + \frac{1}{2}}, 1\right) \cdot \cos re \]
      6. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(im \cdot im, \color{blue}{\mathsf{fma}\left({im}^{2}, \frac{1}{24} + \frac{1}{720} \cdot {im}^{2}, \frac{1}{2}\right)}, 1\right) \cdot \cos re \]
      7. unpow2N/A

        \[\leadsto \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(\color{blue}{im \cdot im}, \frac{1}{24} + \frac{1}{720} \cdot {im}^{2}, \frac{1}{2}\right), 1\right) \cdot \cos re \]
      8. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(\color{blue}{im \cdot im}, \frac{1}{24} + \frac{1}{720} \cdot {im}^{2}, \frac{1}{2}\right), 1\right) \cdot \cos re \]
      9. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im \cdot im, \color{blue}{\frac{1}{720} \cdot {im}^{2} + \frac{1}{24}}, \frac{1}{2}\right), 1\right) \cdot \cos re \]
      10. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im \cdot im, \color{blue}{{im}^{2} \cdot \frac{1}{720}} + \frac{1}{24}, \frac{1}{2}\right), 1\right) \cdot \cos re \]
      11. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im \cdot im, \color{blue}{\mathsf{fma}\left({im}^{2}, \frac{1}{720}, \frac{1}{24}\right)}, \frac{1}{2}\right), 1\right) \cdot \cos re \]
      12. unpow2N/A

        \[\leadsto \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(\color{blue}{im \cdot im}, \frac{1}{720}, \frac{1}{24}\right), \frac{1}{2}\right), 1\right) \cdot \cos re \]
      13. lower-*.f6499.7

        \[\leadsto \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(\color{blue}{im \cdot im}, 0.001388888888888889, 0.041666666666666664\right), 0.5\right), 1\right) \cdot \cos re \]
    7. Simplified99.7%

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

    if 0.999999999997118194 < (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)))

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-cos.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{\cos re}\right) \cdot \left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \]
      2. lift-*.f64N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot \cos re\right)} \cdot \left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \]
      3. lift-neg.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(e^{\color{blue}{\mathsf{neg}\left(im\right)}} + e^{im}\right) \]
      4. lift-exp.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(\color{blue}{e^{\mathsf{neg}\left(im\right)}} + e^{im}\right) \]
      5. lift-exp.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(e^{\mathsf{neg}\left(im\right)} + \color{blue}{e^{im}}\right) \]
      6. lift-+.f64N/A

        \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \color{blue}{\left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right)} \]
      7. *-commutativeN/A

        \[\leadsto \color{blue}{\left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \cdot \left(\frac{1}{2} \cdot \cos re\right)} \]
      8. lift-*.f64N/A

        \[\leadsto \left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \cdot \color{blue}{\left(\frac{1}{2} \cdot \cos re\right)} \]
      9. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \cdot \frac{1}{2}\right) \cdot \cos re} \]
      10. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\left(e^{\mathsf{neg}\left(im\right)} + e^{im}\right) \cdot \frac{1}{2}\right) \cdot \cos re} \]
    4. Applied egg-rr100.0%

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

      \[\leadsto \left(1 \cdot \cosh im\right) \cdot \color{blue}{1} \]
    6. Step-by-step derivation
      1. Simplified99.7%

        \[\leadsto \left(1 \cdot \cosh im\right) \cdot \color{blue}{1} \]
      2. Step-by-step derivation
        1. lift-cosh.f64N/A

          \[\leadsto \left(1 \cdot \color{blue}{\cosh im}\right) \cdot 1 \]
        2. *-lft-identityN/A

          \[\leadsto \color{blue}{\cosh im} \cdot 1 \]
        3. *-rgt-identity99.7

          \[\leadsto \color{blue}{\cosh im} \]
      3. Applied egg-rr99.7%

        \[\leadsto \color{blue}{\cosh im} \]
    7. Recombined 3 regimes into one program.
    8. Final simplification99.7%

      \[\leadsto \begin{array}{l} \mathbf{if}\;\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \leq -\infty:\\ \;\;\;\;\cosh im \cdot \mathsf{fma}\left(re \cdot re, -0.5, 1\right)\\ \mathbf{elif}\;\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \leq 0.9999999999971182:\\ \;\;\;\;\cos re \cdot \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im \cdot im, 0.001388888888888889, 0.041666666666666664\right), 0.5\right), 1\right)\\ \mathbf{else}:\\ \;\;\;\;\cosh im\\ \end{array} \]
    9. Add Preprocessing

    Reproduce

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