Average Error: 44.6 → 0.7
Time: 30.6s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[\left(\sin re \cdot \left(-0.5\right)\right) \cdot \mathsf{fma}\left(\left({im}^{5}\right), \frac{1}{60}, \left(\left(\log \left(e^{\left(im \cdot im\right) \cdot \frac{1}{3}}\right) + 2\right) \cdot im\right)\right)\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\left(\sin re \cdot \left(-0.5\right)\right) \cdot \mathsf{fma}\left(\left({im}^{5}\right), \frac{1}{60}, \left(\left(\log \left(e^{\left(im \cdot im\right) \cdot \frac{1}{3}}\right) + 2\right) \cdot im\right)\right)
double f(double re, double im) {
        double r9660998 = 0.5;
        double r9660999 = re;
        double r9661000 = sin(r9660999);
        double r9661001 = r9660998 * r9661000;
        double r9661002 = im;
        double r9661003 = -r9661002;
        double r9661004 = exp(r9661003);
        double r9661005 = exp(r9661002);
        double r9661006 = r9661004 - r9661005;
        double r9661007 = r9661001 * r9661006;
        return r9661007;
}

double f(double re, double im) {
        double r9661008 = re;
        double r9661009 = sin(r9661008);
        double r9661010 = 0.5;
        double r9661011 = -r9661010;
        double r9661012 = r9661009 * r9661011;
        double r9661013 = im;
        double r9661014 = 5.0;
        double r9661015 = pow(r9661013, r9661014);
        double r9661016 = 0.016666666666666666;
        double r9661017 = r9661013 * r9661013;
        double r9661018 = 0.3333333333333333;
        double r9661019 = r9661017 * r9661018;
        double r9661020 = exp(r9661019);
        double r9661021 = log(r9661020);
        double r9661022 = 2.0;
        double r9661023 = r9661021 + r9661022;
        double r9661024 = r9661023 * r9661013;
        double r9661025 = fma(r9661015, r9661016, r9661024);
        double r9661026 = r9661012 * r9661025;
        return r9661026;
}

Error

Bits error versus re

Bits error versus im

Target

Original44.6
Target0.2
Herbie0.7
\[\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 44.6

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

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

    \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \color{blue}{\left(-\mathsf{fma}\left(\left({im}^{5}\right), \frac{1}{60}, \left(im \cdot \left(\frac{1}{3} \cdot \left(im \cdot im\right) + 2\right)\right)\right)\right)}\]
  4. Using strategy rm
  5. Applied add-log-exp0.7

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

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

Reproduce

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