Average Error: 43.2 → 0.9
Time: 11.2s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[\left(0.5 \cdot \sin re\right) \cdot \left(\left(-\frac{1}{3} \cdot {im}^{3}\right) - 1 \cdot \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)
\left(0.5 \cdot \sin re\right) \cdot \left(\left(-\frac{1}{3} \cdot {im}^{3}\right) - 1 \cdot \mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right)
double f(double re, double im) {
        double r249237 = 0.5;
        double r249238 = re;
        double r249239 = sin(r249238);
        double r249240 = r249237 * r249239;
        double r249241 = im;
        double r249242 = -r249241;
        double r249243 = exp(r249242);
        double r249244 = exp(r249241);
        double r249245 = r249243 - r249244;
        double r249246 = r249240 * r249245;
        return r249246;
}

double f(double re, double im) {
        double r249247 = 0.5;
        double r249248 = re;
        double r249249 = sin(r249248);
        double r249250 = r249247 * r249249;
        double r249251 = 0.3333333333333333;
        double r249252 = im;
        double r249253 = 3.0;
        double r249254 = pow(r249252, r249253);
        double r249255 = r249251 * r249254;
        double r249256 = -r249255;
        double r249257 = 1.0;
        double r249258 = 0.016666666666666666;
        double r249259 = 5.0;
        double r249260 = pow(r249252, r249259);
        double r249261 = 2.0;
        double r249262 = r249261 * r249252;
        double r249263 = fma(r249258, r249260, r249262);
        double r249264 = r249257 * r249263;
        double r249265 = r249256 - r249264;
        double r249266 = r249250 * r249265;
        return r249266;
}

Error

Bits error versus re

Bits error versus im

Target

Original43.2
Target0.3
Herbie0.9
\[\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.2

    \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
  2. Taylor expanded around 0 0.9

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

    \[\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 *-un-lft-identity0.9

    \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \left(\left(-\frac{1}{3} \cdot {im}^{3}\right) - \color{blue}{1 \cdot \mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)}\right)\]
  6. Final simplification0.9

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

Reproduce

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