_divideComplex, real part

Percentage Accurate: 61.5% → 79.8%
Time: 7.1s
Alternatives: 6
Speedup: 1.6×

Specification

?
\[\begin{array}{l} \\ \frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (/ (+ (* x.re y.re) (* x.im y.im)) (+ (* y.re y.re) (* y.im y.im))))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return ((x_46_re * y_46_re) + (x_46_im * y_46_im)) / ((y_46_re * y_46_re) + (y_46_im * y_46_im));
}
real(8) function code(x_46re, x_46im, y_46re, y_46im)
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im
    real(8), intent (in) :: y_46re
    real(8), intent (in) :: y_46im
    code = ((x_46re * y_46re) + (x_46im * y_46im)) / ((y_46re * y_46re) + (y_46im * y_46im))
end function
public static double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return ((x_46_re * y_46_re) + (x_46_im * y_46_im)) / ((y_46_re * y_46_re) + (y_46_im * y_46_im));
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	return ((x_46_re * y_46_re) + (x_46_im * y_46_im)) / ((y_46_re * y_46_re) + (y_46_im * y_46_im))
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	return Float64(Float64(Float64(x_46_re * y_46_re) + Float64(x_46_im * y_46_im)) / Float64(Float64(y_46_re * y_46_re) + Float64(y_46_im * y_46_im)))
end
function tmp = code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = ((x_46_re * y_46_re) + (x_46_im * y_46_im)) / ((y_46_re * y_46_re) + (y_46_im * y_46_im));
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(N[(N[(x$46$re * y$46$re), $MachinePrecision] + N[(x$46$im * y$46$im), $MachinePrecision]), $MachinePrecision] / N[(N[(y$46$re * y$46$re), $MachinePrecision] + N[(y$46$im * y$46$im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 6 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 61.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (/ (+ (* x.re y.re) (* x.im y.im)) (+ (* y.re y.re) (* y.im y.im))))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return ((x_46_re * y_46_re) + (x_46_im * y_46_im)) / ((y_46_re * y_46_re) + (y_46_im * y_46_im));
}
real(8) function code(x_46re, x_46im, y_46re, y_46im)
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im
    real(8), intent (in) :: y_46re
    real(8), intent (in) :: y_46im
    code = ((x_46re * y_46re) + (x_46im * y_46im)) / ((y_46re * y_46re) + (y_46im * y_46im))
end function
public static double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return ((x_46_re * y_46_re) + (x_46_im * y_46_im)) / ((y_46_re * y_46_re) + (y_46_im * y_46_im));
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	return ((x_46_re * y_46_re) + (x_46_im * y_46_im)) / ((y_46_re * y_46_re) + (y_46_im * y_46_im))
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	return Float64(Float64(Float64(x_46_re * y_46_re) + Float64(x_46_im * y_46_im)) / Float64(Float64(y_46_re * y_46_re) + Float64(y_46_im * y_46_im)))
end
function tmp = code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = ((x_46_re * y_46_re) + (x_46_im * y_46_im)) / ((y_46_re * y_46_re) + (y_46_im * y_46_im));
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(N[(N[(x$46$re * y$46$re), $MachinePrecision] + N[(x$46$im * y$46$im), $MachinePrecision]), $MachinePrecision] / N[(N[(y$46$re * y$46$re), $MachinePrecision] + N[(y$46$im * y$46$im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im}
\end{array}

Alternative 1: 79.8% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}{y.re}\\ \mathbf{if}\;y.re \leq -2.4 \cdot 10^{+97}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y.re \leq -4.2 \cdot 10^{-78}:\\ \;\;\;\;\frac{\mathsf{fma}\left(y.re, x.re, y.im \cdot x.im\right)}{y.im \cdot y.im + y.re \cdot y.re}\\ \mathbf{elif}\;y.re \leq 1.75 \cdot 10^{-14}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{x.re}{y.im}, y.re, x.im\right)}{y.im}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (let* ((t_0 (/ (fma (/ x.im y.re) y.im x.re) y.re)))
   (if (<= y.re -2.4e+97)
     t_0
     (if (<= y.re -4.2e-78)
       (/ (fma y.re x.re (* y.im x.im)) (+ (* y.im y.im) (* y.re y.re)))
       (if (<= y.re 1.75e-14) (/ (fma (/ x.re y.im) y.re x.im) y.im) t_0)))))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double t_0 = fma((x_46_im / y_46_re), y_46_im, x_46_re) / y_46_re;
	double tmp;
	if (y_46_re <= -2.4e+97) {
		tmp = t_0;
	} else if (y_46_re <= -4.2e-78) {
		tmp = fma(y_46_re, x_46_re, (y_46_im * x_46_im)) / ((y_46_im * y_46_im) + (y_46_re * y_46_re));
	} else if (y_46_re <= 1.75e-14) {
		tmp = fma((x_46_re / y_46_im), y_46_re, x_46_im) / y_46_im;
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	t_0 = Float64(fma(Float64(x_46_im / y_46_re), y_46_im, x_46_re) / y_46_re)
	tmp = 0.0
	if (y_46_re <= -2.4e+97)
		tmp = t_0;
	elseif (y_46_re <= -4.2e-78)
		tmp = Float64(fma(y_46_re, x_46_re, Float64(y_46_im * x_46_im)) / Float64(Float64(y_46_im * y_46_im) + Float64(y_46_re * y_46_re)));
	elseif (y_46_re <= 1.75e-14)
		tmp = Float64(fma(Float64(x_46_re / y_46_im), y_46_re, x_46_im) / y_46_im);
	else
		tmp = t_0;
	end
	return tmp
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := Block[{t$95$0 = N[(N[(N[(x$46$im / y$46$re), $MachinePrecision] * y$46$im + x$46$re), $MachinePrecision] / y$46$re), $MachinePrecision]}, If[LessEqual[y$46$re, -2.4e+97], t$95$0, If[LessEqual[y$46$re, -4.2e-78], N[(N[(y$46$re * x$46$re + N[(y$46$im * x$46$im), $MachinePrecision]), $MachinePrecision] / N[(N[(y$46$im * y$46$im), $MachinePrecision] + N[(y$46$re * y$46$re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[y$46$re, 1.75e-14], N[(N[(N[(x$46$re / y$46$im), $MachinePrecision] * y$46$re + x$46$im), $MachinePrecision] / y$46$im), $MachinePrecision], t$95$0]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}{y.re}\\
\mathbf{if}\;y.re \leq -2.4 \cdot 10^{+97}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y.re \leq -4.2 \cdot 10^{-78}:\\
\;\;\;\;\frac{\mathsf{fma}\left(y.re, x.re, y.im \cdot x.im\right)}{y.im \cdot y.im + y.re \cdot y.re}\\

\mathbf{elif}\;y.re \leq 1.75 \cdot 10^{-14}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{x.re}{y.im}, y.re, x.im\right)}{y.im}\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y.re < -2.4e97 or 1.7500000000000001e-14 < y.re

    1. Initial program 43.2%

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Add Preprocessing
    3. Taylor expanded in y.re around inf

      \[\leadsto \color{blue}{\frac{x.re + \frac{x.im \cdot y.im}{y.re}}{y.re}} \]
    4. Step-by-step derivation
      1. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{x.re + \frac{x.im \cdot y.im}{y.re}}{y.re}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{x.im \cdot y.im}{y.re} + x.re}}{y.re} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{y.im \cdot x.im}}{y.re} + x.re}{y.re} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{y.im \cdot \frac{x.im}{y.re}} + x.re}{y.re} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{x.im}{y.re} \cdot y.im} + x.re}{y.re} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}}{y.re} \]
      7. lower-/.f6480.0

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{x.im}{y.re}}, y.im, x.re\right)}{y.re} \]
    5. Applied rewrites80.0%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}{y.re}} \]

    if -2.4e97 < y.re < -4.2000000000000001e-78

    1. Initial program 85.5%

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \frac{\color{blue}{x.re \cdot y.re + x.im \cdot y.im}}{y.re \cdot y.re + y.im \cdot y.im} \]
      2. lift-*.f64N/A

        \[\leadsto \frac{\color{blue}{x.re \cdot y.re} + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{y.re \cdot x.re} + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
      4. lower-fma.f6485.6

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(y.re, x.re, x.im \cdot y.im\right)}}{y.re \cdot y.re + y.im \cdot y.im} \]
      5. lift-*.f64N/A

        \[\leadsto \frac{\mathsf{fma}\left(y.re, x.re, \color{blue}{x.im \cdot y.im}\right)}{y.re \cdot y.re + y.im \cdot y.im} \]
      6. *-commutativeN/A

        \[\leadsto \frac{\mathsf{fma}\left(y.re, x.re, \color{blue}{y.im \cdot x.im}\right)}{y.re \cdot y.re + y.im \cdot y.im} \]
      7. lower-*.f6485.6

        \[\leadsto \frac{\mathsf{fma}\left(y.re, x.re, \color{blue}{y.im \cdot x.im}\right)}{y.re \cdot y.re + y.im \cdot y.im} \]
    4. Applied rewrites85.6%

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

    if -4.2000000000000001e-78 < y.re < 1.7500000000000001e-14

    1. Initial program 65.1%

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Add Preprocessing
    3. Taylor expanded in y.im around inf

      \[\leadsto \color{blue}{\frac{x.im + \frac{x.re \cdot y.re}{y.im}}{y.im}} \]
    4. Step-by-step derivation
      1. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{x.im + \frac{x.re \cdot y.re}{y.im}}{y.im}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{x.re \cdot y.re}{y.im} + x.im}}{y.im} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{y.re \cdot x.re}}{y.im} + x.im}{y.im} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{y.re \cdot \frac{x.re}{y.im}} + x.im}{y.im} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{x.re}{y.im} \cdot y.re} + x.im}{y.im} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{x.re}{y.im}, y.re, x.im\right)}}{y.im} \]
      7. lower-/.f6487.2

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{x.re}{y.im}}, y.re, x.im\right)}{y.im} \]
    5. Applied rewrites87.2%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{x.re}{y.im}, y.re, x.im\right)}{y.im}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification84.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y.re \leq -2.4 \cdot 10^{+97}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}{y.re}\\ \mathbf{elif}\;y.re \leq -4.2 \cdot 10^{-78}:\\ \;\;\;\;\frac{\mathsf{fma}\left(y.re, x.re, y.im \cdot x.im\right)}{y.im \cdot y.im + y.re \cdot y.re}\\ \mathbf{elif}\;y.re \leq 1.75 \cdot 10^{-14}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{x.re}{y.im}, y.re, x.im\right)}{y.im}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}{y.re}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 72.7% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y.im \leq -7.5 \cdot 10^{+63}:\\ \;\;\;\;\frac{x.im}{y.im}\\ \mathbf{elif}\;y.im \leq 3.4 \cdot 10^{+17}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}{y.re}\\ \mathbf{elif}\;y.im \leq 1.75 \cdot 10^{+143}:\\ \;\;\;\;\frac{x.im}{\mathsf{fma}\left(y.im, y.im, y.re \cdot y.re\right)} \cdot y.im\\ \mathbf{else}:\\ \;\;\;\;\frac{x.im}{y.im}\\ \end{array} \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (if (<= y.im -7.5e+63)
   (/ x.im y.im)
   (if (<= y.im 3.4e+17)
     (/ (fma (/ x.im y.re) y.im x.re) y.re)
     (if (<= y.im 1.75e+143)
       (* (/ x.im (fma y.im y.im (* y.re y.re))) y.im)
       (/ x.im y.im)))))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double tmp;
	if (y_46_im <= -7.5e+63) {
		tmp = x_46_im / y_46_im;
	} else if (y_46_im <= 3.4e+17) {
		tmp = fma((x_46_im / y_46_re), y_46_im, x_46_re) / y_46_re;
	} else if (y_46_im <= 1.75e+143) {
		tmp = (x_46_im / fma(y_46_im, y_46_im, (y_46_re * y_46_re))) * y_46_im;
	} else {
		tmp = x_46_im / y_46_im;
	}
	return tmp;
}
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = 0.0
	if (y_46_im <= -7.5e+63)
		tmp = Float64(x_46_im / y_46_im);
	elseif (y_46_im <= 3.4e+17)
		tmp = Float64(fma(Float64(x_46_im / y_46_re), y_46_im, x_46_re) / y_46_re);
	elseif (y_46_im <= 1.75e+143)
		tmp = Float64(Float64(x_46_im / fma(y_46_im, y_46_im, Float64(y_46_re * y_46_re))) * y_46_im);
	else
		tmp = Float64(x_46_im / y_46_im);
	end
	return tmp
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := If[LessEqual[y$46$im, -7.5e+63], N[(x$46$im / y$46$im), $MachinePrecision], If[LessEqual[y$46$im, 3.4e+17], N[(N[(N[(x$46$im / y$46$re), $MachinePrecision] * y$46$im + x$46$re), $MachinePrecision] / y$46$re), $MachinePrecision], If[LessEqual[y$46$im, 1.75e+143], N[(N[(x$46$im / N[(y$46$im * y$46$im + N[(y$46$re * y$46$re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * y$46$im), $MachinePrecision], N[(x$46$im / y$46$im), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y.im \leq -7.5 \cdot 10^{+63}:\\
\;\;\;\;\frac{x.im}{y.im}\\

\mathbf{elif}\;y.im \leq 3.4 \cdot 10^{+17}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}{y.re}\\

\mathbf{elif}\;y.im \leq 1.75 \cdot 10^{+143}:\\
\;\;\;\;\frac{x.im}{\mathsf{fma}\left(y.im, y.im, y.re \cdot y.re\right)} \cdot y.im\\

\mathbf{else}:\\
\;\;\;\;\frac{x.im}{y.im}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y.im < -7.5000000000000005e63 or 1.75000000000000004e143 < y.im

    1. Initial program 39.1%

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Add Preprocessing
    3. Taylor expanded in y.im around inf

      \[\leadsto \color{blue}{\frac{x.im}{y.im}} \]
    4. Step-by-step derivation
      1. lower-/.f6480.9

        \[\leadsto \color{blue}{\frac{x.im}{y.im}} \]
    5. Applied rewrites80.9%

      \[\leadsto \color{blue}{\frac{x.im}{y.im}} \]

    if -7.5000000000000005e63 < y.im < 3.4e17

    1. Initial program 68.3%

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Add Preprocessing
    3. Taylor expanded in y.re around inf

      \[\leadsto \color{blue}{\frac{x.re + \frac{x.im \cdot y.im}{y.re}}{y.re}} \]
    4. Step-by-step derivation
      1. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{x.re + \frac{x.im \cdot y.im}{y.re}}{y.re}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{x.im \cdot y.im}{y.re} + x.re}}{y.re} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{y.im \cdot x.im}}{y.re} + x.re}{y.re} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{y.im \cdot \frac{x.im}{y.re}} + x.re}{y.re} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{x.im}{y.re} \cdot y.im} + x.re}{y.re} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}}{y.re} \]
      7. lower-/.f6475.0

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{x.im}{y.re}}, y.im, x.re\right)}{y.re} \]
    5. Applied rewrites75.0%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}{y.re}} \]

    if 3.4e17 < y.im < 1.75000000000000004e143

    1. Initial program 69.4%

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Add Preprocessing
    3. Taylor expanded in x.im around inf

      \[\leadsto \color{blue}{\frac{x.im \cdot y.im}{{y.im}^{2} + {y.re}^{2}}} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{y.im \cdot x.im}}{{y.im}^{2} + {y.re}^{2}} \]
      2. associate-/l*N/A

        \[\leadsto \color{blue}{y.im \cdot \frac{x.im}{{y.im}^{2} + {y.re}^{2}}} \]
      3. *-commutativeN/A

        \[\leadsto \color{blue}{\frac{x.im}{{y.im}^{2} + {y.re}^{2}} \cdot y.im} \]
      4. lower-*.f64N/A

        \[\leadsto \color{blue}{\frac{x.im}{{y.im}^{2} + {y.re}^{2}} \cdot y.im} \]
      5. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{x.im}{{y.im}^{2} + {y.re}^{2}}} \cdot y.im \]
      6. unpow2N/A

        \[\leadsto \frac{x.im}{\color{blue}{y.im \cdot y.im} + {y.re}^{2}} \cdot y.im \]
      7. lower-fma.f64N/A

        \[\leadsto \frac{x.im}{\color{blue}{\mathsf{fma}\left(y.im, y.im, {y.re}^{2}\right)}} \cdot y.im \]
      8. unpow2N/A

        \[\leadsto \frac{x.im}{\mathsf{fma}\left(y.im, y.im, \color{blue}{y.re \cdot y.re}\right)} \cdot y.im \]
      9. lower-*.f6469.3

        \[\leadsto \frac{x.im}{\mathsf{fma}\left(y.im, y.im, \color{blue}{y.re \cdot y.re}\right)} \cdot y.im \]
    5. Applied rewrites69.3%

      \[\leadsto \color{blue}{\frac{x.im}{\mathsf{fma}\left(y.im, y.im, y.re \cdot y.re\right)} \cdot y.im} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 3: 65.5% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y.im \leq -7.7:\\ \;\;\;\;\frac{x.im}{y.im}\\ \mathbf{elif}\;y.im \leq 1200000:\\ \;\;\;\;\frac{x.re}{y.re}\\ \mathbf{elif}\;y.im \leq 1.75 \cdot 10^{+143}:\\ \;\;\;\;\frac{x.im}{\mathsf{fma}\left(y.im, y.im, y.re \cdot y.re\right)} \cdot y.im\\ \mathbf{else}:\\ \;\;\;\;\frac{x.im}{y.im}\\ \end{array} \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (if (<= y.im -7.7)
   (/ x.im y.im)
   (if (<= y.im 1200000.0)
     (/ x.re y.re)
     (if (<= y.im 1.75e+143)
       (* (/ x.im (fma y.im y.im (* y.re y.re))) y.im)
       (/ x.im y.im)))))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double tmp;
	if (y_46_im <= -7.7) {
		tmp = x_46_im / y_46_im;
	} else if (y_46_im <= 1200000.0) {
		tmp = x_46_re / y_46_re;
	} else if (y_46_im <= 1.75e+143) {
		tmp = (x_46_im / fma(y_46_im, y_46_im, (y_46_re * y_46_re))) * y_46_im;
	} else {
		tmp = x_46_im / y_46_im;
	}
	return tmp;
}
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = 0.0
	if (y_46_im <= -7.7)
		tmp = Float64(x_46_im / y_46_im);
	elseif (y_46_im <= 1200000.0)
		tmp = Float64(x_46_re / y_46_re);
	elseif (y_46_im <= 1.75e+143)
		tmp = Float64(Float64(x_46_im / fma(y_46_im, y_46_im, Float64(y_46_re * y_46_re))) * y_46_im);
	else
		tmp = Float64(x_46_im / y_46_im);
	end
	return tmp
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := If[LessEqual[y$46$im, -7.7], N[(x$46$im / y$46$im), $MachinePrecision], If[LessEqual[y$46$im, 1200000.0], N[(x$46$re / y$46$re), $MachinePrecision], If[LessEqual[y$46$im, 1.75e+143], N[(N[(x$46$im / N[(y$46$im * y$46$im + N[(y$46$re * y$46$re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * y$46$im), $MachinePrecision], N[(x$46$im / y$46$im), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y.im \leq -7.7:\\
\;\;\;\;\frac{x.im}{y.im}\\

\mathbf{elif}\;y.im \leq 1200000:\\
\;\;\;\;\frac{x.re}{y.re}\\

\mathbf{elif}\;y.im \leq 1.75 \cdot 10^{+143}:\\
\;\;\;\;\frac{x.im}{\mathsf{fma}\left(y.im, y.im, y.re \cdot y.re\right)} \cdot y.im\\

\mathbf{else}:\\
\;\;\;\;\frac{x.im}{y.im}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y.im < -7.70000000000000018 or 1.75000000000000004e143 < y.im

    1. Initial program 42.5%

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Add Preprocessing
    3. Taylor expanded in y.im around inf

      \[\leadsto \color{blue}{\frac{x.im}{y.im}} \]
    4. Step-by-step derivation
      1. lower-/.f6475.1

        \[\leadsto \color{blue}{\frac{x.im}{y.im}} \]
    5. Applied rewrites75.1%

      \[\leadsto \color{blue}{\frac{x.im}{y.im}} \]

    if -7.70000000000000018 < y.im < 1.2e6

    1. Initial program 68.6%

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Add Preprocessing
    3. Taylor expanded in y.im around 0

      \[\leadsto \color{blue}{\frac{x.re}{y.re}} \]
    4. Step-by-step derivation
      1. lower-/.f6468.2

        \[\leadsto \color{blue}{\frac{x.re}{y.re}} \]
    5. Applied rewrites68.2%

      \[\leadsto \color{blue}{\frac{x.re}{y.re}} \]

    if 1.2e6 < y.im < 1.75000000000000004e143

    1. Initial program 71.2%

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Add Preprocessing
    3. Taylor expanded in x.im around inf

      \[\leadsto \color{blue}{\frac{x.im \cdot y.im}{{y.im}^{2} + {y.re}^{2}}} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{y.im \cdot x.im}}{{y.im}^{2} + {y.re}^{2}} \]
      2. associate-/l*N/A

        \[\leadsto \color{blue}{y.im \cdot \frac{x.im}{{y.im}^{2} + {y.re}^{2}}} \]
      3. *-commutativeN/A

        \[\leadsto \color{blue}{\frac{x.im}{{y.im}^{2} + {y.re}^{2}} \cdot y.im} \]
      4. lower-*.f64N/A

        \[\leadsto \color{blue}{\frac{x.im}{{y.im}^{2} + {y.re}^{2}} \cdot y.im} \]
      5. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{x.im}{{y.im}^{2} + {y.re}^{2}}} \cdot y.im \]
      6. unpow2N/A

        \[\leadsto \frac{x.im}{\color{blue}{y.im \cdot y.im} + {y.re}^{2}} \cdot y.im \]
      7. lower-fma.f64N/A

        \[\leadsto \frac{x.im}{\color{blue}{\mathsf{fma}\left(y.im, y.im, {y.re}^{2}\right)}} \cdot y.im \]
      8. unpow2N/A

        \[\leadsto \frac{x.im}{\mathsf{fma}\left(y.im, y.im, \color{blue}{y.re \cdot y.re}\right)} \cdot y.im \]
      9. lower-*.f6468.3

        \[\leadsto \frac{x.im}{\mathsf{fma}\left(y.im, y.im, \color{blue}{y.re \cdot y.re}\right)} \cdot y.im \]
    5. Applied rewrites68.3%

      \[\leadsto \color{blue}{\frac{x.im}{\mathsf{fma}\left(y.im, y.im, y.re \cdot y.re\right)} \cdot y.im} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 4: 76.6% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}{y.re}\\ \mathbf{if}\;y.re \leq -6.4 \cdot 10^{-78}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y.re \leq 1.75 \cdot 10^{-14}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{x.re}{y.im}, y.re, x.im\right)}{y.im}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (let* ((t_0 (/ (fma (/ x.im y.re) y.im x.re) y.re)))
   (if (<= y.re -6.4e-78)
     t_0
     (if (<= y.re 1.75e-14) (/ (fma (/ x.re y.im) y.re x.im) y.im) t_0))))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double t_0 = fma((x_46_im / y_46_re), y_46_im, x_46_re) / y_46_re;
	double tmp;
	if (y_46_re <= -6.4e-78) {
		tmp = t_0;
	} else if (y_46_re <= 1.75e-14) {
		tmp = fma((x_46_re / y_46_im), y_46_re, x_46_im) / y_46_im;
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	t_0 = Float64(fma(Float64(x_46_im / y_46_re), y_46_im, x_46_re) / y_46_re)
	tmp = 0.0
	if (y_46_re <= -6.4e-78)
		tmp = t_0;
	elseif (y_46_re <= 1.75e-14)
		tmp = Float64(fma(Float64(x_46_re / y_46_im), y_46_re, x_46_im) / y_46_im);
	else
		tmp = t_0;
	end
	return tmp
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := Block[{t$95$0 = N[(N[(N[(x$46$im / y$46$re), $MachinePrecision] * y$46$im + x$46$re), $MachinePrecision] / y$46$re), $MachinePrecision]}, If[LessEqual[y$46$re, -6.4e-78], t$95$0, If[LessEqual[y$46$re, 1.75e-14], N[(N[(N[(x$46$re / y$46$im), $MachinePrecision] * y$46$re + x$46$im), $MachinePrecision] / y$46$im), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}{y.re}\\
\mathbf{if}\;y.re \leq -6.4 \cdot 10^{-78}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y.re \leq 1.75 \cdot 10^{-14}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{x.re}{y.im}, y.re, x.im\right)}{y.im}\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y.re < -6.4e-78 or 1.7500000000000001e-14 < y.re

    1. Initial program 53.6%

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Add Preprocessing
    3. Taylor expanded in y.re around inf

      \[\leadsto \color{blue}{\frac{x.re + \frac{x.im \cdot y.im}{y.re}}{y.re}} \]
    4. Step-by-step derivation
      1. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{x.re + \frac{x.im \cdot y.im}{y.re}}{y.re}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{x.im \cdot y.im}{y.re} + x.re}}{y.re} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{y.im \cdot x.im}}{y.re} + x.re}{y.re} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{y.im \cdot \frac{x.im}{y.re}} + x.re}{y.re} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{x.im}{y.re} \cdot y.im} + x.re}{y.re} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}}{y.re} \]
      7. lower-/.f6476.6

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{x.im}{y.re}}, y.im, x.re\right)}{y.re} \]
    5. Applied rewrites76.6%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{x.im}{y.re}, y.im, x.re\right)}{y.re}} \]

    if -6.4e-78 < y.re < 1.7500000000000001e-14

    1. Initial program 65.1%

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Add Preprocessing
    3. Taylor expanded in y.im around inf

      \[\leadsto \color{blue}{\frac{x.im + \frac{x.re \cdot y.re}{y.im}}{y.im}} \]
    4. Step-by-step derivation
      1. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{x.im + \frac{x.re \cdot y.re}{y.im}}{y.im}} \]
      2. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{x.re \cdot y.re}{y.im} + x.im}}{y.im} \]
      3. *-commutativeN/A

        \[\leadsto \frac{\frac{\color{blue}{y.re \cdot x.re}}{y.im} + x.im}{y.im} \]
      4. associate-/l*N/A

        \[\leadsto \frac{\color{blue}{y.re \cdot \frac{x.re}{y.im}} + x.im}{y.im} \]
      5. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{\frac{x.re}{y.im} \cdot y.re} + x.im}{y.im} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{x.re}{y.im}, y.re, x.im\right)}}{y.im} \]
      7. lower-/.f6487.2

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{x.re}{y.im}}, y.re, x.im\right)}{y.im} \]
    5. Applied rewrites87.2%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{x.re}{y.im}, y.re, x.im\right)}{y.im}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 64.8% accurate, 1.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y.im \leq -7.7:\\ \;\;\;\;\frac{x.im}{y.im}\\ \mathbf{elif}\;y.im \leq 3.4 \cdot 10^{+17}:\\ \;\;\;\;\frac{x.re}{y.re}\\ \mathbf{else}:\\ \;\;\;\;\frac{x.im}{y.im}\\ \end{array} \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (if (<= y.im -7.7)
   (/ x.im y.im)
   (if (<= y.im 3.4e+17) (/ x.re y.re) (/ x.im y.im))))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double tmp;
	if (y_46_im <= -7.7) {
		tmp = x_46_im / y_46_im;
	} else if (y_46_im <= 3.4e+17) {
		tmp = x_46_re / y_46_re;
	} else {
		tmp = x_46_im / y_46_im;
	}
	return tmp;
}
real(8) function code(x_46re, x_46im, y_46re, y_46im)
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im
    real(8), intent (in) :: y_46re
    real(8), intent (in) :: y_46im
    real(8) :: tmp
    if (y_46im <= (-7.7d0)) then
        tmp = x_46im / y_46im
    else if (y_46im <= 3.4d+17) then
        tmp = x_46re / y_46re
    else
        tmp = x_46im / y_46im
    end if
    code = tmp
