Average Error: 43.3 → 0.7
Time: 13.0s
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(1 \cdot im + 0.00833333333333333322 \cdot {im}^{5}\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(1 \cdot im + 0.00833333333333333322 \cdot {im}^{5}\right)\right)
double f(double re, double im) {
        double r252085 = 0.5;
        double r252086 = re;
        double r252087 = sin(r252086);
        double r252088 = r252085 * r252087;
        double r252089 = im;
        double r252090 = -r252089;
        double r252091 = exp(r252090);
        double r252092 = exp(r252089);
        double r252093 = r252091 - r252092;
        double r252094 = r252088 * r252093;
        return r252094;
}

double f(double re, double im) {
        double r252095 = 0.16666666666666666;
        double r252096 = re;
        double r252097 = sin(r252096);
        double r252098 = r252095 * r252097;
        double r252099 = im;
        double r252100 = 3.0;
        double r252101 = pow(r252099, r252100);
        double r252102 = 1.0;
        double r252103 = r252102 * r252099;
        double r252104 = 0.008333333333333333;
        double r252105 = 5.0;
        double r252106 = pow(r252099, r252105);
        double r252107 = r252104 * r252106;
        double r252108 = r252103 + r252107;
        double r252109 = r252097 * r252108;
        double r252110 = fma(r252098, r252101, r252109);
        double r252111 = -r252110;
        return r252111;
}

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(1 \cdot im + 0.00833333333333333322 \cdot {im}^{5}\right)\right)}\]
  6. Final simplification0.7

    \[\leadsto -\mathsf{fma}\left(0.166666666666666657 \cdot \sin re, {im}^{3}, \sin re \cdot \left(1 \cdot im + 0.00833333333333333322 \cdot {im}^{5}\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))))