Average Error: 43.8 → 0.8
Time: 10.1s
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 r248213 = 0.5;
        double r248214 = re;
        double r248215 = sin(r248214);
        double r248216 = r248213 * r248215;
        double r248217 = im;
        double r248218 = -r248217;
        double r248219 = exp(r248218);
        double r248220 = exp(r248217);
        double r248221 = r248219 - r248220;
        double r248222 = r248216 * r248221;
        return r248222;
}

double f(double re, double im) {
        double r248223 = 0.5;
        double r248224 = re;
        double r248225 = sin(r248224);
        double r248226 = -0.3333333333333333;
        double r248227 = im;
        double r248228 = 3.0;
        double r248229 = pow(r248227, r248228);
        double r248230 = r248226 * r248229;
        double r248231 = r248225 * r248230;
        double r248232 = r248223 * r248231;
        double r248233 = r248223 * r248225;
        double r248234 = 0.016666666666666666;
        double r248235 = 5.0;
        double r248236 = pow(r248227, r248235);
        double r248237 = 2.0;
        double r248238 = r248237 * r248227;
        double r248239 = fma(r248234, r248236, r248238);
        double r248240 = -r248239;
        double r248241 = r248233 * r248240;
        double r248242 = r248232 + r248241;
        return r248242;
}

Error

Bits error versus re

Bits error versus im

Target

Original43.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 43.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 add-log-exp0.9

    \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \left(\left(-\color{blue}{\log \left(e^{\frac{1}{3} \cdot {im}^{3}}\right)}\right) - \mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right)\]
  6. Using strategy rm
  7. Applied sub-neg0.9

    \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \color{blue}{\left(\left(-\log \left(e^{\frac{1}{3} \cdot {im}^{3}}\right)\right) + \left(-\mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right)\right)}\]
  8. Applied distribute-lft-in0.9

    \[\leadsto \color{blue}{\left(0.5 \cdot \sin re\right) \cdot \left(-\log \left(e^{\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)}\]
  9. 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)\]
  10. 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 2020035 +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))))