Average Error: 44.1 → 0.8
Time: 20.3s
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(-\mathsf{fma}\left(\frac{1}{3}, {im}^{3}, \mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\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(-\mathsf{fma}\left(\frac{1}{3}, {im}^{3}, \mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right)\right)
double f(double re, double im) {
        double r632476 = 0.5;
        double r632477 = re;
        double r632478 = sin(r632477);
        double r632479 = r632476 * r632478;
        double r632480 = im;
        double r632481 = -r632480;
        double r632482 = exp(r632481);
        double r632483 = exp(r632480);
        double r632484 = r632482 - r632483;
        double r632485 = r632479 * r632484;
        return r632485;
}

double f(double re, double im) {
        double r632486 = 0.5;
        double r632487 = re;
        double r632488 = sin(r632487);
        double r632489 = r632486 * r632488;
        double r632490 = 0.3333333333333333;
        double r632491 = im;
        double r632492 = 3.0;
        double r632493 = pow(r632491, r632492);
        double r632494 = 0.016666666666666666;
        double r632495 = 5.0;
        double r632496 = pow(r632491, r632495);
        double r632497 = 2.0;
        double r632498 = r632497 * r632491;
        double r632499 = fma(r632494, r632496, r632498);
        double r632500 = fma(r632490, r632493, r632499);
        double r632501 = -r632500;
        double r632502 = r632489 * r632501;
        return r632502;
}

Error

Bits error versus re

Bits error versus im

Target

Original44.1
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 44.1

    \[\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(-\mathsf{fma}\left(\frac{1}{3}, {im}^{3}, \mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right)\right)}\]
  4. Final simplification0.8

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

Reproduce

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