Average Error: 7.3 → 3.8
Time: 3.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 \cdot \left(x.re \cdot x.re - x.im \cdot x.im\right) - x.im \cdot \left(x.re \cdot x.im + x.re \cdot x.im\right) \leq -\infty:\\ \;\;\;\;\mathsf{fma}\left({\left(x.im \cdot \sqrt{x.re}\right)}^{2}, -3, {x.re}^{3}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x.re \cdot \left(x.im \cdot x.im\right), -3, {x.re}^{3}\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 (- (* x.re x.re) (* x.im x.im)))
       (* x.im (+ (* x.re x.im) (* x.re x.im))))
      (- INFINITY))
   (fma (pow (* x.im (sqrt x.re)) 2.0) -3.0 (pow x.re 3.0))
   (fma (* x.re (* x.im x.im)) -3.0 (pow x.re 3.0))))
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 * ((x_46_re * x_46_re) - (x_46_im * x_46_im))) - (x_46_im * ((x_46_re * x_46_im) + (x_46_re * x_46_im)))) <= -((double) INFINITY)) {
		tmp = fma(pow((x_46_im * sqrt(x_46_re)), 2.0), -3.0, pow(x_46_re, 3.0));
	} else {
		tmp = fma((x_46_re * (x_46_im * x_46_im)), -3.0, pow(x_46_re, 3.0));
	}
	return tmp;
}
function code(x_46_re, x_46_im)
	return Float64(Float64(Float64(Float64(x_46_re * x_46_re) - Float64(x_46_im * x_46_im)) * x_46_re) - Float64(Float64(Float64(x_46_re * x_46_im) + Float64(x_46_im * x_46_re)) * x_46_im))
end
function code(x_46_re, x_46_im)
	tmp = 0.0
	if (Float64(Float64(x_46_re * Float64(Float64(x_46_re * x_46_re) - Float64(x_46_im * x_46_im))) - Float64(x_46_im * Float64(Float64(x_46_re * x_46_im) + Float64(x_46_re * x_46_im)))) <= Float64(-Inf))
		tmp = fma((Float64(x_46_im * sqrt(x_46_re)) ^ 2.0), -3.0, (x_46_re ^ 3.0));
	else
		tmp = fma(Float64(x_46_re * Float64(x_46_im * x_46_im)), -3.0, (x_46_re ^ 3.0));
	end
	return tmp
end
code[x$46$re_, x$46$im_] := N[(N[(N[(N[(x$46$re * x$46$re), $MachinePrecision] - N[(x$46$im * x$46$im), $MachinePrecision]), $MachinePrecision] * x$46$re), $MachinePrecision] - N[(N[(N[(x$46$re * x$46$im), $MachinePrecision] + N[(x$46$im * x$46$re), $MachinePrecision]), $MachinePrecision] * x$46$im), $MachinePrecision]), $MachinePrecision]
code[x$46$re_, x$46$im_] := If[LessEqual[N[(N[(x$46$re * N[(N[(x$46$re * x$46$re), $MachinePrecision] - N[(x$46$im * x$46$im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(x$46$im * N[(N[(x$46$re * x$46$im), $MachinePrecision] + N[(x$46$re * x$46$im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], (-Infinity)], N[(N[Power[N[(x$46$im * N[Sqrt[x$46$re], $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] * -3.0 + N[Power[x$46$re, 3.0], $MachinePrecision]), $MachinePrecision], N[(N[(x$46$re * N[(x$46$im * x$46$im), $MachinePrecision]), $MachinePrecision] * -3.0 + N[Power[x$46$re, 3.0], $MachinePrecision]), $MachinePrecision]]
\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 \cdot \left(x.re \cdot x.re - x.im \cdot x.im\right) - x.im \cdot \left(x.re \cdot x.im + x.re \cdot x.im\right) \leq -\infty:\\
\;\;\;\;\mathsf{fma}\left({\left(x.im \cdot \sqrt{x.re}\right)}^{2}, -3, {x.re}^{3}\right)\\

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


\end{array}

Error

Bits error versus x.re

Bits error versus x.im

Target

Original7.3
Target0.2
Herbie3.8
\[\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 2 regimes
  2. if (-.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.re) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.im)) < -inf.0

    1. Initial program 64.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 in x.re around 0 64.0

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(x.re \cdot \left(x.im \cdot x.im\right), -3, {x.re}^{3}\right)} \]
    4. Applied egg-rr0.7

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

    if -inf.0 < (-.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.re) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.im))

    1. Initial program 4.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 in x.re around 0 3.9

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

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

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

Reproduce

herbie shell --seed 2022133 
(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)))