Average Error: 43.5 → 0.7
Time: 31.3s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[\mathsf{fma}\left({im}^{5}, \frac{1}{60}, \mathsf{fma}\left(\frac{1}{3}, im \cdot im, 2\right) \cdot im\right) \cdot \left(\left(-0.5\right) \cdot \sin re\right)\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\mathsf{fma}\left({im}^{5}, \frac{1}{60}, \mathsf{fma}\left(\frac{1}{3}, im \cdot im, 2\right) \cdot im\right) \cdot \left(\left(-0.5\right) \cdot \sin re\right)
double f(double re, double im) {
        double r8804535 = 0.5;
        double r8804536 = re;
        double r8804537 = sin(r8804536);
        double r8804538 = r8804535 * r8804537;
        double r8804539 = im;
        double r8804540 = -r8804539;
        double r8804541 = exp(r8804540);
        double r8804542 = exp(r8804539);
        double r8804543 = r8804541 - r8804542;
        double r8804544 = r8804538 * r8804543;
        return r8804544;
}

double f(double re, double im) {
        double r8804545 = im;
        double r8804546 = 5.0;
        double r8804547 = pow(r8804545, r8804546);
        double r8804548 = 0.016666666666666666;
        double r8804549 = 0.3333333333333333;
        double r8804550 = r8804545 * r8804545;
        double r8804551 = 2.0;
        double r8804552 = fma(r8804549, r8804550, r8804551);
        double r8804553 = r8804552 * r8804545;
        double r8804554 = fma(r8804547, r8804548, r8804553);
        double r8804555 = 0.5;
        double r8804556 = -r8804555;
        double r8804557 = re;
        double r8804558 = sin(r8804557);
        double r8804559 = r8804556 * r8804558;
        double r8804560 = r8804554 * r8804559;
        return r8804560;
}

Error

Bits error versus re

Bits error versus im

Target

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

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

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

    \[\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. Final simplification0.7

    \[\leadsto \mathsf{fma}\left({im}^{5}, \frac{1}{60}, \mathsf{fma}\left(\frac{1}{3}, im \cdot im, 2\right) \cdot im\right) \cdot \left(\left(-0.5\right) \cdot \sin re\right)\]

Reproduce

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