Average Error: 25.7 → 10.2
Time: 7.2s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} \mathbf{if}\;d \leq -1.5835790079768928 \cdot 10^{+81}:\\ \;\;\;\;\frac{-\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_0 := \frac{\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{if}\;d \leq -4.914157784902541 \cdot 10^{-154}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 5.0503981640593735 \cdot 10^{-129}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 1.5945253184501813 \cdot 10^{+119}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\ \end{array}\\ \end{array} \]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \leq -1.5835790079768928 \cdot 10^{+81}:\\
\;\;\;\;\frac{-\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{\mathsf{hypot}\left(d, c\right)}\\

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

\mathbf{elif}\;d \leq 5.0503981640593735 \cdot 10^{-129}:\\
\;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\

\mathbf{elif}\;d \leq 1.5945253184501813 \cdot 10^{+119}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\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 (<= d -1.5835790079768928e+81)
   (/ (- (fma (/ c d) a b)) (hypot d c))
   (let* ((t_0 (/ (/ (fma d b (* c a)) (hypot d c)) (hypot d c))))
     (if (<= d -4.914157784902541e-154)
       t_0
       (if (<= d 5.0503981640593735e-129)
         (+ (/ a c) (/ (* d b) (pow c 2.0)))
         (if (<= d 1.5945253184501813e+119)
           t_0
           (fma (/ a d) (/ c d) (/ b d))))))))
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 (d <= -1.5835790079768928e+81) {
		tmp = -fma((c / d), a, b) / hypot(d, c);
	} else {
		double t_0 = (fma(d, b, (c * a)) / hypot(d, c)) / hypot(d, c);
		double tmp_1;
		if (d <= -4.914157784902541e-154) {
			tmp_1 = t_0;
		} else if (d <= 5.0503981640593735e-129) {
			tmp_1 = (a / c) + ((d * b) / pow(c, 2.0));
		} else if (d <= 1.5945253184501813e+119) {
			tmp_1 = t_0;
		} else {
			tmp_1 = fma((a / d), (c / d), (b / d));
		}
		tmp = tmp_1;
	}
	return tmp;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.7
Target0.5
Herbie10.2
\[\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 d < -1.58357900797689277e81

    1. Initial program 37.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified37.0

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Applied add-sqr-sqrt_binary6437.0

      \[\leadsto \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}} \]
    4. Applied *-un-lft-identity_binary6437.0

      \[\leadsto \frac{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    5. Applied times-frac_binary6437.0

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}} \]
    6. Simplified37.0

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

      \[\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)}} \]
    8. Applied associate-*l/_binary6425.0

      \[\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)}} \]
    9. Taylor expanded in d around -inf 13.5

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

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

    if -1.58357900797689277e81 < d < -4.91415778490254085e-154 or 5.05039816405937352e-129 < d < 1.59452531845018132e119

    1. Initial program 15.8

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified15.8

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Applied add-sqr-sqrt_binary6415.8

      \[\leadsto \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}} \]
    4. Applied add-cube-cbrt_binary6416.4

      \[\leadsto \frac{\color{blue}{\left(\sqrt[3]{\mathsf{fma}\left(a, c, b \cdot d\right)} \cdot \sqrt[3]{\mathsf{fma}\left(a, c, b \cdot d\right)}\right) \cdot \sqrt[3]{\mathsf{fma}\left(a, c, b \cdot d\right)}}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    5. Applied times-frac_binary6416.4

      \[\leadsto \color{blue}{\frac{\sqrt[3]{\mathsf{fma}\left(a, c, b \cdot d\right)} \cdot \sqrt[3]{\mathsf{fma}\left(a, c, b \cdot d\right)}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{\sqrt[3]{\mathsf{fma}\left(a, c, b \cdot d\right)}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}} \]
    6. Simplified16.4

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

      \[\leadsto \frac{\sqrt[3]{\mathsf{fma}\left(d, b, c \cdot a\right)} \cdot \sqrt[3]{\mathsf{fma}\left(d, b, c \cdot a\right)}}{\mathsf{hypot}\left(d, c\right)} \cdot \color{blue}{\frac{\sqrt[3]{\mathsf{fma}\left(d, b, c \cdot a\right)}}{\mathsf{hypot}\left(d, c\right)}} \]
    8. Applied associate-*l/_binary6411.6

      \[\leadsto \color{blue}{\frac{\left(\sqrt[3]{\mathsf{fma}\left(d, b, c \cdot a\right)} \cdot \sqrt[3]{\mathsf{fma}\left(d, b, c \cdot a\right)}\right) \cdot \frac{\sqrt[3]{\mathsf{fma}\left(d, b, c \cdot a\right)}}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}} \]
    9. Simplified10.7

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

    if -4.91415778490254085e-154 < d < 5.05039816405937352e-129

    1. Initial program 22.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified22.9

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Taylor expanded in c around inf 9.8

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

    if 1.59452531845018132e119 < d

    1. Initial program 41.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified41.0

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Applied add-sqr-sqrt_binary6441.0

      \[\leadsto \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}} \]
    4. Applied *-un-lft-identity_binary6441.0

      \[\leadsto \frac{\color{blue}{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    5. Applied times-frac_binary6441.0

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}} \]
    6. Simplified41.0

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

      \[\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)}} \]
    8. Taylor expanded in d around inf 16.2

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}} \]
    9. Simplified8.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -1.5835790079768928 \cdot 10^{+81}:\\ \;\;\;\;\frac{-\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq -4.914157784902541 \cdot 10^{-154}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq 5.0503981640593735 \cdot 10^{-129}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 1.5945253184501813 \cdot 10^{+119}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\ \end{array} \]

Reproduce

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