Average Error: 29.3 → 0.2
Time: 9.1s
Precision: binary64
\[\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right) \]
\[\begin{array}{l} \mathbf{if}\;\begin{array}{l} t_0 := e^{-im} - e^{im}\\ t_0 \leq -\infty \lor \neg \left(t_0 \leq 0.008696040391028559\right) \end{array}:\\ \;\;\;\;\cos re \cdot \mathsf{fma}\left(e^{im}, -0.5, \frac{0.5}{e^{im}}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\left(\cos re \cdot {im}^{3}\right) \cdot -0.16666666666666666 + \left(\cos re \cdot {im}^{5}\right) \cdot -0.008333333333333333\right) - im \cdot \cos re\\ \end{array} \]
\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right)
\begin{array}{l}
\mathbf{if}\;\begin{array}{l}
t_0 := e^{-im} - e^{im}\\
t_0 \leq -\infty \lor \neg \left(t_0 \leq 0.008696040391028559\right)
\end{array}:\\
\;\;\;\;\cos re \cdot \mathsf{fma}\left(e^{im}, -0.5, \frac{0.5}{e^{im}}\right)\\

\mathbf{else}:\\
\;\;\;\;\left(\left(\cos re \cdot {im}^{3}\right) \cdot -0.16666666666666666 + \left(\cos re \cdot {im}^{5}\right) \cdot -0.008333333333333333\right) - im \cdot \cos re\\


\end{array}
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (- (exp (- 0.0 im)) (exp im))))
(FPCore (re im)
 :precision binary64
 (if (let* ((t_0 (- (exp (- im)) (exp im))))
       (or (<= t_0 (- INFINITY)) (not (<= t_0 0.008696040391028559))))
   (* (cos re) (fma (exp im) -0.5 (/ 0.5 (exp im))))
   (-
    (+
     (* (* (cos re) (pow im 3.0)) -0.16666666666666666)
     (* (* (cos re) (pow im 5.0)) -0.008333333333333333))
    (* im (cos re)))))
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) - exp(im);
	double tmp;
	if ((t_0 <= -((double) INFINITY)) || !(t_0 <= 0.008696040391028559)) {
		tmp = cos(re) * fma(exp(im), -0.5, (0.5 / exp(im)));
	} else {
		tmp = (((cos(re) * pow(im, 3.0)) * -0.16666666666666666) + ((cos(re) * pow(im, 5.0)) * -0.008333333333333333)) - (im * cos(re));
	}
	return tmp;
}

Error

Bits error versus re

Bits error versus im

Target

Original29.3
Target0.1
Herbie0.2
\[\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 2 regimes
  2. if (-.f64 (exp.f64 (-.f64 0 im)) (exp.f64 im)) < -inf.0 or 0.008696040391028559 < (-.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. Simplified0.0

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

    if -inf.0 < (-.f64 (exp.f64 (-.f64 0 im)) (exp.f64 im)) < 0.008696040391028559

    1. Initial program 58.3

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right) \]
    2. Simplified58.3

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

      \[\leadsto \color{blue}{-\left(\cos re \cdot im + \left(0.16666666666666666 \cdot \left(\cos re \cdot {im}^{3}\right) + 0.008333333333333333 \cdot \left(\cos re \cdot {im}^{5}\right)\right)\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{-im} - e^{im} \leq -\infty \lor \neg \left(e^{-im} - e^{im} \leq 0.008696040391028559\right):\\ \;\;\;\;\cos re \cdot \mathsf{fma}\left(e^{im}, -0.5, \frac{0.5}{e^{im}}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\left(\cos re \cdot {im}^{3}\right) \cdot -0.16666666666666666 + \left(\cos re \cdot {im}^{5}\right) \cdot -0.008333333333333333\right) - im \cdot \cos re\\ \end{array} \]

Reproduce

herbie shell --seed 2022068 
(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))))