Average Error: 43.1 → 0.8
Time: 9.9s
Precision: 64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\]
\[\left(0.5 \cdot \sin 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 \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\left(0.5 \cdot \sin 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 r228047 = 0.5;
        double r228048 = re;
        double r228049 = sin(r228048);
        double r228050 = r228047 * r228049;
        double r228051 = im;
        double r228052 = -r228051;
        double r228053 = exp(r228052);
        double r228054 = exp(r228051);
        double r228055 = r228053 - r228054;
        double r228056 = r228050 * r228055;
        return r228056;
}

double f(double re, double im) {
        double r228057 = 0.5;
        double r228058 = re;
        double r228059 = sin(r228058);
        double r228060 = r228057 * r228059;
        double r228061 = im;
        double r228062 = 3.0;
        double r228063 = pow(r228061, r228062);
        double r228064 = 0.3333333333333333;
        double r228065 = 0.016666666666666666;
        double r228066 = 5.0;
        double r228067 = pow(r228061, r228066);
        double r228068 = r228065 * r228067;
        double r228069 = fma(r228063, r228064, r228068);
        double r228070 = -r228069;
        double r228071 = 2.0;
        double r228072 = r228071 * r228061;
        double r228073 = r228070 - r228072;
        double r228074 = r228060 * r228073;
        return r228074;
}

Error

Bits error versus re

Bits error versus im

Target

Original43.1
Target0.3
Herbie0.8
\[\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.1

    \[\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(\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 \sin 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 \sin 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 \sin 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 \sin 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 2020039 +o rules:numerics
(FPCore (re im)
  :name "math.cos on complex, imaginary part"
  :precision binary64

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