math.cos on complex, imaginary part

Percentage Accurate: 65.5% → 99.8%
Time: 14.5s
Alternatives: 22
Speedup: 0.7×

Specification

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

\\
\left(0.5 \cdot \sin 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 22 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: 65.5% accurate, 1.0× speedup?

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

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

Alternative 1: 99.8% accurate, 0.5× speedup?

\[\begin{array}{l} im\_m = \left|im\right| \\ im\_s = \mathsf{copysign}\left(1, im\right) \\ \begin{array}{l} t_0 := e^{-im\_m}\\ im\_s \cdot \begin{array}{l} \mathbf{if}\;t\_0 - e^{im\_m} \leq -0.1:\\ \;\;\;\;\mathsf{fma}\left(-e^{im\_m} \cdot \sin re, 0.5, 0.5 \cdot \left(t\_0 \cdot \sin re\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\sin re \cdot \left(im\_m \cdot \mathsf{fma}\left(-0.16666666666666666, im\_m \cdot im\_m, -1\right)\right)\\ \end{array} \end{array} \end{array} \]
im\_m = (fabs.f64 im)
im\_s = (copysign.f64 #s(literal 1 binary64) im)
(FPCore (im_s re im_m)
 :precision binary64
 (let* ((t_0 (exp (- im_m))))
   (*
    im_s
    (if (<= (- t_0 (exp im_m)) -0.1)
      (fma (- (* (exp im_m) (sin re))) 0.5 (* 0.5 (* t_0 (sin re))))
      (* (sin re) (* im_m (fma -0.16666666666666666 (* im_m im_m) -1.0)))))))
im\_m = fabs(im);
im\_s = copysign(1.0, im);
double code(double im_s, double re, double im_m) {
	double t_0 = exp(-im_m);
	double tmp;
	if ((t_0 - exp(im_m)) <= -0.1) {
		tmp = fma(-(exp(im_m) * sin(re)), 0.5, (0.5 * (t_0 * sin(re))));
	} else {
		tmp = sin(re) * (im_m * fma(-0.16666666666666666, (im_m * im_m), -1.0));
	}
	return im_s * tmp;
}
im\_m = abs(im)
im\_s = copysign(1.0, im)
function code(im_s, re, im_m)
	t_0 = exp(Float64(-im_m))
	tmp = 0.0
	if (Float64(t_0 - exp(im_m)) <= -0.1)
		tmp = fma(Float64(-Float64(exp(im_m) * sin(re))), 0.5, Float64(0.5 * Float64(t_0 * sin(re))));
	else
		tmp = Float64(sin(re) * Float64(im_m * fma(-0.16666666666666666, Float64(im_m * im_m), -1.0)));
	end
	return Float64(im_s * tmp)
end
im\_m = N[Abs[im], $MachinePrecision]
im\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[im]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[im$95$s_, re_, im$95$m_] := Block[{t$95$0 = N[Exp[(-im$95$m)], $MachinePrecision]}, N[(im$95$s * If[LessEqual[N[(t$95$0 - N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision], -0.1], N[((-N[(N[Exp[im$95$m], $MachinePrecision] * N[Sin[re], $MachinePrecision]), $MachinePrecision]) * 0.5 + N[(0.5 * N[(t$95$0 * N[Sin[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Sin[re], $MachinePrecision] * N[(im$95$m * N[(-0.16666666666666666 * N[(im$95$m * im$95$m), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]]
\begin{array}{l}
im\_m = \left|im\right|
\\
im\_s = \mathsf{copysign}\left(1, im\right)

\\
\begin{array}{l}
t_0 := e^{-im\_m}\\
im\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_0 - e^{im\_m} \leq -0.1:\\
\;\;\;\;\mathsf{fma}\left(-e^{im\_m} \cdot \sin re, 0.5, 0.5 \cdot \left(t\_0 \cdot \sin re\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\sin re \cdot \left(im\_m \cdot \mathsf{fma}\left(-0.16666666666666666, im\_m \cdot im\_m, -1\right)\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)) < -0.10000000000000001

    1. Initial program 100.0%

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

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

        \[\leadsto \left(\frac{1}{2} \cdot \sin re\right) \cdot \color{blue}{\left(e^{\mathsf{neg}\left(im\right)} - e^{im}\right)} \]
      3. sub-negN/A

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

        \[\leadsto \left(\frac{1}{2} \cdot \sin re\right) \cdot \color{blue}{\left(\left(\mathsf{neg}\left(e^{im}\right)\right) + e^{\mathsf{neg}\left(im\right)}\right)} \]
      5. distribute-rgt-inN/A

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(\sin re \cdot \left(\mathsf{neg}\left(e^{im}\right)\right), \frac{1}{2}, \color{blue}{\left(\frac{1}{2} \cdot \sin re\right)} \cdot e^{\mathsf{neg}\left(im\right)}\right) \]
      15. associate-*l*N/A

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

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

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

        \[\leadsto \mathsf{fma}\left(\sin re \cdot \left(\mathsf{neg}\left(e^{im}\right)\right), \frac{1}{2}, \frac{1}{2} \cdot \color{blue}{\left(\sin re \cdot e^{\mathsf{neg}\left(im\right)}\right)}\right) \]
    4. Applied rewrites100.0%

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

    if -0.10000000000000001 < (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))

    1. Initial program 34.3%

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

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

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

        \[\leadsto \color{blue}{\left(\frac{-1}{6} \cdot \left({im}^{2} \cdot \sin re\right) + -1 \cdot \sin re\right)} \cdot im \]
      3. associate-*r*N/A

        \[\leadsto \left(\color{blue}{\left(\frac{-1}{6} \cdot {im}^{2}\right) \cdot \sin re} + -1 \cdot \sin re\right) \cdot im \]
      4. distribute-rgt-outN/A

        \[\leadsto \color{blue}{\left(\sin re \cdot \left(\frac{-1}{6} \cdot {im}^{2} + -1\right)\right)} \cdot im \]
      5. unpow2N/A

        \[\leadsto \left(\sin re \cdot \left(\frac{-1}{6} \cdot \color{blue}{\left(im \cdot im\right)} + -1\right)\right) \cdot im \]
      6. associate-*r*N/A

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

        \[\leadsto \left(\sin re \cdot \left(\color{blue}{im \cdot \left(\frac{-1}{6} \cdot im\right)} + -1\right)\right) \cdot im \]
      8. associate-*l*N/A

        \[\leadsto \color{blue}{\sin re \cdot \left(\left(im \cdot \left(\frac{-1}{6} \cdot im\right) + -1\right) \cdot im\right)} \]
      9. lower-*.f64N/A

        \[\leadsto \color{blue}{\sin re \cdot \left(\left(im \cdot \left(\frac{-1}{6} \cdot im\right) + -1\right) \cdot im\right)} \]
      10. lower-sin.f64N/A

        \[\leadsto \color{blue}{\sin re} \cdot \left(\left(im \cdot \left(\frac{-1}{6} \cdot im\right) + -1\right) \cdot im\right) \]
      11. lower-*.f64N/A

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

        \[\leadsto \sin re \cdot \left(\left(\color{blue}{\left(\frac{-1}{6} \cdot im\right) \cdot im} + -1\right) \cdot im\right) \]
      13. associate-*r*N/A

        \[\leadsto \sin re \cdot \left(\left(\color{blue}{\frac{-1}{6} \cdot \left(im \cdot im\right)} + -1\right) \cdot im\right) \]
      14. unpow2N/A

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

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

        \[\leadsto \sin re \cdot \left(\mathsf{fma}\left(\frac{-1}{6}, \color{blue}{im \cdot im}, -1\right) \cdot im\right) \]
      17. lower-*.f6499.9

        \[\leadsto \sin re \cdot \left(\mathsf{fma}\left(-0.16666666666666666, \color{blue}{im \cdot im}, -1\right) \cdot im\right) \]
    5. Applied rewrites99.9%

      \[\leadsto \color{blue}{\sin re \cdot \left(\mathsf{fma}\left(-0.16666666666666666, im \cdot im, -1\right) \cdot im\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{-im} - e^{im} \leq -0.1:\\ \;\;\;\;\mathsf{fma}\left(-e^{im} \cdot \sin re, 0.5, 0.5 \cdot \left(e^{-im} \cdot \sin re\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\sin re \cdot \left(im \cdot \mathsf{fma}\left(-0.16666666666666666, im \cdot im, -1\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 84.8% accurate, 0.4× speedup?

\[\begin{array}{l} im\_m = \left|im\right| \\ im\_s = \mathsf{copysign}\left(1, im\right) \\ \begin{array}{l} t_0 := \left(e^{-im\_m} - e^{im\_m}\right) \cdot \left(\sin re \cdot 0.5\right)\\ t_1 := \mathsf{fma}\left(-0.16666666666666666, im\_m \cdot im\_m, -1\right)\\ im\_s \cdot \begin{array}{l} \mathbf{if}\;t\_0 \leq -\infty:\\ \;\;\;\;\left(1 - e^{im\_m}\right) \cdot \left(re \cdot 0.5\right)\\ \mathbf{elif}\;t\_0 \leq 4 \cdot 10^{-5}:\\ \;\;\;\;\sin re \cdot \left(im\_m \cdot t\_1\right)\\ \mathbf{else}:\\ \;\;\;\;im\_m \cdot \left(\mathsf{fma}\left(re, -0.16666666666666666 \cdot \left(re \cdot re\right), re\right) \cdot \mathsf{fma}\left(\mathsf{fma}\left(im\_m, im\_m \cdot -0.0001984126984126984, -0.008333333333333333\right), im\_m \cdot \left(im\_m \cdot \left(im\_m \cdot im\_m\right)\right), t\_1\right)\right)\\ \end{array} \end{array} \end{array} \]
im\_m = (fabs.f64 im)
im\_s = (copysign.f64 #s(literal 1 binary64) im)
(FPCore (im_s re im_m)
 :precision binary64
 (let* ((t_0 (* (- (exp (- im_m)) (exp im_m)) (* (sin re) 0.5)))
        (t_1 (fma -0.16666666666666666 (* im_m im_m) -1.0)))
   (*
    im_s
    (if (<= t_0 (- INFINITY))
      (* (- 1.0 (exp im_m)) (* re 0.5))
      (if (<= t_0 4e-5)
        (* (sin re) (* im_m t_1))
        (*
         im_m
         (*
          (fma re (* -0.16666666666666666 (* re re)) re)
          (fma
           (fma im_m (* im_m -0.0001984126984126984) -0.008333333333333333)
           (* im_m (* im_m (* im_m im_m)))
           t_1))))))))
im\_m = fabs(im);
im\_s = copysign(1.0, im);
double code(double im_s, double re, double im_m) {
	double t_0 = (exp(-im_m) - exp(im_m)) * (sin(re) * 0.5);
	double t_1 = fma(-0.16666666666666666, (im_m * im_m), -1.0);
	double tmp;
	if (t_0 <= -((double) INFINITY)) {
		tmp = (1.0 - exp(im_m)) * (re * 0.5);
	} else if (t_0 <= 4e-5) {
		tmp = sin(re) * (im_m * t_1);
	} else {
		tmp = im_m * (fma(re, (-0.16666666666666666 * (re * re)), re) * fma(fma(im_m, (im_m * -0.0001984126984126984), -0.008333333333333333), (im_m * (im_m * (im_m * im_m))), t_1));
	}
	return im_s * tmp;
}
im\_m = abs(im)
im\_s = copysign(1.0, im)
function code(im_s, re, im_m)
	t_0 = Float64(Float64(exp(Float64(-im_m)) - exp(im_m)) * Float64(sin(re) * 0.5))
	t_1 = fma(-0.16666666666666666, Float64(im_m * im_m), -1.0)
	tmp = 0.0
	if (t_0 <= Float64(-Inf))
		tmp = Float64(Float64(1.0 - exp(im_m)) * Float64(re * 0.5));
	elseif (t_0 <= 4e-5)
		tmp = Float64(sin(re) * Float64(im_m * t_1));
	else
		tmp = Float64(im_m * Float64(fma(re, Float64(-0.16666666666666666 * Float64(re * re)), re) * fma(fma(im_m, Float64(im_m * -0.0001984126984126984), -0.008333333333333333), Float64(im_m * Float64(im_m * Float64(im_m * im_m))), t_1)));
	end
	return Float64(im_s * tmp)
end
im\_m = N[Abs[im], $MachinePrecision]
im\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[im]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[im$95$s_, re_, im$95$m_] := Block[{t$95$0 = N[(N[(N[Exp[(-im$95$m)], $MachinePrecision] - N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision] * N[(N[Sin[re], $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(-0.16666666666666666 * N[(im$95$m * im$95$m), $MachinePrecision] + -1.0), $MachinePrecision]}, N[(im$95$s * If[LessEqual[t$95$0, (-Infinity)], N[(N[(1.0 - N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision] * N[(re * 0.5), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 4e-5], N[(N[Sin[re], $MachinePrecision] * N[(im$95$m * t$95$1), $MachinePrecision]), $MachinePrecision], N[(im$95$m * N[(N[(re * N[(-0.16666666666666666 * N[(re * re), $MachinePrecision]), $MachinePrecision] + re), $MachinePrecision] * N[(N[(im$95$m * N[(im$95$m * -0.0001984126984126984), $MachinePrecision] + -0.008333333333333333), $MachinePrecision] * N[(im$95$m * N[(im$95$m * N[(im$95$m * im$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]]]
\begin{array}{l}
im\_m = \left|im\right|
\\
im\_s = \mathsf{copysign}\left(1, im\right)

\\
\begin{array}{l}
t_0 := \left(e^{-im\_m} - e^{im\_m}\right) \cdot \left(\sin re \cdot 0.5\right)\\
t_1 := \mathsf{fma}\left(-0.16666666666666666, im\_m \cdot im\_m, -1\right)\\
im\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_0 \leq -\infty:\\
\;\;\;\;\left(1 - e^{im\_m}\right) \cdot \left(re \cdot 0.5\right)\\

\mathbf{elif}\;t\_0 \leq 4 \cdot 10^{-5}:\\
\;\;\;\;\sin re \cdot \left(im\_m \cdot t\_1\right)\\

\mathbf{else}:\\
\;\;\;\;im\_m \cdot \left(\mathsf{fma}\left(re, -0.16666666666666666 \cdot \left(re \cdot re\right), re\right) \cdot \mathsf{fma}\left(\mathsf{fma}\left(im\_m, im\_m \cdot -0.0001984126984126984, -0.008333333333333333\right), im\_m \cdot \left(im\_m \cdot \left(im\_m \cdot im\_m\right)\right), t\_1\right)\right)\\


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

    1. Initial program 100.0%

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

      \[\leadsto \left(\frac{1}{2} \cdot \sin re\right) \cdot \left(\color{blue}{1} - e^{im}\right) \]
    4. Step-by-step derivation
      1. Applied rewrites100.0%

        \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \left(\color{blue}{1} - e^{im}\right) \]
      2. Taylor expanded in re around 0

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot re\right)} \cdot \left(1 - e^{im}\right) \]
      3. Step-by-step derivation
        1. lower-*.f6474.2

          \[\leadsto \color{blue}{\left(0.5 \cdot re\right)} \cdot \left(1 - e^{im}\right) \]
      4. Applied rewrites74.2%

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

      if -inf.0 < (*.f64 (*.f64 #s(literal 1/2 binary64) (sin.f64 re)) (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))) < 4.00000000000000033e-5

      1. Initial program 31.6%

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

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

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

          \[\leadsto \color{blue}{\left(\frac{-1}{6} \cdot \left({im}^{2} \cdot \sin re\right) + -1 \cdot \sin re\right)} \cdot im \]
        3. associate-*r*N/A

          \[\leadsto \left(\color{blue}{\left(\frac{-1}{6} \cdot {im}^{2}\right) \cdot \sin re} + -1 \cdot \sin re\right) \cdot im \]
        4. distribute-rgt-outN/A

          \[\leadsto \color{blue}{\left(\sin re \cdot \left(\frac{-1}{6} \cdot {im}^{2} + -1\right)\right)} \cdot im \]
        5. unpow2N/A

          \[\leadsto \left(\sin re \cdot \left(\frac{-1}{6} \cdot \color{blue}{\left(im \cdot im\right)} + -1\right)\right) \cdot im \]
        6. associate-*r*N/A

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

          \[\leadsto \left(\sin re \cdot \left(\color{blue}{im \cdot \left(\frac{-1}{6} \cdot im\right)} + -1\right)\right) \cdot im \]
        8. associate-*l*N/A

          \[\leadsto \color{blue}{\sin re \cdot \left(\left(im \cdot \left(\frac{-1}{6} \cdot im\right) + -1\right) \cdot im\right)} \]
        9. lower-*.f64N/A

          \[\leadsto \color{blue}{\sin re \cdot \left(\left(im \cdot \left(\frac{-1}{6} \cdot im\right) + -1\right) \cdot im\right)} \]
        10. lower-sin.f64N/A

          \[\leadsto \color{blue}{\sin re} \cdot \left(\left(im \cdot \left(\frac{-1}{6} \cdot im\right) + -1\right) \cdot im\right) \]
        11. lower-*.f64N/A

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

          \[\leadsto \sin re \cdot \left(\left(\color{blue}{\left(\frac{-1}{6} \cdot im\right) \cdot im} + -1\right) \cdot im\right) \]
        13. associate-*r*N/A

          \[\leadsto \sin re \cdot \left(\left(\color{blue}{\frac{-1}{6} \cdot \left(im \cdot im\right)} + -1\right) \cdot im\right) \]
        14. unpow2N/A

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

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

          \[\leadsto \sin re \cdot \left(\mathsf{fma}\left(\frac{-1}{6}, \color{blue}{im \cdot im}, -1\right) \cdot im\right) \]
        17. lower-*.f6498.8

          \[\leadsto \sin re \cdot \left(\mathsf{fma}\left(-0.16666666666666666, \color{blue}{im \cdot im}, -1\right) \cdot im\right) \]
      5. Applied rewrites98.8%

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

      if 4.00000000000000033e-5 < (*.f64 (*.f64 #s(literal 1/2 binary64) (sin.f64 re)) (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)))

      1. Initial program 99.9%

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

        \[\leadsto \color{blue}{im \cdot \left(-1 \cdot \sin re + {im}^{2} \cdot \left(\frac{-1}{6} \cdot \sin re + {im}^{2} \cdot \left(\frac{-1}{120} \cdot \sin re + \frac{-1}{5040} \cdot \left({im}^{2} \cdot \sin re\right)\right)\right)\right)} \]
      4. Applied rewrites86.7%

        \[\leadsto \color{blue}{im \cdot \left(\sin re \cdot \mathsf{fma}\left(\mathsf{fma}\left(im, im \cdot -0.0001984126984126984, -0.008333333333333333\right), im \cdot \left(im \cdot \left(im \cdot im\right)\right), \mathsf{fma}\left(-0.16666666666666666, im \cdot im, -1\right)\right)\right)} \]
      5. Taylor expanded in re around 0

        \[\leadsto im \cdot \left(\left(re \cdot \left(1 + \frac{-1}{6} \cdot {re}^{2}\right)\right) \cdot \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(im, im \cdot \frac{-1}{5040}, \frac{-1}{120}\right)}, im \cdot \left(im \cdot \left(im \cdot im\right)\right), \mathsf{fma}\left(\frac{-1}{6}, im \cdot im, -1\right)\right)\right) \]
      6. Step-by-step derivation
        1. Applied rewrites67.0%

          \[\leadsto im \cdot \left(\mathsf{fma}\left(re, \left(re \cdot re\right) \cdot -0.16666666666666666, re\right) \cdot \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(im, im \cdot -0.0001984126984126984, -0.008333333333333333\right)}, im \cdot \left(im \cdot \left(im \cdot im\right)\right), \mathsf{fma}\left(-0.16666666666666666, im \cdot im, -1\right)\right)\right) \]
      7. Recombined 3 regimes into one program.
      8. Final simplification84.8%

        \[\leadsto \begin{array}{l} \mathbf{if}\;\left(e^{-im} - e^{im}\right) \cdot \left(\sin re \cdot 0.5\right) \leq -\infty:\\ \;\;\;\;\left(1 - e^{im}\right) \cdot \left(re \cdot 0.5\right)\\ \mathbf{elif}\;\left(e^{-im} - e^{im}\right) \cdot \left(\sin re \cdot 0.5\right) \leq 4 \cdot 10^{-5}:\\ \;\;\;\;\sin re \cdot \left(im \cdot \mathsf{fma}\left(-0.16666666666666666, im \cdot im, -1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(\mathsf{fma}\left(re, -0.16666666666666666 \cdot \left(re \cdot re\right), re\right) \cdot \mathsf{fma}\left(\mathsf{fma}\left(im, im \cdot -0.0001984126984126984, -0.008333333333333333\right), im \cdot \left(im \cdot \left(im \cdot im\right)\right), \mathsf{fma}\left(-0.16666666666666666, im \cdot im, -1\right)\right)\right)\\ \end{array} \]
      9. Add Preprocessing

      Developer Target 1: 99.8% accurate, 1.0× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\left|im\right| < 1:\\ \;\;\;\;-\sin re \cdot \left(\left(im + \left(\left(0.16666666666666666 \cdot im\right) \cdot im\right) \cdot im\right) + \left(\left(\left(\left(0.008333333333333333 \cdot im\right) \cdot im\right) \cdot im\right) \cdot im\right) \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\\ \end{array} \end{array} \]
      (FPCore (re im)
       :precision binary64
       (if (< (fabs im) 1.0)
         (-
          (*
           (sin re)
           (+
            (+ im (* (* (* 0.16666666666666666 im) im) im))
            (* (* (* (* (* 0.008333333333333333 im) im) im) im) im))))
         (* (* 0.5 (sin re)) (- (exp (- im)) (exp im)))))
      double code(double re, double im) {
      	double tmp;
      	if (fabs(im) < 1.0) {
      		tmp = -(sin(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im)));
      	} else {
      		tmp = (0.5 * sin(re)) * (exp(-im) - exp(im));
      	}
      	return tmp;
      }
      
      real(8) function code(re, im)
          real(8), intent (in) :: re
          real(8), intent (in) :: im
          real(8) :: tmp
          if (abs(im) < 1.0d0) then
              tmp = -(sin(re) * ((im + (((0.16666666666666666d0 * im) * im) * im)) + (((((0.008333333333333333d0 * im) * im) * im) * im) * im)))
          else
              tmp = (0.5d0 * sin(re)) * (exp(-im) - exp(im))
          end if
          code = tmp
      end function
      
      public static double code(double re, double im) {
      	double tmp;
      	if (Math.abs(im) < 1.0) {
      		tmp = -(Math.sin(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im)));
      	} else {
      		tmp = (0.5 * Math.sin(re)) * (Math.exp(-im) - Math.exp(im));
      	}
      	return tmp;
      }
      
      def code(re, im):
      	tmp = 0
      	if math.fabs(im) < 1.0:
      		tmp = -(math.sin(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im)))
      	else:
      		tmp = (0.5 * math.sin(re)) * (math.exp(-im) - math.exp(im))
      	return tmp
      
      function code(re, im)
      	tmp = 0.0
      	if (abs(im) < 1.0)
      		tmp = Float64(-Float64(sin(re) * Float64(Float64(im + Float64(Float64(Float64(0.16666666666666666 * im) * im) * im)) + Float64(Float64(Float64(Float64(Float64(0.008333333333333333 * im) * im) * im) * im) * im))));
      	else
      		tmp = Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(-im)) - exp(im)));
      	end
      	return tmp
      end
      
      function tmp_2 = code(re, im)
      	tmp = 0.0;
      	if (abs(im) < 1.0)
      		tmp = -(sin(re) * ((im + (((0.16666666666666666 * im) * im) * im)) + (((((0.008333333333333333 * im) * im) * im) * im) * im)));
      	else
      		tmp = (0.5 * sin(re)) * (exp(-im) - exp(im));
      	end
      	tmp_2 = tmp;
      end
      
      code[re_, im_] := If[Less[N[Abs[im], $MachinePrecision], 1.0], (-N[(N[Sin[re], $MachinePrecision] * N[(N[(im + N[(N[(N[(0.16666666666666666 * im), $MachinePrecision] * im), $MachinePrecision] * im), $MachinePrecision]), $MachinePrecision] + N[(N[(N[(N[(N[(0.008333333333333333 * im), $MachinePrecision] * im), $MachinePrecision] * im), $MachinePrecision] * im), $MachinePrecision] * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      \mathbf{if}\;\left|im\right| < 1:\\
      \;\;\;\;-\sin re \cdot \left(\left(im + \left(\left(0.16666666666666666 \cdot im\right) \cdot im\right) \cdot im\right) + \left(\left(\left(\left(0.008333333333333333 \cdot im\right) \cdot im\right) \cdot im\right) \cdot im\right) \cdot im\right)\\
      
      \mathbf{else}:\\
      \;\;\;\;\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\\
      
      
      \end{array}
      \end{array}
      

      Reproduce

      ?
      herbie shell --seed 2024226 
      (FPCore (re im)
        :name "math.cos on complex, imaginary part"
        :precision binary64
      
        :alt
        (! :herbie-platform default (if (< (fabs im) 1) (- (* (sin re) (+ im (* 1/6 im im im) (* 1/120 im im im im im)))) (* (* 1/2 (sin re)) (- (exp (- im)) (exp im)))))
      
        (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))