Average Error: 43.7 → 0.8
Time: 22.2s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[\left(\left(\mathsf{fma}\left({im}^{5}, \frac{-1}{60}, {im}^{3} \cdot \frac{-1}{3}\right) - 2 \cdot im\right) \cdot 0.5\right) \cdot \sin re\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\left(\left(\mathsf{fma}\left({im}^{5}, \frac{-1}{60}, {im}^{3} \cdot \frac{-1}{3}\right) - 2 \cdot im\right) \cdot 0.5\right) \cdot \sin re
double f(double re, double im) {
        double r134230 = 0.5;
        double r134231 = re;
        double r134232 = sin(r134231);
        double r134233 = r134230 * r134232;
        double r134234 = im;
        double r134235 = -r134234;
        double r134236 = exp(r134235);
        double r134237 = exp(r134234);
        double r134238 = r134236 - r134237;
        double r134239 = r134233 * r134238;
        return r134239;
}

double f(double re, double im) {
        double r134240 = im;
        double r134241 = 5.0;
        double r134242 = pow(r134240, r134241);
        double r134243 = -0.016666666666666666;
        double r134244 = 3.0;
        double r134245 = pow(r134240, r134244);
        double r134246 = -0.3333333333333333;
        double r134247 = r134245 * r134246;
        double r134248 = fma(r134242, r134243, r134247);
        double r134249 = 2.0;
        double r134250 = r134249 * r134240;
        double r134251 = r134248 - r134250;
        double r134252 = 0.5;
        double r134253 = r134251 * r134252;
        double r134254 = re;
        double r134255 = sin(r134254);
        double r134256 = r134253 * r134255;
        return r134256;
}

Error

Bits error versus re

Bits error versus im

Target

Original43.7
Target0.3
Herbie0.8
\[\begin{array}{l} \mathbf{if}\;\left|im\right| \lt 1:\\ \;\;\;\;-\sin re \cdot \left(\left(im + \left(\left(0.1666666666666666574148081281236954964697 \cdot im\right) \cdot im\right) \cdot im\right) + \left(\left(\left(\left(0.008333333333333333217685101601546193705872 \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.7

    \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
  2. Simplified43.7

    \[\leadsto \color{blue}{\left(\left(e^{-im} - e^{im}\right) \cdot 0.5\right) \cdot \sin re}\]
  3. Taylor expanded around 0 0.8

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

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

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

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

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

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

Reproduce

herbie shell --seed 2019194 +o rules:numerics
(FPCore (re im)
  :name "math.cos on complex, imaginary part"

  :herbie-target
  (if (< (fabs im) 1.0) (- (* (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))))