Average Error: 43.3 → 0.8
Time: 29.9s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[-\mathsf{fma}\left({im}^{5}, \sin re \cdot 0.008333333333333333217685101601546193705872, \sin re \cdot \left({im}^{3} \cdot 0.1666666666666666574148081281236954964697 + im \cdot 1\right)\right)\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
-\mathsf{fma}\left({im}^{5}, \sin re \cdot 0.008333333333333333217685101601546193705872, \sin re \cdot \left({im}^{3} \cdot 0.1666666666666666574148081281236954964697 + im \cdot 1\right)\right)
double f(double re, double im) {
        double r186786 = 0.5;
        double r186787 = re;
        double r186788 = sin(r186787);
        double r186789 = r186786 * r186788;
        double r186790 = im;
        double r186791 = -r186790;
        double r186792 = exp(r186791);
        double r186793 = exp(r186790);
        double r186794 = r186792 - r186793;
        double r186795 = r186789 * r186794;
        return r186795;
}

double f(double re, double im) {
        double r186796 = im;
        double r186797 = 5.0;
        double r186798 = pow(r186796, r186797);
        double r186799 = re;
        double r186800 = sin(r186799);
        double r186801 = 0.008333333333333333;
        double r186802 = r186800 * r186801;
        double r186803 = 3.0;
        double r186804 = pow(r186796, r186803);
        double r186805 = 0.16666666666666666;
        double r186806 = r186804 * r186805;
        double r186807 = 1.0;
        double r186808 = r186796 * r186807;
        double r186809 = r186806 + r186808;
        double r186810 = r186800 * r186809;
        double r186811 = fma(r186798, r186802, r186810);
        double r186812 = -r186811;
        return r186812;
}

Error

Bits error versus re

Bits error versus im

Target

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

    \[\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}{\mathsf{fma}\left(\frac{-1}{3}, {im}^{3}, \mathsf{fma}\left(im, -2, {im}^{5} \cdot \frac{-1}{60}\right)\right)}\]
  4. Taylor expanded around inf 0.8

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

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

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

Reproduce

herbie shell --seed 2019325 +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))))