Average Error: 26.4 → 10.4
Time: 4.8s
Precision: binary64
\[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
\[\begin{array}{l} t_0 := \frac{\frac{\mathsf{fma}\left(x.re, y.re, y.im \cdot x.im\right)}{\mathsf{hypot}\left(y.re, y.im\right)}}{\mathsf{hypot}\left(y.re, y.im\right)}\\ \mathbf{if}\;y.re \leq -9.705756685731516 \cdot 10^{+151}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y.im}{y.re}, x.im, x.re\right) \cdot \frac{-1}{\mathsf{hypot}\left(y.re, y.im\right)}\\ \mathbf{elif}\;y.re \leq -4.084260136662665 \cdot 10^{-238}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y.re \leq 4.5110647824345223 \cdot 10^{-212}:\\ \;\;\;\;\mathsf{fma}\left(\frac{x.re}{y.im}, \frac{y.re}{y.im}, \frac{x.im}{y.im}\right)\\ \mathbf{elif}\;y.re \leq 3.408225542871235 \cdot 10^{+180}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y.im}{y.re}, \frac{x.im}{y.re}, \frac{x.re}{y.re}\right)\\ \end{array} \]
\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im}
\begin{array}{l}
t_0 := \frac{\frac{\mathsf{fma}\left(x.re, y.re, y.im \cdot x.im\right)}{\mathsf{hypot}\left(y.re, y.im\right)}}{\mathsf{hypot}\left(y.re, y.im\right)}\\
\mathbf{if}\;y.re \leq -9.705756685731516 \cdot 10^{+151}:\\
\;\;\;\;\mathsf{fma}\left(\frac{y.im}{y.re}, x.im, x.re\right) \cdot \frac{-1}{\mathsf{hypot}\left(y.re, y.im\right)}\\

\mathbf{elif}\;y.re \leq -4.084260136662665 \cdot 10^{-238}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;y.re \leq 3.408225542871235 \cdot 10^{+180}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{y.im}{y.re}, \frac{x.im}{y.re}, \frac{x.re}{y.re}\right)\\


\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))))
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (let* ((t_0
         (/
          (/ (fma x.re y.re (* y.im x.im)) (hypot y.re y.im))
          (hypot y.re y.im))))
   (if (<= y.re -9.705756685731516e+151)
     (* (fma (/ y.im y.re) x.im x.re) (/ -1.0 (hypot y.re y.im)))
     (if (<= y.re -4.084260136662665e-238)
       t_0
       (if (<= y.re 4.5110647824345223e-212)
         (fma (/ x.re y.im) (/ y.re y.im) (/ x.im y.im))
         (if (<= y.re 3.408225542871235e+180)
           t_0
           (fma (/ y.im y.re) (/ x.im y.re) (/ x.re y.re))))))))
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));
}
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double t_0 = (fma(x_46_re, y_46_re, (y_46_im * x_46_im)) / hypot(y_46_re, y_46_im)) / hypot(y_46_re, y_46_im);
	double tmp;
	if (y_46_re <= -9.705756685731516e+151) {
		tmp = fma((y_46_im / y_46_re), x_46_im, x_46_re) * (-1.0 / hypot(y_46_re, y_46_im));
	} else if (y_46_re <= -4.084260136662665e-238) {
		tmp = t_0;
	} else if (y_46_re <= 4.5110647824345223e-212) {
		tmp = fma((x_46_re / y_46_im), (y_46_re / y_46_im), (x_46_im / y_46_im));
	} else if (y_46_re <= 3.408225542871235e+180) {
		tmp = t_0;
	} else {
		tmp = fma((y_46_im / y_46_re), (x_46_im / y_46_re), (x_46_re / y_46_re));
	}
	return tmp;
}

Error

Bits error versus x.re

Bits error versus x.im

Bits error versus y.re

Bits error versus y.im

Derivation

  1. Split input into 4 regimes
  2. if y.re < -9.7057566857315155e151

    1. Initial program 44.9

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Simplified44.9

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

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(y.re, y.im\right)} \cdot \frac{\mathsf{fma}\left(x.re, y.re, x.im \cdot y.im\right)}{\mathsf{hypot}\left(y.re, y.im\right)}} \]
    4. Taylor expanded in y.re around -inf 11.3

      \[\leadsto \frac{1}{\mathsf{hypot}\left(y.re, y.im\right)} \cdot \color{blue}{\left(-\left(x.re + \frac{y.im \cdot x.im}{y.re}\right)\right)} \]
    5. Simplified7.4

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

    if -9.7057566857315155e151 < y.re < -4.08426013666266484e-238 or 4.51106478243452233e-212 < y.re < 3.408225542871235e180

    1. Initial program 19.4

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Simplified19.4

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

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(y.re, y.im\right)} \cdot \frac{\mathsf{fma}\left(x.re, y.re, x.im \cdot y.im\right)}{\mathsf{hypot}\left(y.re, y.im\right)}} \]
    4. Applied egg-rr12.4

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

    if -4.08426013666266484e-238 < y.re < 4.51106478243452233e-212

    1. Initial program 23.3

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Simplified23.3

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

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(y.re, y.im\right)} \cdot \frac{\mathsf{fma}\left(x.re, y.re, x.im \cdot y.im\right)}{\mathsf{hypot}\left(y.re, y.im\right)}} \]
    4. Taylor expanded in y.re around 0 9.2

      \[\leadsto \color{blue}{\frac{x.re \cdot y.re}{{y.im}^{2}} + \frac{x.im}{y.im}} \]
    5. Simplified6.2

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

    if 3.408225542871235e180 < y.re

    1. Initial program 45.9

      \[\frac{x.re \cdot y.re + x.im \cdot y.im}{y.re \cdot y.re + y.im \cdot y.im} \]
    2. Simplified45.9

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

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(y.re, y.im\right)} \cdot \frac{\mathsf{fma}\left(x.re, y.re, x.im \cdot y.im\right)}{\mathsf{hypot}\left(y.re, y.im\right)}} \]
    4. Taylor expanded in y.re around inf 14.8

      \[\leadsto \color{blue}{\frac{y.im \cdot x.im}{{y.re}^{2}} + \frac{x.re}{y.re}} \]
    5. Simplified6.8

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y.re \leq -9.705756685731516 \cdot 10^{+151}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y.im}{y.re}, x.im, x.re\right) \cdot \frac{-1}{\mathsf{hypot}\left(y.re, y.im\right)}\\ \mathbf{elif}\;y.re \leq -4.084260136662665 \cdot 10^{-238}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(x.re, y.re, y.im \cdot x.im\right)}{\mathsf{hypot}\left(y.re, y.im\right)}}{\mathsf{hypot}\left(y.re, y.im\right)}\\ \mathbf{elif}\;y.re \leq 4.5110647824345223 \cdot 10^{-212}:\\ \;\;\;\;\mathsf{fma}\left(\frac{x.re}{y.im}, \frac{y.re}{y.im}, \frac{x.im}{y.im}\right)\\ \mathbf{elif}\;y.re \leq 3.408225542871235 \cdot 10^{+180}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(x.re, y.re, y.im \cdot x.im\right)}{\mathsf{hypot}\left(y.re, y.im\right)}}{\mathsf{hypot}\left(y.re, y.im\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y.im}{y.re}, \frac{x.im}{y.re}, \frac{x.re}{y.re}\right)\\ \end{array} \]

Reproduce

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