Average Error: 7.0 → 0.2
Time: 1.1m
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(-x.im\right) \cdot \left(x.re \cdot x.im + x.re \cdot x.im\right)\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(-x.im\right) \cdot \left(x.re \cdot x.im + x.re \cdot x.im\right)\right)
double f(double x_re, double x_im) {
        double r8911697 = x_re;
        double r8911698 = r8911697 * r8911697;
        double r8911699 = x_im;
        double r8911700 = r8911699 * r8911699;
        double r8911701 = r8911698 - r8911700;
        double r8911702 = r8911701 * r8911697;
        double r8911703 = r8911697 * r8911699;
        double r8911704 = r8911699 * r8911697;
        double r8911705 = r8911703 + r8911704;
        double r8911706 = r8911705 * r8911699;
        double r8911707 = r8911702 - r8911706;
        return r8911707;
}

double f(double x_re, double x_im) {
        double r8911708 = x_im;
        double r8911709 = x_re;
        double r8911710 = r8911708 + r8911709;
        double r8911711 = r8911709 - r8911708;
        double r8911712 = r8911711 * r8911709;
        double r8911713 = -r8911708;
        double r8911714 = r8911709 * r8911708;
        double r8911715 = r8911714 + r8911714;
        double r8911716 = r8911713 * r8911715;
        double r8911717 = fma(r8911710, r8911712, r8911716);
        return r8911717;
}

Error

Bits error versus x.re

Bits error versus x.im

Target

Original7.0
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 7.0

    \[\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. Taylor expanded around 0 7.0

    \[\leadsto \color{blue}{\left({x.re}^{3} - {x.im}^{2} \cdot x.re\right)} - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im\]
  3. Simplified0.2

    \[\leadsto \color{blue}{\left(x.im + x.re\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\]
  4. Using strategy rm
  5. Applied fma-neg0.2

    \[\leadsto \color{blue}{\mathsf{fma}\left(x.im + x.re, \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)}\]
  6. Simplified0.2

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

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

Reproduce

herbie shell --seed 2019200 +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.0 x.im))))

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