Average Error: 43.5 → 0.8
Time: 10.7s
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 r397427 = 0.5;
        double r397428 = re;
        double r397429 = sin(r397428);
        double r397430 = r397427 * r397429;
        double r397431 = im;
        double r397432 = -r397431;
        double r397433 = exp(r397432);
        double r397434 = exp(r397431);
        double r397435 = r397433 - r397434;
        double r397436 = r397430 * r397435;
        return r397436;
}

double f(double re, double im) {
        double r397437 = 0.5;
        double r397438 = re;
        double r397439 = sin(r397438);
        double r397440 = -0.3333333333333333;
        double r397441 = im;
        double r397442 = 3.0;
        double r397443 = pow(r397441, r397442);
        double r397444 = r397440 * r397443;
        double r397445 = r397439 * r397444;
        double r397446 = r397437 * r397445;
        double r397447 = r397437 * r397439;
        double r397448 = 0.016666666666666666;
        double r397449 = 5.0;
        double r397450 = pow(r397441, r397449);
        double r397451 = 2.0;
        double r397452 = r397451 * r397441;
        double r397453 = fma(r397448, r397450, r397452);
        double r397454 = -r397453;
        double r397455 = r397447 * r397454;
        double r397456 = r397446 + r397455;
        return r397456;
}

Error

Bits error versus re

Bits error versus im

Target

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

    \[\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.8

    \[\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.8

    \[\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.8

    \[\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 2019354 +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))))