Average Error: 26.4 → 10.4
Time: 9.4s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} \mathbf{if}\;c \leq -4.3216122432969205 \cdot 10^{+170}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, b, a\right) \cdot \frac{-1}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_0 := \frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{if}\;c \leq -9.690011489311247 \cdot 10^{-152}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.2661331500288663 \cdot 10^{-210}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{d}, \frac{a}{d}, \frac{b}{d}\right)\\ \mathbf{elif}\;c \leq 3.6305850563522157 \cdot 10^{+137}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\ \end{array}\\ \end{array} \]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \leq -4.3216122432969205 \cdot 10^{+170}:\\
\;\;\;\;\mathsf{fma}\left(\frac{d}{c}, b, a\right) \cdot \frac{-1}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_0 := \frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\
\mathbf{if}\;c \leq -9.690011489311247 \cdot 10^{-152}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;c \leq 1.2661331500288663 \cdot 10^{-210}:\\
\;\;\;\;\mathsf{fma}\left(\frac{c}{d}, \frac{a}{d}, \frac{b}{d}\right)\\

\mathbf{elif}\;c \leq 3.6305850563522157 \cdot 10^{+137}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\


\end{array}\\


\end{array}
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (if (<= c -4.3216122432969205e+170)
   (* (fma (/ d c) b a) (/ -1.0 (hypot d c)))
   (let* ((t_0 (/ (/ (fma a c (* d b)) (hypot d c)) (hypot d c))))
     (if (<= c -9.690011489311247e-152)
       t_0
       (if (<= c 1.2661331500288663e-210)
         (fma (/ c d) (/ a d) (/ b d))
         (if (<= c 3.6305850563522157e+137)
           t_0
           (fma (/ d c) (/ b c) (/ a c))))))))
double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
	double tmp;
	if (c <= -4.3216122432969205e+170) {
		tmp = fma((d / c), b, a) * (-1.0 / hypot(d, c));
	} else {
		double t_0 = (fma(a, c, (d * b)) / hypot(d, c)) / hypot(d, c);
		double tmp_1;
		if (c <= -9.690011489311247e-152) {
			tmp_1 = t_0;
		} else if (c <= 1.2661331500288663e-210) {
			tmp_1 = fma((c / d), (a / d), (b / d));
		} else if (c <= 3.6305850563522157e+137) {
			tmp_1 = t_0;
		} else {
			tmp_1 = fma((d / c), (b / c), (a / c));
		}
		tmp = tmp_1;
	}
	return tmp;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.4
Target0.4
Herbie10.4
\[\begin{array}{l} \mathbf{if}\;\left|d\right| < \left|c\right|:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array} \]

Derivation

  1. Split input into 4 regimes
  2. if c < -4.3216122432969205e170

    1. Initial program 43.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied add-sqr-sqrt_binary6443.3

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} \]
    3. Applied *-un-lft-identity_binary6443.3

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}} \]
    4. Applied times-frac_binary6443.3

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}} \]
    5. Simplified43.3

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(d, c\right)}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}} \]
    6. Simplified28.9

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \color{blue}{\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}} \]
    7. Taylor expanded in c around -inf 10.9

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \color{blue}{\left(-\left(a + \frac{d \cdot b}{c}\right)\right)} \]
    8. Simplified6.0

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \color{blue}{\left(-\mathsf{fma}\left(\frac{d}{c}, b, a\right)\right)} \]

    if -4.3216122432969205e170 < c < -9.690011489311247e-152 or 1.26613315002886632e-210 < c < 3.63058505635221573e137

    1. Initial program 19.2

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied add-sqr-sqrt_binary6419.2

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} \]
    3. Applied *-un-lft-identity_binary6419.2

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}} \]
    4. Applied times-frac_binary6419.3

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}} \]
    5. Simplified19.3

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(d, c\right)}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}} \]
    6. Simplified13.3

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \color{blue}{\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}} \]
    7. Applied associate-*l/_binary6413.1

      \[\leadsto \color{blue}{\frac{1 \cdot \frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}} \]
    8. Simplified13.1

      \[\leadsto \frac{\color{blue}{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}}{\mathsf{hypot}\left(d, c\right)} \]

    if -9.690011489311247e-152 < c < 1.26613315002886632e-210

    1. Initial program 25.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in c around 0 9.6

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}} \]
    3. Simplified7.1

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{c}{d}, \frac{a}{d}, \frac{b}{d}\right)} \]

    if 3.63058505635221573e137 < c

    1. Initial program 42.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in c around inf 15.4

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}} \]
    3. Simplified7.6

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification10.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -4.3216122432969205 \cdot 10^{+170}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, b, a\right) \cdot \frac{-1}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \leq -9.690011489311247 \cdot 10^{-152}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \leq 1.2661331500288663 \cdot 10^{-210}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{d}, \frac{a}{d}, \frac{b}{d}\right)\\ \mathbf{elif}\;c \leq 3.6305850563522157 \cdot 10^{+137}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\ \end{array} \]

Reproduce

herbie shell --seed 2021344 
(FPCore (a b c d)
  :name "Complex division, real part"
  :precision binary64

  :herbie-target
  (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d)))))

  (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))