Average Error: 43.5 → 0.8
Time: 13.3s
Precision: binary64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
\[-\mathsf{fma}\left(im, \sin re, \sin re \cdot \left(0.16666666666666666 \cdot {im}^{3} + 0.008333333333333333 \cdot {im}^{5}\right)\right) \]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
-\mathsf{fma}\left(im, \sin re, \sin re \cdot \left(0.16666666666666666 \cdot {im}^{3} + 0.008333333333333333 \cdot {im}^{5}\right)\right)
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))
(FPCore (re im)
 :precision binary64
 (-
  (fma
   im
   (sin re)
   (*
    (sin re)
    (+
     (* 0.16666666666666666 (pow im 3.0))
     (* 0.008333333333333333 (pow im 5.0)))))))
double code(double re, double im) {
	return (0.5 * sin(re)) * (exp(-im) - exp(im));
}
double code(double re, double im) {
	return -fma(im, sin(re), (sin(re) * ((0.16666666666666666 * pow(im, 3.0)) + (0.008333333333333333 * pow(im, 5.0)))));
}

Error

Bits error versus re

Bits error versus im

Target

Original43.5
Target0.3
Herbie0.8
\[\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} \]

Derivation

  1. Initial program 43.5

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

    \[\leadsto \color{blue}{-\left(\sin re \cdot im + \left(0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right) + 0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right)\right)\right)} \]
  3. Simplified0.8

    \[\leadsto \color{blue}{-\mathsf{fma}\left(im, \sin re, \sin re \cdot \left(0.16666666666666666 \cdot {im}^{3} + 0.008333333333333333 \cdot {im}^{5}\right)\right)} \]
  4. Final simplification0.8

    \[\leadsto -\mathsf{fma}\left(im, \sin re, \sin re \cdot \left(0.16666666666666666 \cdot {im}^{3} + 0.008333333333333333 \cdot {im}^{5}\right)\right) \]

Reproduce

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

  :herbie-target
  (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))))

  (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))