Average Error: 58.1 → 0.8
Time: 8.8s
Precision: 64
\[\left(0.5 \cdot \cos re\right) \cdot \left(e^{0.0 - im} - e^{im}\right)\]
\[\left(0.5 \cdot \cos re\right) \cdot \left(\left(-\mathsf{fma}\left({im}^{3}, \frac{1}{3}, \frac{1}{60} \cdot {im}^{5}\right)\right) - 2 \cdot im\right)\]
\left(0.5 \cdot \cos re\right) \cdot \left(e^{0.0 - im} - e^{im}\right)
\left(0.5 \cdot \cos re\right) \cdot \left(\left(-\mathsf{fma}\left({im}^{3}, \frac{1}{3}, \frac{1}{60} \cdot {im}^{5}\right)\right) - 2 \cdot im\right)
double f(double re, double im) {
        double r277423 = 0.5;
        double r277424 = re;
        double r277425 = cos(r277424);
        double r277426 = r277423 * r277425;
        double r277427 = 0.0;
        double r277428 = im;
        double r277429 = r277427 - r277428;
        double r277430 = exp(r277429);
        double r277431 = exp(r277428);
        double r277432 = r277430 - r277431;
        double r277433 = r277426 * r277432;
        return r277433;
}

double f(double re, double im) {
        double r277434 = 0.5;
        double r277435 = re;
        double r277436 = cos(r277435);
        double r277437 = r277434 * r277436;
        double r277438 = im;
        double r277439 = 3.0;
        double r277440 = pow(r277438, r277439);
        double r277441 = 0.3333333333333333;
        double r277442 = 0.016666666666666666;
        double r277443 = 5.0;
        double r277444 = pow(r277438, r277443);
        double r277445 = r277442 * r277444;
        double r277446 = fma(r277440, r277441, r277445);
        double r277447 = -r277446;
        double r277448 = 2.0;
        double r277449 = r277448 * r277438;
        double r277450 = r277447 - r277449;
        double r277451 = r277437 * r277450;
        return r277451;
}

Error

Bits error versus re

Bits error versus im

Target

Original58.1
Target0.3
Herbie0.8
\[\begin{array}{l} \mathbf{if}\;\left|im\right| \lt 1:\\ \;\;\;\;-\cos 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 \cos re\right) \cdot \left(e^{0.0 - im} - e^{im}\right)\\ \end{array}\]

Derivation

  1. Initial program 58.1

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

    \[\leadsto \left(0.5 \cdot \cos 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 \cos 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 fma-udef0.8

    \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\left(-\frac{1}{3} \cdot {im}^{3}\right) - \color{blue}{\left(\frac{1}{60} \cdot {im}^{5} + 2 \cdot im\right)}\right)\]
  6. Applied associate--r+0.8

    \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\left(\left(\left(-\frac{1}{3} \cdot {im}^{3}\right) - \frac{1}{60} \cdot {im}^{5}\right) - 2 \cdot im\right)}\]
  7. Simplified0.8

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

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

Reproduce

herbie shell --seed 2020020 +o rules:numerics
(FPCore (re im)
  :name "math.sin on complex, imaginary part"
  :precision binary64

  :herbie-target
  (if (< (fabs im) 1) (- (* (cos re) (+ (+ im (* (* (* 0.16666666666666666 im) im) im)) (* (* (* (* (* 0.008333333333333333 im) im) im) im) im)))) (* (* 0.5 (cos re)) (- (exp (- 0.0 im)) (exp im))))

  (* (* 0.5 (cos re)) (- (exp (- 0.0 im)) (exp im))))