Average Error: 43.4 → 0.8
Time: 33.2s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[-\mathsf{fma}\left(0.16666666666666666 \cdot \sin re, im \cdot \left(im \cdot im\right), \mathsf{fma}\left(0.008333333333333333 \cdot \sin re, {im}^{5}, im \cdot \left(1.0 \cdot \sin re\right)\right)\right)\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
-\mathsf{fma}\left(0.16666666666666666 \cdot \sin re, im \cdot \left(im \cdot im\right), \mathsf{fma}\left(0.008333333333333333 \cdot \sin re, {im}^{5}, im \cdot \left(1.0 \cdot \sin re\right)\right)\right)
double f(double re, double im) {
        double r8145978 = 0.5;
        double r8145979 = re;
        double r8145980 = sin(r8145979);
        double r8145981 = r8145978 * r8145980;
        double r8145982 = im;
        double r8145983 = -r8145982;
        double r8145984 = exp(r8145983);
        double r8145985 = exp(r8145982);
        double r8145986 = r8145984 - r8145985;
        double r8145987 = r8145981 * r8145986;
        return r8145987;
}

double f(double re, double im) {
        double r8145988 = 0.16666666666666666;
        double r8145989 = re;
        double r8145990 = sin(r8145989);
        double r8145991 = r8145988 * r8145990;
        double r8145992 = im;
        double r8145993 = r8145992 * r8145992;
        double r8145994 = r8145992 * r8145993;
        double r8145995 = 0.008333333333333333;
        double r8145996 = r8145995 * r8145990;
        double r8145997 = 5.0;
        double r8145998 = pow(r8145992, r8145997);
        double r8145999 = 1.0;
        double r8146000 = r8145999 * r8145990;
        double r8146001 = r8145992 * r8146000;
        double r8146002 = fma(r8145996, r8145998, r8146001);
        double r8146003 = fma(r8145991, r8145994, r8146002);
        double r8146004 = -r8146003;
        return r8146004;
}

Error

Bits error versus re

Bits error versus im

Target

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

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

    \[\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.9

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

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

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

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

Reproduce

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

  :herbie-target
  (if (< (fabs im) 1) (- (* (sin re) (+ (+ im (* (* (* 1/6 im) im) im)) (* (* (* (* (* 1/120 im) im) im) im) im)))) (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))

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