Average Error: 10.6 → 1.9
Time: 2.8s
Precision: binary64
\[\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 \]
\[\begin{array}{l} \mathbf{if}\;x.re \leq -20274.627700358556:\\ \;\;\;\;x.re \cdot \mathsf{fma}\left(x.re, x.re, x.im \cdot \left(x.im \cdot -3\right)\right)\\ \mathbf{elif}\;x.re \leq 2.0298236451026524 \cdot 10^{+47}:\\ \;\;\;\;\mathsf{fma}\left(x.re + x.im, x.re \cdot \left(x.re - x.im\right), x.im \cdot \left(\left(x.re \cdot x.im\right) \cdot -2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{x.re} \cdot \left(\sqrt{x.re} \cdot \mathsf{fma}\left(x.re, x.re, -3 \cdot \left(x.im \cdot x.im\right)\right)\right)\\ \end{array} \]
\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
\begin{array}{l}
\mathbf{if}\;x.re \leq -20274.627700358556:\\
\;\;\;\;x.re \cdot \mathsf{fma}\left(x.re, x.re, x.im \cdot \left(x.im \cdot -3\right)\right)\\

\mathbf{elif}\;x.re \leq 2.0298236451026524 \cdot 10^{+47}:\\
\;\;\;\;\mathsf{fma}\left(x.re + x.im, x.re \cdot \left(x.re - x.im\right), x.im \cdot \left(\left(x.re \cdot x.im\right) \cdot -2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\sqrt{x.re} \cdot \left(\sqrt{x.re} \cdot \mathsf{fma}\left(x.re, x.re, -3 \cdot \left(x.im \cdot x.im\right)\right)\right)\\


\end{array}
(FPCore (x.re x.im)
 :precision binary64
 (-
  (* (- (* x.re x.re) (* x.im x.im)) x.re)
  (* (+ (* x.re x.im) (* x.im x.re)) x.im)))
(FPCore (x.re x.im)
 :precision binary64
 (if (<= x.re -20274.627700358556)
   (* x.re (fma x.re x.re (* x.im (* x.im -3.0))))
   (if (<= x.re 2.0298236451026524e+47)
     (fma (+ x.re x.im) (* x.re (- x.re x.im)) (* x.im (* (* x.re x.im) -2.0)))
     (* (sqrt x.re) (* (sqrt x.re) (fma x.re x.re (* -3.0 (* x.im x.im))))))))
double code(double x_46_re, double x_46_im) {
	return (((x_46_re * x_46_re) - (x_46_im * x_46_im)) * x_46_re) - (((x_46_re * x_46_im) + (x_46_im * x_46_re)) * x_46_im);
}
double code(double x_46_re, double x_46_im) {
	double tmp;
	if (x_46_re <= -20274.627700358556) {
		tmp = x_46_re * fma(x_46_re, x_46_re, (x_46_im * (x_46_im * -3.0)));
	} else if (x_46_re <= 2.0298236451026524e+47) {
		tmp = fma((x_46_re + x_46_im), (x_46_re * (x_46_re - x_46_im)), (x_46_im * ((x_46_re * x_46_im) * -2.0)));
	} else {
		tmp = sqrt(x_46_re) * (sqrt(x_46_re) * fma(x_46_re, x_46_re, (-3.0 * (x_46_im * x_46_im))));
	}
	return tmp;
}

Error

Bits error versus x.re

Bits error versus x.im

Target

Original10.6
Target8.1
Herbie1.9
\[\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. Split input into 3 regimes
  2. if x.re < -20274.627700358556

    1. Initial program 14.5

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

      \[\leadsto \color{blue}{x.re \cdot \mathsf{fma}\left(x.re, x.re, \left(x.im \cdot x.im\right) \cdot -3\right)} \]
    3. Applied associate-*l*_binary643.8

      \[\leadsto x.re \cdot \mathsf{fma}\left(x.re, x.re, \color{blue}{x.im \cdot \left(x.im \cdot -3\right)}\right) \]
    4. Simplified3.8

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

    if -20274.627700358556 < x.re < 2.02982364510265238e47

    1. Initial program 6.8

      \[\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. Applied difference-of-squares_binary646.8

      \[\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 \]
    3. Applied associate-*l*_binary640.2

      \[\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 \]
    4. Applied fma-neg_binary640.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)} \]
    5. Simplified0.2

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

    if 2.02982364510265238e47 < x.re

    1. Initial program 15.9

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

      \[\leadsto \color{blue}{x.re \cdot \mathsf{fma}\left(x.re, x.re, \left(x.im \cdot x.im\right) \cdot -3\right)} \]
    3. Applied add-sqr-sqrt_binary644.1

      \[\leadsto \color{blue}{\left(\sqrt{x.re} \cdot \sqrt{x.re}\right)} \cdot \mathsf{fma}\left(x.re, x.re, \left(x.im \cdot x.im\right) \cdot -3\right) \]
    4. Applied associate-*l*_binary644.1

      \[\leadsto \color{blue}{\sqrt{x.re} \cdot \left(\sqrt{x.re} \cdot \mathsf{fma}\left(x.re, x.re, \left(x.im \cdot x.im\right) \cdot -3\right)\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification1.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;x.re \leq -20274.627700358556:\\ \;\;\;\;x.re \cdot \mathsf{fma}\left(x.re, x.re, x.im \cdot \left(x.im \cdot -3\right)\right)\\ \mathbf{elif}\;x.re \leq 2.0298236451026524 \cdot 10^{+47}:\\ \;\;\;\;\mathsf{fma}\left(x.re + x.im, x.re \cdot \left(x.re - x.im\right), x.im \cdot \left(\left(x.re \cdot x.im\right) \cdot -2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{x.re} \cdot \left(\sqrt{x.re} \cdot \mathsf{fma}\left(x.re, x.re, -3 \cdot \left(x.im \cdot x.im\right)\right)\right)\\ \end{array} \]

Reproduce

herbie shell --seed 2022088 
(FPCore (x.re x.im)
  :name "math.cube on complex, real part"
  :precision binary64

  :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)))