Average Error: 43.7 → 0.8
Time: 8.1s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[\left(-0.166666666666666657 \cdot \left(\sin re \cdot {im}^{3}\right)\right) - \mathsf{fma}\left(1, \sin re \cdot im, 0.00833333333333333322 \cdot \left(\sin re \cdot {im}^{5}\right)\right)\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\left(-0.166666666666666657 \cdot \left(\sin re \cdot {im}^{3}\right)\right) - \mathsf{fma}\left(1, \sin re \cdot im, 0.00833333333333333322 \cdot \left(\sin re \cdot {im}^{5}\right)\right)
double f(double re, double im) {
        double r238783 = 0.5;
        double r238784 = re;
        double r238785 = sin(r238784);
        double r238786 = r238783 * r238785;
        double r238787 = im;
        double r238788 = -r238787;
        double r238789 = exp(r238788);
        double r238790 = exp(r238787);
        double r238791 = r238789 - r238790;
        double r238792 = r238786 * r238791;
        return r238792;
}

double f(double re, double im) {
        double r238793 = 0.16666666666666666;
        double r238794 = re;
        double r238795 = sin(r238794);
        double r238796 = im;
        double r238797 = 3.0;
        double r238798 = pow(r238796, r238797);
        double r238799 = r238795 * r238798;
        double r238800 = r238793 * r238799;
        double r238801 = -r238800;
        double r238802 = 1.0;
        double r238803 = r238795 * r238796;
        double r238804 = 0.008333333333333333;
        double r238805 = 5.0;
        double r238806 = pow(r238796, r238805);
        double r238807 = r238795 * r238806;
        double r238808 = r238804 * r238807;
        double r238809 = fma(r238802, r238803, r238808);
        double r238810 = r238801 - r238809;
        return r238810;
}

Error

Bits error versus re

Bits error versus im

Target

Original43.7
Target0.3
Herbie0.8
\[\begin{array}{l} \mathbf{if}\;\left|im\right| \lt 1:\\ \;\;\;\;-\sin re \cdot \left(\left(im + \left(\left(0.166666666666666657 \cdot im\right) \cdot im\right) \cdot im\right) + \left(\left(\left(\left(0.00833333333333333322 \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.7

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

    \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \color{blue}{\left(-\left(\frac{1}{3} \cdot {im}^{3} + \left(\frac{1}{60} \cdot {im}^{5} + 2 \cdot im\right)\right)\right)}\]
  3. Simplified0.8

    \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \color{blue}{\left(\left(-\frac{1}{3} \cdot {im}^{3}\right) - \mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right)}\]
  4. Taylor expanded around inf 0.8

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

    \[\leadsto \color{blue}{\left(-0.166666666666666657 \cdot \left(\sin re \cdot {im}^{3}\right)\right) - \mathsf{fma}\left(1, \sin re \cdot im, 0.00833333333333333322 \cdot \left(\sin re \cdot {im}^{5}\right)\right)}\]
  6. Final simplification0.8

    \[\leadsto \left(-0.166666666666666657 \cdot \left(\sin re \cdot {im}^{3}\right)\right) - \mathsf{fma}\left(1, \sin re \cdot im, 0.00833333333333333322 \cdot \left(\sin re \cdot {im}^{5}\right)\right)\]

Reproduce

herbie shell --seed 2020021 +o rules:numerics
(FPCore (re im)
  :name "math.cos on complex, imaginary part"
  :precision binary64

  :herbie-target
  (if (< (fabs im) 1) (- (* (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))))