Average Error: 6.6 → 0.2
Time: 30.0s
Precision: 64
\[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.re - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im\]
\[\mathsf{fma}\left(x.im + x.re, \left(x.re - x.im\right) \cdot x.re, \left(\left(-x.im\right) \cdot x.re + \left(-x.im\right) \cdot x.re\right) \cdot x.im\right)\]
\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.re - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im
\mathsf{fma}\left(x.im + x.re, \left(x.re - x.im\right) \cdot x.re, \left(\left(-x.im\right) \cdot x.re + \left(-x.im\right) \cdot x.re\right) \cdot x.im\right)
double f(double x_re, double x_im) {
        double r7674978 = x_re;
        double r7674979 = r7674978 * r7674978;
        double r7674980 = x_im;
        double r7674981 = r7674980 * r7674980;
        double r7674982 = r7674979 - r7674981;
        double r7674983 = r7674982 * r7674978;
        double r7674984 = r7674978 * r7674980;
        double r7674985 = r7674980 * r7674978;
        double r7674986 = r7674984 + r7674985;
        double r7674987 = r7674986 * r7674980;
        double r7674988 = r7674983 - r7674987;
        return r7674988;
}

double f(double x_re, double x_im) {
        double r7674989 = x_im;
        double r7674990 = x_re;
        double r7674991 = r7674989 + r7674990;
        double r7674992 = r7674990 - r7674989;
        double r7674993 = r7674992 * r7674990;
        double r7674994 = -r7674989;
        double r7674995 = r7674994 * r7674990;
        double r7674996 = r7674995 + r7674995;
        double r7674997 = r7674996 * r7674989;
        double r7674998 = fma(r7674991, r7674993, r7674997);
        return r7674998;
}

Error

Bits error versus x.re

Bits error versus x.im

Target

Original6.6
Target0.3
Herbie0.2
\[\left(x.re \cdot x.re\right) \cdot \left(x.re - x.im\right) + \left(x.re \cdot x.im\right) \cdot \left(x.re - 3 \cdot x.im\right)\]

Derivation

  1. Initial program 6.6

    \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.re - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im\]
  2. Using strategy rm
  3. Applied difference-of-squares6.6

    \[\leadsto \color{blue}{\left(\left(x.re + x.im\right) \cdot \left(x.re - x.im\right)\right)} \cdot x.re - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im\]
  4. Applied associate-*l*0.3

    \[\leadsto \color{blue}{\left(x.re + x.im\right) \cdot \left(\left(x.re - x.im\right) \cdot x.re\right)} - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im\]
  5. Using strategy rm
  6. Applied fma-neg0.2

    \[\leadsto \color{blue}{\mathsf{fma}\left(x.re + x.im, \left(x.re - x.im\right) \cdot x.re, -\left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im\right)}\]
  7. Simplified0.2

    \[\leadsto \mathsf{fma}\left(x.re + x.im, \left(x.re - x.im\right) \cdot x.re, \color{blue}{\left(-x.im\right) \cdot \left(x.re \cdot x.im + x.re \cdot x.im\right)}\right)\]
  8. Final simplification0.2

    \[\leadsto \mathsf{fma}\left(x.im + x.re, \left(x.re - x.im\right) \cdot x.re, \left(\left(-x.im\right) \cdot x.re + \left(-x.im\right) \cdot x.re\right) \cdot x.im\right)\]

Reproduce

herbie shell --seed 2019152 +o rules:numerics
(FPCore (x.re x.im)
  :name "math.cube on complex, real part"

  :herbie-target
  (+ (* (* x.re x.re) (- x.re x.im)) (* (* x.re x.im) (- x.re (* 3 x.im))))

  (- (* (- (* x.re x.re) (* x.im x.im)) x.re) (* (+ (* x.re x.im) (* x.im x.re)) x.im)))