Average Error: 29.3 → 0.1
Time: 6.3s
Precision: binary64
\[\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right) \]
\[\begin{array}{l} t_0 := e^{-im}\\ t_1 := t_0 - e^{im}\\ \mathbf{if}\;t_1 \leq -0.06182422377168262:\\ \;\;\;\;\begin{array}{l} t_2 := \sqrt{t_0}\\ \left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(t_2, t_2, -e^{im}\right) \end{array}\\ \mathbf{elif}\;t_1 \leq 0.09698978473685482:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(-0.5 \cdot \left(\cos re \cdot \mathsf{fma}\left(0.016666666666666666, {im}^{5}, \mathsf{fma}\left(0.3333333333333333, {im}^{3}, \mathsf{fma}\left(im, 2, 0.0003968253968253968 \cdot {im}^{7}\right)\right)\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot \left(0.5 \cdot \sqrt[3]{{\cos re}^{3}}\right)\\ \end{array} \]
\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right)
\begin{array}{l}
t_0 := e^{-im}\\
t_1 := t_0 - e^{im}\\
\mathbf{if}\;t_1 \leq -0.06182422377168262:\\
\;\;\;\;\begin{array}{l}
t_2 := \sqrt{t_0}\\
\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(t_2, t_2, -e^{im}\right)
\end{array}\\

\mathbf{elif}\;t_1 \leq 0.09698978473685482:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(-0.5 \cdot \left(\cos re \cdot \mathsf{fma}\left(0.016666666666666666, {im}^{5}, \mathsf{fma}\left(0.3333333333333333, {im}^{3}, \mathsf{fma}\left(im, 2, 0.0003968253968253968 \cdot {im}^{7}\right)\right)\right)\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;t_1 \cdot \left(0.5 \cdot \sqrt[3]{{\cos re}^{3}}\right)\\


\end{array}
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (- (exp (- 0.0 im)) (exp im))))
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (exp (- im))) (t_1 (- t_0 (exp im))))
   (if (<= t_1 -0.06182422377168262)
     (let* ((t_2 (sqrt t_0))) (* (* 0.5 (cos re)) (fma t_2 t_2 (- (exp im)))))
     (if (<= t_1 0.09698978473685482)
       (expm1
        (log1p
         (*
          -0.5
          (*
           (cos re)
           (fma
            0.016666666666666666
            (pow im 5.0)
            (fma
             0.3333333333333333
             (pow im 3.0)
             (fma im 2.0 (* 0.0003968253968253968 (pow im 7.0)))))))))
       (* t_1 (* 0.5 (cbrt (pow (cos re) 3.0))))))))
double code(double re, double im) {
	return (0.5 * cos(re)) * (exp(0.0 - im) - exp(im));
}
double code(double re, double im) {
	double t_0 = exp(-im);
	double t_1 = t_0 - exp(im);
	double tmp;
	if (t_1 <= -0.06182422377168262) {
		double t_2_1 = sqrt(t_0);
		tmp = (0.5 * cos(re)) * fma(t_2_1, t_2_1, -exp(im));
	} else if (t_1 <= 0.09698978473685482) {
		tmp = expm1(log1p(-0.5 * (cos(re) * fma(0.016666666666666666, pow(im, 5.0), fma(0.3333333333333333, pow(im, 3.0), fma(im, 2.0, (0.0003968253968253968 * pow(im, 7.0))))))));
	} else {
		tmp = t_1 * (0.5 * cbrt(pow(cos(re), 3.0)));
	}
	return tmp;
}

Error

Bits error versus re

Bits error versus im

Target

Original29.3
Target0.1
Herbie0.1
\[\begin{array}{l} \mathbf{if}\;\left|im\right| < 1:\\ \;\;\;\;-\cos 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 \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right)\\ \end{array} \]

Derivation

  1. Split input into 3 regimes
  2. if (-.f64 (exp.f64 (-.f64 0 im)) (exp.f64 im)) < -0.06182422377168262

    1. Initial program 0.0

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right) \]
    2. Applied add-sqr-sqrt_binary640.0

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{\sqrt{e^{0 - im}} \cdot \sqrt{e^{0 - im}}} - e^{im}\right) \]
    3. Applied fma-neg_binary640.0

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

    if -0.06182422377168262 < (-.f64 (exp.f64 (-.f64 0 im)) (exp.f64 im)) < 0.09698978473685482

    1. Initial program 58.9

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

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\left(-\left(0.016666666666666666 \cdot {im}^{5} + \left(0.3333333333333333 \cdot {im}^{3} + \left(0.0003968253968253968 \cdot {im}^{7} + 2 \cdot im\right)\right)\right)\right)} \]
    3. Simplified0.1

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\left(-\mathsf{fma}\left(0.016666666666666666, {im}^{5}, \mathsf{fma}\left(0.3333333333333333, {im}^{3}, \mathsf{fma}\left(im, 2, 0.0003968253968253968 \cdot {im}^{7}\right)\right)\right)\right)} \]
    4. Applied expm1-log1p-u_binary640.1

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\left(0.5 \cdot \cos re\right) \cdot \left(-\mathsf{fma}\left(0.016666666666666666, {im}^{5}, \mathsf{fma}\left(0.3333333333333333, {im}^{3}, \mathsf{fma}\left(im, 2, 0.0003968253968253968 \cdot {im}^{7}\right)\right)\right)\right)\right)\right)} \]
    5. Simplified0.1

      \[\leadsto \mathsf{expm1}\left(\color{blue}{\mathsf{log1p}\left(-0.5 \cdot \left(\mathsf{fma}\left(0.016666666666666666, {im}^{5}, \mathsf{fma}\left(0.3333333333333333, {im}^{3}, \mathsf{fma}\left(im, 2, 0.0003968253968253968 \cdot {im}^{7}\right)\right)\right) \cdot \cos re\right)\right)}\right) \]

    if 0.09698978473685482 < (-.f64 (exp.f64 (-.f64 0 im)) (exp.f64 im))

    1. Initial program 0.0

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right) \]
    2. Applied add-cbrt-cube_binary640.0

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

      \[\leadsto \left(0.5 \cdot \sqrt[3]{\color{blue}{{\cos re}^{3}}}\right) \cdot \left(e^{0 - im} - e^{im}\right) \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{-im} - e^{im} \leq -0.06182422377168262:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(\sqrt{e^{-im}}, \sqrt{e^{-im}}, -e^{im}\right)\\ \mathbf{elif}\;e^{-im} - e^{im} \leq 0.09698978473685482:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(-0.5 \cdot \left(\cos re \cdot \mathsf{fma}\left(0.016666666666666666, {im}^{5}, \mathsf{fma}\left(0.3333333333333333, {im}^{3}, \mathsf{fma}\left(im, 2, 0.0003968253968253968 \cdot {im}^{7}\right)\right)\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(e^{-im} - e^{im}\right) \cdot \left(0.5 \cdot \sqrt[3]{{\cos re}^{3}}\right)\\ \end{array} \]

Reproduce

herbie shell --seed 2022088 
(FPCore (re im)
  :name "math.sin on complex, imaginary part"
  :precision binary64

  :herbie-target
  (if (< (fabs im) 1.0) (- (* (cos re) (+ (+ im (* (* (* 0.16666666666666666 im) im) im)) (* (* (* (* (* 0.008333333333333333 im) im) im) im) im)))) (* (* 0.5 (cos re)) (- (exp (- 0.0 im)) (exp im))))

  (* (* 0.5 (cos re)) (- (exp (- 0.0 im)) (exp im))))