Average Error: 26.4 → 10.8
Time: 9.2s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ t_1 := \mathsf{fma}\left(d, d, c \cdot c\right)\\ t_2 := \mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\ t_3 := \mathsf{fma}\left(\frac{b}{c}, \frac{d}{c}, \frac{a}{c}\right)\\ \mathbf{if}\;d \leq -7.798094340522651 \cdot 10^{+148}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d \leq -3.4710805651394907 \cdot 10^{+37}:\\ \;\;\;\;\mathsf{fma}\left(b, \frac{d}{t_1}, \frac{a \cdot c}{t_1}\right)\\ \mathbf{elif}\;d \leq -4.34100723376406 \cdot 10^{+29}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;d \leq -3.840391229221447 \cdot 10^{-106}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.7980760572038457 \cdot 10^{-66}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;d \leq 6.164541131217644 \cdot 10^{+185}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\
t_1 := \mathsf{fma}\left(d, d, c \cdot c\right)\\
t_2 := \mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\
t_3 := \mathsf{fma}\left(\frac{b}{c}, \frac{d}{c}, \frac{a}{c}\right)\\
\mathbf{if}\;d \leq -7.798094340522651 \cdot 10^{+148}:\\
\;\;\;\;t_2\\

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

\mathbf{elif}\;d \leq -4.34100723376406 \cdot 10^{+29}:\\
\;\;\;\;t_3\\

\mathbf{elif}\;d \leq -3.840391229221447 \cdot 10^{-106}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d \leq 3.7980760572038457 \cdot 10^{-66}:\\
\;\;\;\;t_3\\

\mathbf{elif}\;d \leq 6.164541131217644 \cdot 10^{+185}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;t_2\\


\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 a (* d b)) (hypot d c)) (hypot d c)))
        (t_1 (fma d d (* c c)))
        (t_2 (fma (/ a d) (/ c d) (/ b d)))
        (t_3 (fma (/ b c) (/ d c) (/ a c))))
   (if (<= d -7.798094340522651e+148)
     t_2
     (if (<= d -3.4710805651394907e+37)
       (fma b (/ d t_1) (/ (* a c) t_1))
       (if (<= d -4.34100723376406e+29)
         t_3
         (if (<= d -3.840391229221447e-106)
           t_0
           (if (<= d 3.7980760572038457e-66)
             t_3
             (if (<= d 6.164541131217644e+185) t_0 t_2))))))))
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, a, (d * b)) / hypot(d, c)) / hypot(d, c);
	double t_1 = fma(d, d, (c * c));
	double t_2 = fma((a / d), (c / d), (b / d));
	double t_3 = fma((b / c), (d / c), (a / c));
	double tmp;
	if (d <= -7.798094340522651e+148) {
		tmp = t_2;
	} else if (d <= -3.4710805651394907e+37) {
		tmp = fma(b, (d / t_1), ((a * c) / t_1));
	} else if (d <= -4.34100723376406e+29) {
		tmp = t_3;
	} else if (d <= -3.840391229221447e-106) {
		tmp = t_0;
	} else if (d <= 3.7980760572038457e-66) {
		tmp = t_3;
	} else if (d <= 6.164541131217644e+185) {
		tmp = t_0;
	} else {
		tmp = t_2;
	}
	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.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 < -7.7980943405226511e148 or 6.16454113121764419e185 < d

    1. Initial program 44.2

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

      \[\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_binary6444.2

      \[\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_binary6444.2

      \[\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_binary6444.2

      \[\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. Simplified44.2

      \[\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. Simplified29.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. Taylor expanded in d around inf 14.9

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

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

    if -7.7980943405226511e148 < d < -3.47108056513949065e37

    1. Initial program 19.8

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified19.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 a around 0 19.9

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

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

    if -3.47108056513949065e37 < d < -4.3410072337640601e29 or -3.8403912292214472e-106 < d < 3.79807605720384573e-66

    1. Initial program 21.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified21.6

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

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

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

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

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

      \[\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 0 12.3

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

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

    if -4.3410072337640601e29 < d < -3.8403912292214472e-106 or 3.79807605720384573e-66 < d < 6.16454113121764419e185

    1. Initial program 20.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified20.3

      \[\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_binary6420.3

      \[\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_binary6420.3

      \[\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_binary6420.3

      \[\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. Simplified20.3

      \[\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. Simplified13.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 add-sqr-sqrt_binary6413.2

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\color{blue}{\sqrt{\mathsf{hypot}\left(d, c\right)} \cdot \sqrt{\mathsf{hypot}\left(d, c\right)}}} \]
    9. Applied *-un-lft-identity_binary6413.2

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{\color{blue}{1 \cdot \mathsf{fma}\left(d, b, c \cdot a\right)}}{\sqrt{\mathsf{hypot}\left(d, c\right)} \cdot \sqrt{\mathsf{hypot}\left(d, c\right)}} \]
    10. Applied times-frac_binary6413.3

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

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \left(\color{blue}{{\left(\frac{1}{\sqrt{\mathsf{hypot}\left(d, c\right)}}\right)}^{1}} \cdot {\left(\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\sqrt{\mathsf{hypot}\left(d, c\right)}}\right)}^{1}\right) \]
    13. Applied pow-prod-down_binary6413.3

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

      \[\leadsto \color{blue}{{\left(\frac{1}{\mathsf{hypot}\left(d, c\right)}\right)}^{1}} \cdot {\left(\frac{1}{\sqrt{\mathsf{hypot}\left(d, c\right)}} \cdot \frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\sqrt{\mathsf{hypot}\left(d, c\right)}}\right)}^{1} \]
    15. Applied pow-prod-down_binary6413.3

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -7.798094340522651 \cdot 10^{+148}:\\ \;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\ \mathbf{elif}\;d \leq -3.4710805651394907 \cdot 10^{+37}:\\ \;\;\;\;\mathsf{fma}\left(b, \frac{d}{\mathsf{fma}\left(d, d, c \cdot c\right)}, \frac{a \cdot c}{\mathsf{fma}\left(d, d, c \cdot c\right)}\right)\\ \mathbf{elif}\;d \leq -4.34100723376406 \cdot 10^{+29}:\\ \;\;\;\;\mathsf{fma}\left(\frac{b}{c}, \frac{d}{c}, \frac{a}{c}\right)\\ \mathbf{elif}\;d \leq -3.840391229221447 \cdot 10^{-106}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq 3.7980760572038457 \cdot 10^{-66}:\\ \;\;\;\;\mathsf{fma}\left(\frac{b}{c}, \frac{d}{c}, \frac{a}{c}\right)\\ \mathbf{elif}\;d \leq 6.164541131217644 \cdot 10^{+185}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\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 2022125 
(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))))