Average Error: 43.7 → 0.9
Time: 58.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 r5814563 = 0.5;
        double r5814564 = re;
        double r5814565 = sin(r5814564);
        double r5814566 = r5814563 * r5814565;
        double r5814567 = im;
        double r5814568 = -r5814567;
        double r5814569 = exp(r5814568);
        double r5814570 = exp(r5814567);
        double r5814571 = r5814569 - r5814570;
        double r5814572 = r5814566 * r5814571;
        return r5814572;
}

double f(double re, double im) {
        double r5814573 = im;
        double r5814574 = 5.0;
        double r5814575 = pow(r5814573, r5814574);
        double r5814576 = 0.016666666666666666;
        double r5814577 = 0.3333333333333333;
        double r5814578 = r5814573 * r5814573;
        double r5814579 = 2.0;
        double r5814580 = fma(r5814577, r5814578, r5814579);
        double r5814581 = r5814580 * r5814573;
        double r5814582 = fma(r5814575, r5814576, r5814581);
        double r5814583 = 0.5;
        double r5814584 = -r5814583;
        double r5814585 = re;
        double r5814586 = sin(r5814585);
        double r5814587 = r5814584 * r5814586;
        double r5814588 = r5814582 * r5814587;
        return r5814588;
}

Error

Bits error versus re

Bits error versus im

Target

Original43.7
Target0.3
Herbie0.9
\[\begin{array}{l} \mathbf{if}\;\left|im\right| \lt 1.0:\\ \;\;\;\;-\sin re \cdot \left(\left(im + \left(\left(0.16666666666666666 \cdot im\right) \cdot im\right) \cdot im\right) + \left(\left(\left(\left(0.008333333333333333 \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.7

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

    \[\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 2019165 +o rules:numerics
(FPCore (re im)
  :name "math.cos on complex, imaginary part"

  :herbie-target
  (if (< (fabs im) 1.0) (- (* (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))))