Average Error: 43.8 → 0.9
Time: 29.0s
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.008333333333333333, \sin re \cdot \left(\left(im \cdot \left(im \cdot im\right)\right) \cdot 0.16666666666666666 + 1.0 \cdot im\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.008333333333333333, \sin re \cdot \left(\left(im \cdot \left(im \cdot im\right)\right) \cdot 0.16666666666666666 + 1.0 \cdot im\right)\right)
double f(double re, double im) {
        double r10295641 = 0.5;
        double r10295642 = re;
        double r10295643 = sin(r10295642);
        double r10295644 = r10295641 * r10295643;
        double r10295645 = im;
        double r10295646 = -r10295645;
        double r10295647 = exp(r10295646);
        double r10295648 = exp(r10295645);
        double r10295649 = r10295647 - r10295648;
        double r10295650 = r10295644 * r10295649;
        return r10295650;
}

double f(double re, double im) {
        double r10295651 = im;
        double r10295652 = 5.0;
        double r10295653 = pow(r10295651, r10295652);
        double r10295654 = re;
        double r10295655 = sin(r10295654);
        double r10295656 = 0.008333333333333333;
        double r10295657 = r10295655 * r10295656;
        double r10295658 = r10295651 * r10295651;
        double r10295659 = r10295651 * r10295658;
        double r10295660 = 0.16666666666666666;
        double r10295661 = r10295659 * r10295660;
        double r10295662 = 1.0;
        double r10295663 = r10295662 * r10295651;
        double r10295664 = r10295661 + r10295663;
        double r10295665 = r10295655 * r10295664;
        double r10295666 = fma(r10295653, r10295657, r10295665);
        double r10295667 = -r10295666;
        return r10295667;
}

Error

Bits error versus re

Bits error versus im

Target

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

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

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

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

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

Reproduce

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