Average Error: 43.7 → 0.7
Time: 9.8s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[\left(0.5 \cdot \sin re\right) \cdot \mathsf{fma}\left(im, -2, -\mathsf{fma}\left({im}^{3}, \frac{1}{3}, \frac{1}{60} \cdot {im}^{5}\right)\right)\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\left(0.5 \cdot \sin re\right) \cdot \mathsf{fma}\left(im, -2, -\mathsf{fma}\left({im}^{3}, \frac{1}{3}, \frac{1}{60} \cdot {im}^{5}\right)\right)
double f(double re, double im) {
        double r290157 = 0.5;
        double r290158 = re;
        double r290159 = sin(r290158);
        double r290160 = r290157 * r290159;
        double r290161 = im;
        double r290162 = -r290161;
        double r290163 = exp(r290162);
        double r290164 = exp(r290161);
        double r290165 = r290163 - r290164;
        double r290166 = r290160 * r290165;
        return r290166;
}

double f(double re, double im) {
        double r290167 = 0.5;
        double r290168 = re;
        double r290169 = sin(r290168);
        double r290170 = r290167 * r290169;
        double r290171 = im;
        double r290172 = -2.0;
        double r290173 = 3.0;
        double r290174 = pow(r290171, r290173);
        double r290175 = 0.3333333333333333;
        double r290176 = 0.016666666666666666;
        double r290177 = 5.0;
        double r290178 = pow(r290171, r290177);
        double r290179 = r290176 * r290178;
        double r290180 = fma(r290174, r290175, r290179);
        double r290181 = -r290180;
        double r290182 = fma(r290171, r290172, r290181);
        double r290183 = r290170 * r290182;
        return r290183;
}

Error

Bits error versus re

Bits error versus im

Target

Original43.7
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.7

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

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

Reproduce

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