Average Error: 43.5 → 0.8
Time: 31.6s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[-\mathsf{fma}\left(im \cdot \left(im \cdot im\right), \sin re \cdot 0.16666666666666666, \sin re \cdot \left(1.0 \cdot im + {im}^{5} \cdot 0.008333333333333333\right)\right)\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
-\mathsf{fma}\left(im \cdot \left(im \cdot im\right), \sin re \cdot 0.16666666666666666, \sin re \cdot \left(1.0 \cdot im + {im}^{5} \cdot 0.008333333333333333\right)\right)
double f(double re, double im) {
        double r7248256 = 0.5;
        double r7248257 = re;
        double r7248258 = sin(r7248257);
        double r7248259 = r7248256 * r7248258;
        double r7248260 = im;
        double r7248261 = -r7248260;
        double r7248262 = exp(r7248261);
        double r7248263 = exp(r7248260);
        double r7248264 = r7248262 - r7248263;
        double r7248265 = r7248259 * r7248264;
        return r7248265;
}

double f(double re, double im) {
        double r7248266 = im;
        double r7248267 = r7248266 * r7248266;
        double r7248268 = r7248266 * r7248267;
        double r7248269 = re;
        double r7248270 = sin(r7248269);
        double r7248271 = 0.16666666666666666;
        double r7248272 = r7248270 * r7248271;
        double r7248273 = 1.0;
        double r7248274 = r7248273 * r7248266;
        double r7248275 = 5.0;
        double r7248276 = pow(r7248266, r7248275);
        double r7248277 = 0.008333333333333333;
        double r7248278 = r7248276 * r7248277;
        double r7248279 = r7248274 + r7248278;
        double r7248280 = r7248270 * r7248279;
        double r7248281 = fma(r7248268, r7248272, r7248280);
        double r7248282 = -r7248281;
        return r7248282;
}

Error

Bits error versus re

Bits error versus im

Target

Original43.5
Target0.2
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.5

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

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

Reproduce

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