Average Error: 26.7 → 10.8
Time: 7.6s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \mathsf{fma}\left(\frac{c}{d}, a, b\right)\\ \mathbf{if}\;d \leq -9.393577737026942 \cdot 10^{+45}:\\ \;\;\;\;\frac{-t_0}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_1 := \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}\;d \leq -3.099134525215515 \cdot 10^{-142}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 2.88893887034297 \cdot 10^{-173}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\ \mathbf{elif}\;d \leq 2.0670348214171683 \cdot 10^{+97}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\\ \end{array} \]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \mathsf{fma}\left(\frac{c}{d}, a, b\right)\\
\mathbf{if}\;d \leq -9.393577737026942 \cdot 10^{+45}:\\
\;\;\;\;\frac{-t_0}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_1 := \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}\;d \leq -3.099134525215515 \cdot 10^{-142}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d \leq 2.88893887034297 \cdot 10^{-173}:\\
\;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\

\mathbf{elif}\;d \leq 2.0670348214171683 \cdot 10^{+97}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;\frac{t_0}{\mathsf{hypot}\left(d, 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
 (let* ((t_0 (fma (/ c d) a b)))
   (if (<= d -9.393577737026942e+45)
     (/ (- t_0) (hypot d c))
     (let* ((t_1 (/ (/ (fma a c (* d b)) (hypot d c)) (hypot d c))))
       (if (<= d -3.099134525215515e-142)
         t_1
         (if (<= d 2.88893887034297e-173)
           (+ (/ a c) (/ (* d b) (* c c)))
           (if (<= d 2.0670348214171683e+97) t_1 (/ t_0 (hypot d 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 t_0 = fma((c / d), a, b);
	double tmp;
	if (d <= -9.393577737026942e+45) {
		tmp = -t_0 / hypot(d, c);
	} else {
		double t_1 = (fma(a, c, (d * b)) / hypot(d, c)) / hypot(d, c);
		double tmp_1;
		if (d <= -3.099134525215515e-142) {
			tmp_1 = t_1;
		} else if (d <= 2.88893887034297e-173) {
			tmp_1 = (a / c) + ((d * b) / (c * c));
		} else if (d <= 2.0670348214171683e+97) {
			tmp_1 = t_1;
		} else {
			tmp_1 = t_0 / hypot(d, 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.7
Target0.4
Herbie10.8
\[\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 < -9.39357773702694194e45

    1. Initial program 35.5

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified35.5

      \[\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_binary6435.5

      \[\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_binary6435.5

      \[\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_binary6435.5

      \[\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. Simplified35.5

      \[\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. Simplified23.8

      \[\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/_binary6423.8

      \[\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. Simplified23.8

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

      \[\leadsto \frac{\color{blue}{-\left(\frac{c \cdot a}{d} + b\right)}}{\mathsf{hypot}\left(d, c\right)} \]
    11. Simplified12.4

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

    if -9.39357773702694194e45 < d < -3.0991345252155149e-142 or 2.8889388703429699e-173 < d < 2.0670348214171683e97

    1. Initial program 16.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified16.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_binary6416.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_binary6416.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_binary6416.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. Simplified16.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. Simplified10.8

      \[\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/_binary6410.7

      \[\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. Simplified10.7

      \[\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)} \]
    10. Applied pow1_binary6410.7

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

    if -3.0991345252155149e-142 < d < 2.8889388703429699e-173

    1. Initial program 24.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified24.1

      \[\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_binary6424.1

      \[\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_binary6424.6

      \[\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_binary6424.6

      \[\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. Simplified24.5

      \[\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. Simplified14.0

      \[\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. Taylor expanded in d around 0 9.8

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

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

    if 2.0670348214171683e97 < d

    1. Initial program 40.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified40.4

      \[\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_binary6440.4

      \[\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_binary6440.4

      \[\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_binary6440.4

      \[\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. Simplified40.4

      \[\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. Simplified28.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/_binary6428.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. Simplified28.0

      \[\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)} \]
    10. Taylor expanded in c around 0 14.2

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -9.393577737026942 \cdot 10^{+45}:\\ \;\;\;\;\frac{-\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq -3.099134525215515 \cdot 10^{-142}:\\ \;\;\;\;\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}\;d \leq 2.88893887034297 \cdot 10^{-173}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\ \mathbf{elif}\;d \leq 2.0670348214171683 \cdot 10^{+97}:\\ \;\;\;\;\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}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{\mathsf{hypot}\left(d, c\right)}\\ \end{array} \]

Reproduce

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