end function
public static double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double tmp;
	if (y_46_im <= -7.7) {
		tmp = x_46_im / y_46_im;
	} else if (y_46_im <= 3.4e+17) {
		tmp = x_46_re / y_46_re;
	} else {
		tmp = x_46_im / y_46_im;
	}
	return tmp;
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	tmp = 0
	if y_46_im <= -7.7:
		tmp = x_46_im / y_46_im
	elif y_46_im <= 3.4e+17:
		tmp = x_46_re / y_46_re
	else:
		tmp = x_46_im / y_46_im
	return tmp
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = 0.0
	if (y_46_im <= -7.7)
		tmp = Float64(x_46_im / y_46_im);
	elseif (y_46_im <= 3.4e+17)
		tmp = Float64(x_46_re / y_46_re);
	else
		tmp = Float64(x_46_im / y_46_im);
	end
	return tmp
end
function tmp_2 = code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = 0.0;
	if (y_46_im <= -7.7)
		tmp = x_46_im / y_46_im;
	elseif (y_46_im <= 3.4e+17)
		tmp = x_46_re / y_46_re;
	else
		tmp = x_46_im / y_46_im;
	end
	tmp_2 = tmp;
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := If[LessEqual[y$46$im, -7.7], N[(x$46$im / y$46$im), $MachinePrecision], If[LessEqual[y$46$im, 3.4e+17], N[(x$46$re / y$46$re), $MachinePrecision], N[(x$46$im / y$46$im), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y.im \leq -7.7:\\
\;\;\;\;\frac{x.im}{y.im}\\

\mathbf{elif}\;y.im \leq 3.4 \cdot 10^{+17}:\\
\;\;\;\;\frac{x.re}{y.re}\\

\mathbf{else}:\\
\;\;\;\;\frac{x.im}{y.im}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y.im < -7.70000000000000018 or 3.4e17 < y.im

    1. Initial program 49.1%

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Add Preprocessing
    3. Taylor expanded in y.im around inf

      \[\leadsto \color{blue}{\frac{x.im}{y.im}} \]
    4. Step-by-step derivation
      1. lower-/.f6469.6

        \[\leadsto \color{blue}{\frac{x.im}{y.im}} \]
    5. Applied rewrites69.6%

      \[\leadsto \color{blue}{\frac{x.im}{y.im}} \]

    if -7.70000000000000018 < y.im < 3.4e17

    1. Initial program 69.1%

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Add Preprocessing
    3. Taylor expanded in y.im around 0

      \[\leadsto \color{blue}{\frac{x.re}{y.re}} \]
    4. Step-by-step derivation
      1. lower-/.f6467.9

        \[\leadsto \color{blue}{\frac{x.re}{y.re}} \]
    5. Applied rewrites67.9%

      \[\leadsto \color{blue}{\frac{x.re}{y.re}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 42.8% accurate, 3.2× speedup?

\[\begin{array}{l} \\ \frac{x.im}{y.im} \end{array} \]
(FPCore (x.re x.im y.re y.im) :precision binary64 (/ x.im y.im))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return x_46_im / y_46_im;
}
real(8) function code(x_46re, x_46im, y_46re, y_46im)
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im
    real(8), intent (in) :: y_46re
    real(8), intent (in) :: y_46im
    code = x_46im / y_46im
end function
public static double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return x_46_im / y_46_im;
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	return x_46_im / y_46_im
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	return Float64(x_46_im / y_46_im)
end
function tmp = code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = x_46_im / y_46_im;
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(x$46$im / y$46$im), $MachinePrecision]
\begin{array}{l}

\\
\frac{x.im}{y.im}
\end{array}
Derivation
  1. Initial program 58.9%

    \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
  2. Add Preprocessing
  3. Taylor expanded in y.im around inf

    \[\leadsto \color{blue}{\frac{x.im}{y.im}} \]
  4. Step-by-step derivation
    1. lower-/.f6442.7

      \[\leadsto \color{blue}{\frac{x.im}{y.im}} \]
  5. Applied rewrites42.7%

    \[\leadsto \color{blue}{\frac{x.im}{y.im}} \]
  6. Add Preprocessing

Reproduce

?
herbie shell --seed 2024244 
(FPCore (x.re x.im y.re y.im)
  :name "_divideComplex, real part"
  :precision binary64
  (/ (+ (* x.re y.re) (* x.im y.im)) (+ (* y.re y.re) (* y.im y.im))))