Average Error: 26.0 → 6.2
Time: 7.4s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\ \mathbf{if}\;c \leq -2.1315939500394346 \cdot 10^{+159}:\\ \;\;\;\;t_0 \cdot \frac{-1}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_1 := \mathsf{fma}\left(\frac{c}{\mathsf{fma}\left(c, c, d \cdot d\right)}, a, \frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\right)\\ \mathbf{if}\;c \leq -3.072242908321757 \cdot 10^{-142}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 4.87134912457176 \cdot 10^{-174}:\\ \;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\ \mathbf{elif}\;c \leq 2.1546967477784777 \cdot 10^{+122}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot t_0\\ \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{d}{c}, b, a\right)\\
\mathbf{if}\;c \leq -2.1315939500394346 \cdot 10^{+159}:\\
\;\;\;\;t_0 \cdot \frac{-1}{\mathsf{hypot}\left(d, c\right)}\\

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

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

\mathbf{elif}\;c \leq 2.1546967477784777 \cdot 10^{+122}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot t_0\\


\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 (/ d c) b a)))
   (if (<= c -2.1315939500394346e+159)
     (* t_0 (/ -1.0 (hypot d c)))
     (let* ((t_1
             (fma
              (/ c (fma c c (* d d)))
              a
              (* (/ d (hypot d c)) (/ b (hypot d c))))))
       (if (<= c -3.072242908321757e-142)
         t_1
         (if (<= c 4.87134912457176e-174)
           (fma (/ a d) (/ c d) (/ b d))
           (if (<= c 2.1546967477784777e+122)
             t_1
             (* (/ 1.0 (hypot d c)) t_0))))))))
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((d / c), b, a);
	double tmp;
	if (c <= -2.1315939500394346e+159) {
		tmp = t_0 * (-1.0 / hypot(d, c));
	} else {
		double t_1 = fma((c / fma(c, c, (d * d))), a, ((d / hypot(d, c)) * (b / hypot(d, c))));
		double tmp_1;
		if (c <= -3.072242908321757e-142) {
			tmp_1 = t_1;
		} else if (c <= 4.87134912457176e-174) {
			tmp_1 = fma((a / d), (c / d), (b / d));
		} else if (c <= 2.1546967477784777e+122) {
			tmp_1 = t_1;
		} else {
			tmp_1 = (1.0 / hypot(d, c)) * t_0;
		}
		tmp = tmp_1;
	}
	return tmp;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.0
Target0.4
Herbie6.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 c < -2.1315939500394346e159

    1. Initial program 42.8

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified42.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_binary6442.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 *-un-lft-identity_binary6442.8

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

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

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

      \[\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 c around -inf 11.5

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \color{blue}{\left(-\left(a + \frac{d \cdot b}{c}\right)\right)} \]
    9. Simplified7.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 -2.1315939500394346e159 < c < -3.07224290832175711e-142 or 4.87134912457176006e-174 < c < 2.15469674777847772e122

    1. Initial program 17.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified17.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_binary6417.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 *-un-lft-identity_binary6417.1

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

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

      \[\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. Simplified11.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 add-sqr-sqrt_binary6412.0

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

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

      \[\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 associate-*r*_binary6414.0

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

      \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{\mathsf{hypot}\left(d, c\right)}}}{\mathsf{hypot}\left(d, c\right)}} \cdot \frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\sqrt{\mathsf{hypot}\left(d, c\right)}} \]
    13. Taylor expanded in b around 0 17.1

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

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

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

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

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

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

    if -3.07224290832175711e-142 < c < 4.87134912457176006e-174

    1. Initial program 24.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified24.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_binary6424.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_binary6424.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_binary6424.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. Simplified24.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. Simplified12.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 10.0

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

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

    if 2.15469674777847772e122 < c

    1. Initial program 42.7

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -2.1315939500394346 \cdot 10^{+159}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, b, a\right) \cdot \frac{-1}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \leq -3.072242908321757 \cdot 10^{-142}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\mathsf{fma}\left(c, c, d \cdot d\right)}, a, \frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\right)\\ \mathbf{elif}\;c \leq 4.87134912457176 \cdot 10^{-174}:\\ \;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\ \mathbf{elif}\;c \leq 2.1546967477784777 \cdot 10^{+122}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\mathsf{fma}\left(c, c, d \cdot d\right)}, a, \frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\ \end{array} \]

Reproduce

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