Average Error: 43.7 → 0.8
Time: 30.1s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[-\mathsf{fma}\left({im}^{5}, \sin re \cdot 0.008333333333333333, \mathsf{fma}\left(\sin re \cdot im, 1.0, \sin re \cdot \left(\left(\left(im \cdot im\right) \cdot im\right) \cdot 0.16666666666666666\right)\right)\right)\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
-\mathsf{fma}\left({im}^{5}, \sin re \cdot 0.008333333333333333, \mathsf{fma}\left(\sin re \cdot im, 1.0, \sin re \cdot \left(\left(\left(im \cdot im\right) \cdot im\right) \cdot 0.16666666666666666\right)\right)\right)
double f(double re, double im) {
        double r3653147 = 0.5;
        double r3653148 = re;
        double r3653149 = sin(r3653148);
        double r3653150 = r3653147 * r3653149;
        double r3653151 = im;
        double r3653152 = -r3653151;
        double r3653153 = exp(r3653152);
        double r3653154 = exp(r3653151);
        double r3653155 = r3653153 - r3653154;
        double r3653156 = r3653150 * r3653155;
        return r3653156;
}

double f(double re, double im) {
        double r3653157 = im;
        double r3653158 = 5.0;
        double r3653159 = pow(r3653157, r3653158);
        double r3653160 = re;
        double r3653161 = sin(r3653160);
        double r3653162 = 0.008333333333333333;
        double r3653163 = r3653161 * r3653162;
        double r3653164 = r3653161 * r3653157;
        double r3653165 = 1.0;
        double r3653166 = r3653157 * r3653157;
        double r3653167 = r3653166 * r3653157;
        double r3653168 = 0.16666666666666666;
        double r3653169 = r3653167 * r3653168;
        double r3653170 = r3653161 * r3653169;
        double r3653171 = fma(r3653164, r3653165, r3653170);
        double r3653172 = fma(r3653159, r3653163, r3653171);
        double r3653173 = -r3653172;
        return r3653173;
}

Error

Bits error versus re

Bits error versus im

Target

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

    \[\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(im \cdot \left(im \cdot im\right)\right) \cdot \frac{-1}{3} - \mathsf{fma}\left(\frac{1}{60}, {im}^{5}, im + im\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({im}^{5}, \sin re \cdot 0.008333333333333333, \mathsf{fma}\left(im \cdot \sin re, 1.0, \sin re \cdot \left(\left(im \cdot \left(im \cdot im\right)\right) \cdot 0.16666666666666666\right)\right)\right)}\]
  6. Final simplification0.8

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

Reproduce

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