Average Error: 43.4 → 0.7
Time: 26.7s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[\left(\frac{-1}{3} \cdot {im}^{3}\right) \cdot \left(\sin re \cdot 0.5\right) + \left(-\mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right) \cdot \left(\sin re \cdot 0.5\right)\]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\left(\frac{-1}{3} \cdot {im}^{3}\right) \cdot \left(\sin re \cdot 0.5\right) + \left(-\mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right) \cdot \left(\sin re \cdot 0.5\right)
double f(double re, double im) {
        double r219098 = 0.5;
        double r219099 = re;
        double r219100 = sin(r219099);
        double r219101 = r219098 * r219100;
        double r219102 = im;
        double r219103 = -r219102;
        double r219104 = exp(r219103);
        double r219105 = exp(r219102);
        double r219106 = r219104 - r219105;
        double r219107 = r219101 * r219106;
        return r219107;
}

double f(double re, double im) {
        double r219108 = -0.3333333333333333;
        double r219109 = im;
        double r219110 = 3.0;
        double r219111 = pow(r219109, r219110);
        double r219112 = r219108 * r219111;
        double r219113 = re;
        double r219114 = sin(r219113);
        double r219115 = 0.5;
        double r219116 = r219114 * r219115;
        double r219117 = r219112 * r219116;
        double r219118 = 0.016666666666666666;
        double r219119 = 5.0;
        double r219120 = pow(r219109, r219119);
        double r219121 = 2.0;
        double r219122 = r219121 * r219109;
        double r219123 = fma(r219118, r219120, r219122);
        double r219124 = -r219123;
        double r219125 = r219124 * r219116;
        double r219126 = r219117 + r219125;
        return r219126;
}

Error

Bits error versus re

Bits error versus im

Target

Original43.4
Target0.3
Herbie0.7
\[\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 43.4

    \[\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(\frac{1}{3}, {im}^{3}, \mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right)\right)}\]
  4. Using strategy rm
  5. Applied fma-udef0.7

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

    \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \color{blue}{\left(\left(-\frac{1}{3} \cdot {im}^{3}\right) + \left(-\mathsf{fma}\left(\frac{1}{60}, {im}^{5}, 2 \cdot im\right)\right)\right)}\]
  7. Applied distribute-lft-in0.7

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

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

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

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

Reproduce

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