Average Error: 43.5 → 0.8
Time: 29.8s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[-\mathsf{fma}\left(im \cdot \left(im \cdot im\right), \sin re \cdot 0.16666666666666666, \sin re \cdot \left(1.0 \cdot im + {im}^{5} \cdot 0.008333333333333333\right)\right)\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
-\mathsf{fma}\left(im \cdot \left(im \cdot im\right), \sin re \cdot 0.16666666666666666, \sin re \cdot \left(1.0 \cdot im + {im}^{5} \cdot 0.008333333333333333\right)\right)
double f(double re, double im) {
        double r7860710 = 0.5;
        double r7860711 = re;
        double r7860712 = sin(r7860711);
        double r7860713 = r7860710 * r7860712;
        double r7860714 = im;
        double r7860715 = -r7860714;
        double r7860716 = exp(r7860715);
        double r7860717 = exp(r7860714);
        double r7860718 = r7860716 - r7860717;
        double r7860719 = r7860713 * r7860718;
        return r7860719;
}

double f(double re, double im) {
        double r7860720 = im;
        double r7860721 = r7860720 * r7860720;
        double r7860722 = r7860720 * r7860721;
        double r7860723 = re;
        double r7860724 = sin(r7860723);
        double r7860725 = 0.16666666666666666;
        double r7860726 = r7860724 * r7860725;
        double r7860727 = 1.0;
        double r7860728 = r7860727 * r7860720;
        double r7860729 = 5.0;
        double r7860730 = pow(r7860720, r7860729);
        double r7860731 = 0.008333333333333333;
        double r7860732 = r7860730 * r7860731;
        double r7860733 = r7860728 + r7860732;
        double r7860734 = r7860724 * r7860733;
        double r7860735 = fma(r7860722, r7860726, r7860734);
        double r7860736 = -r7860735;
        return r7860736;
}

Error

Bits error versus re

Bits error versus im

Target

Original43.5
Target0.2
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.5

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

    \[\leadsto \color{blue}{-\left(0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right) + \left(1.0 \cdot \left(\sin re \cdot im\right) + 0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right)\right)\right)}\]
  5. Simplified0.8

    \[\leadsto \color{blue}{-\mathsf{fma}\left(\left(im \cdot im\right) \cdot im, \sin re \cdot 0.16666666666666666, \sin re \cdot \left(0.008333333333333333 \cdot {im}^{5} + 1.0 \cdot im\right)\right)}\]
  6. Final simplification0.8

    \[\leadsto -\mathsf{fma}\left(im \cdot \left(im \cdot im\right), \sin re \cdot 0.16666666666666666, \sin re \cdot \left(1.0 \cdot im + {im}^{5} \cdot 0.008333333333333333\right)\right)\]

Reproduce

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