Average Error: 42.8 → 0.8
Time: 9.9s
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 r304723 = 0.5;
        double r304724 = re;
        double r304725 = sin(r304724);
        double r304726 = r304723 * r304725;
        double r304727 = im;
        double r304728 = -r304727;
        double r304729 = exp(r304728);
        double r304730 = exp(r304727);
        double r304731 = r304729 - r304730;
        double r304732 = r304726 * r304731;
        return r304732;
}

double f(double re, double im) {
        double r304733 = 0.5;
        double r304734 = re;
        double r304735 = sin(r304734);
        double r304736 = -0.3333333333333333;
        double r304737 = im;
        double r304738 = 3.0;
        double r304739 = pow(r304737, r304738);
        double r304740 = r304736 * r304739;
        double r304741 = r304735 * r304740;
        double r304742 = r304733 * r304741;
        double r304743 = r304733 * r304735;
        double r304744 = 0.016666666666666666;
        double r304745 = 5.0;
        double r304746 = pow(r304737, r304745);
        double r304747 = 2.0;
        double r304748 = r304747 * r304737;
        double r304749 = fma(r304744, r304746, r304748);
        double r304750 = -r304749;
        double r304751 = r304743 * r304750;
        double r304752 = r304742 + r304751;
        return r304752;
}

Error

Bits error versus re

Bits error versus im

Target

Original42.8
Target0.3
Herbie0.8
\[\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 42.8

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