Average Error: 43.3 → 0.8
Time: 31.6s
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 r157559 = 0.5;
        double r157560 = re;
        double r157561 = sin(r157560);
        double r157562 = r157559 * r157561;
        double r157563 = im;
        double r157564 = -r157563;
        double r157565 = exp(r157564);
        double r157566 = exp(r157563);
        double r157567 = r157565 - r157566;
        double r157568 = r157562 * r157567;
        return r157568;
}

double f(double re, double im) {
        double r157569 = im;
        double r157570 = 5.0;
        double r157571 = pow(r157569, r157570);
        double r157572 = re;
        double r157573 = sin(r157572);
        double r157574 = 0.008333333333333333;
        double r157575 = r157573 * r157574;
        double r157576 = 3.0;
        double r157577 = pow(r157569, r157576);
        double r157578 = 0.16666666666666666;
        double r157579 = r157577 * r157578;
        double r157580 = 1.0;
        double r157581 = r157569 * r157580;
        double r157582 = r157579 + r157581;
        double r157583 = r157573 * r157582;
        double r157584 = fma(r157571, r157575, r157583);
        double r157585 = -r157584;
        return r157585;
}

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))))