Average Error: 43.2 → 0.9
Time: 9.3s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[0.5 \cdot \left(\sin re \cdot \left(\frac{-1}{3} \cdot {im}^{3}\right)\right) + \left(0.5 \cdot \sin re\right) \cdot \left(-\mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right)\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
0.5 \cdot \left(\sin re \cdot \left(\frac{-1}{3} \cdot {im}^{3}\right)\right) + \left(0.5 \cdot \sin re\right) \cdot \left(-\mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right)
double f(double re, double im) {
        double r322659 = 0.5;
        double r322660 = re;
        double r322661 = sin(r322660);
        double r322662 = r322659 * r322661;
        double r322663 = im;
        double r322664 = -r322663;
        double r322665 = exp(r322664);
        double r322666 = exp(r322663);
        double r322667 = r322665 - r322666;
        double r322668 = r322662 * r322667;
        return r322668;
}

double f(double re, double im) {
        double r322669 = 0.5;
        double r322670 = re;
        double r322671 = sin(r322670);
        double r322672 = -0.3333333333333333;
        double r322673 = im;
        double r322674 = 3.0;
        double r322675 = pow(r322673, r322674);
        double r322676 = r322672 * r322675;
        double r322677 = r322671 * r322676;
        double r322678 = r322669 * r322677;
        double r322679 = r322669 * r322671;
        double r322680 = 0.016666666666666666;
        double r322681 = 5.0;
        double r322682 = pow(r322673, r322681);
        double r322683 = 2.0;
        double r322684 = r322683 * r322673;
        double r322685 = fma(r322680, r322682, r322684);
        double r322686 = -r322685;
        double r322687 = r322679 * r322686;
        double r322688 = r322678 + r322687;
        return r322688;
}

Error

Bits error versus re

Bits error versus im

Target

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

    \[\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(\left(-\frac{1}{3} \cdot {im}^{3}\right) - \mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right)}\]
  4. Using strategy rm
  5. Applied sub-neg0.9

    \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \color{blue}{\left(\left(-\frac{1}{3} \cdot {im}^{3}\right) + \left(-\mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right)\right)}\]
  6. Applied distribute-lft-in0.9

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

    \[\leadsto \color{blue}{0.5 \cdot \left(\sin re \cdot \left(\frac{-1}{3} \cdot {im}^{3}\right)\right)} + \left(0.5 \cdot \sin re\right) \cdot \left(-\mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right)\]
  8. Final simplification0.9

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

Reproduce

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