Average Error: 43.3 → 0.8
Time: 32.5s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[\left(\sin re \cdot \left(-0.5\right)\right) \cdot \mathsf{fma}\left(\frac{1}{60}, \left({im}^{5}\right), \left(\left(2 + \frac{1}{3} \cdot \left(im \cdot im\right)\right) \cdot im\right)\right)\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\left(\sin re \cdot \left(-0.5\right)\right) \cdot \mathsf{fma}\left(\frac{1}{60}, \left({im}^{5}\right), \left(\left(2 + \frac{1}{3} \cdot \left(im \cdot im\right)\right) \cdot im\right)\right)
double f(double re, double im) {
        double r2833679 = 0.5;
        double r2833680 = re;
        double r2833681 = sin(r2833680);
        double r2833682 = r2833679 * r2833681;
        double r2833683 = im;
        double r2833684 = -r2833683;
        double r2833685 = exp(r2833684);
        double r2833686 = exp(r2833683);
        double r2833687 = r2833685 - r2833686;
        double r2833688 = r2833682 * r2833687;
        return r2833688;
}

double f(double re, double im) {
        double r2833689 = re;
        double r2833690 = sin(r2833689);
        double r2833691 = 0.5;
        double r2833692 = -r2833691;
        double r2833693 = r2833690 * r2833692;
        double r2833694 = 0.016666666666666666;
        double r2833695 = im;
        double r2833696 = 5.0;
        double r2833697 = pow(r2833695, r2833696);
        double r2833698 = 2.0;
        double r2833699 = 0.3333333333333333;
        double r2833700 = r2833695 * r2833695;
        double r2833701 = r2833699 * r2833700;
        double r2833702 = r2833698 + r2833701;
        double r2833703 = r2833702 * r2833695;
        double r2833704 = fma(r2833694, r2833697, r2833703);
        double r2833705 = r2833693 * r2833704;
        return r2833705;
}

Error

Bits error versus re

Bits error versus im

Target

Original43.3
Target0.3
Herbie0.8
\[\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.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}{\left(-\mathsf{fma}\left(\frac{1}{60}, \left({im}^{5}\right), \left(im \cdot \left(2 + \frac{1}{3} \cdot \left(im \cdot im\right)\right)\right)\right)\right)}\]
  4. Final simplification0.8

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

Reproduce

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