Average Error: 43.3 → 0.7
Time: 23.1s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[-\mathsf{fma}\left(0.166666666666666657 \cdot \sin re, {im}^{3}, \sin re \cdot \left({im}^{5} \cdot 0.00833333333333333322 + im \cdot 1\right)\right)\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
-\mathsf{fma}\left(0.166666666666666657 \cdot \sin re, {im}^{3}, \sin re \cdot \left({im}^{5} \cdot 0.00833333333333333322 + im \cdot 1\right)\right)
double f(double re, double im) {
        double r239613 = 0.5;
        double r239614 = re;
        double r239615 = sin(r239614);
        double r239616 = r239613 * r239615;
        double r239617 = im;
        double r239618 = -r239617;
        double r239619 = exp(r239618);
        double r239620 = exp(r239617);
        double r239621 = r239619 - r239620;
        double r239622 = r239616 * r239621;
        return r239622;
}

double f(double re, double im) {
        double r239623 = 0.16666666666666666;
        double r239624 = re;
        double r239625 = sin(r239624);
        double r239626 = r239623 * r239625;
        double r239627 = im;
        double r239628 = 3.0;
        double r239629 = pow(r239627, r239628);
        double r239630 = 5.0;
        double r239631 = pow(r239627, r239630);
        double r239632 = 0.008333333333333333;
        double r239633 = r239631 * r239632;
        double r239634 = 1.0;
        double r239635 = r239627 * r239634;
        double r239636 = r239633 + r239635;
        double r239637 = r239625 * r239636;
        double r239638 = fma(r239626, r239629, r239637);
        double r239639 = -r239638;
        return r239639;
}

Error

Bits error versus re

Bits error versus im

Target

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

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

    \[\leadsto \color{blue}{-\left(0.166666666666666657 \cdot \left(\sin re \cdot {im}^{3}\right) + \left(1 \cdot \left(\sin re \cdot im\right) + 0.00833333333333333322 \cdot \left(\sin re \cdot {im}^{5}\right)\right)\right)}\]
  5. Simplified0.7

    \[\leadsto \color{blue}{-\mathsf{fma}\left(0.166666666666666657 \cdot \sin re, {im}^{3}, \sin re \cdot \left({im}^{5} \cdot 0.00833333333333333322 + im \cdot 1\right)\right)}\]
  6. Final simplification0.7

    \[\leadsto -\mathsf{fma}\left(0.166666666666666657 \cdot \sin re, {im}^{3}, \sin re \cdot \left({im}^{5} \cdot 0.00833333333333333322 + im \cdot 1\right)\right)\]

Reproduce

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