Average Error: 24.6 → 9.8
Time: 11.7s
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.1840157650846506 \cdot 10^{+101}:\\ \;\;\;\;\frac{-t_0}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \leq -3.6283479029046922 \cdot 10^{-140}:\\ \;\;\;\;\begin{array}{l} t_1 := \mathsf{fma}\left(d, d, c \cdot c\right)\\ \mathsf{fma}\left(b, \frac{d}{t_1}, \frac{c \cdot a}{t_1}\right) \end{array}\\ \mathbf{elif}\;c \leq 8.272481472784187 \cdot 10^{-130}:\\ \;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\ \mathbf{elif}\;c \leq 1.7908903707439708 \cdot 10^{+98}:\\ \;\;\;\;\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{t_0}{\mathsf{hypot}\left(d, c\right)}\\ \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.1840157650846506 \cdot 10^{+101}:\\
\;\;\;\;\frac{-t_0}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;c \leq -3.6283479029046922 \cdot 10^{-140}:\\
\;\;\;\;\begin{array}{l}
t_1 := \mathsf{fma}\left(d, d, c \cdot c\right)\\
\mathsf{fma}\left(b, \frac{d}{t_1}, \frac{c \cdot a}{t_1}\right)
\end{array}\\

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

\mathbf{elif}\;c \leq 1.7908903707439708 \cdot 10^{+98}:\\
\;\;\;\;\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{t_0}{\mathsf{hypot}\left(d, c\right)}\\


\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.1840157650846506e+101)
     (/ (- t_0) (hypot d c))
     (if (<= c -3.6283479029046922e-140)
       (let* ((t_1 (fma d d (* c c)))) (fma b (/ d t_1) (/ (* c a) t_1)))
       (if (<= c 8.272481472784187e-130)
         (fma (/ a d) (/ c d) (/ b d))
         (if (<= c 1.7908903707439708e+98)
           (/ (/ (fma a c (* d b)) (hypot d c)) (hypot d c))
           (/ 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((d / c), b, a);
	double tmp;
	if (c <= -2.1840157650846506e+101) {
		tmp = -t_0 / hypot(d, c);
	} else if (c <= -3.6283479029046922e-140) {
		double t_1 = fma(d, d, (c * c));
		tmp = fma(b, (d / t_1), ((c * a) / t_1));
	} else if (c <= 8.272481472784187e-130) {
		tmp = fma((a / d), (c / d), (b / d));
	} else if (c <= 1.7908903707439708e+98) {
		tmp = (fma(a, c, (d * b)) / hypot(d, c)) / hypot(d, c);
	} else {
		tmp = t_0 / hypot(d, c);
	}
	return tmp;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original24.6
Target0.4
Herbie9.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 5 regimes
  2. if c < -2.18401576508465057e101

    1. Initial program 39.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified39.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_binary6439.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_binary6439.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_binary6439.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. Simplified39.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. Simplified26.2

      \[\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/_binary6426.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)}} \]
    9. Simplified26.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)} \]
    10. Taylor expanded in c around -inf 12.9

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

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

    if -2.18401576508465057e101 < c < -3.6283479029046922e-140

    1. Initial program 15.2

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

      \[\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 15.2

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

      \[\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.6283479029046922e-140 < c < 8.272481472784187e-130

    1. Initial program 19.7

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

      \[\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 11.3

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

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

    if 8.272481472784187e-130 < c < 1.79089037074397082e98

    1. Initial program 14.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified14.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_binary6414.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_binary6414.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_binary6414.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. Simplified14.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. Simplified9.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. Applied associate-*l/_binary649.4

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

      \[\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 1.79089037074397082e98 < c

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

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

      \[\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 inf 13.4

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -2.1840157650846506 \cdot 10^{+101}:\\ \;\;\;\;\frac{-\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \leq -3.6283479029046922 \cdot 10^{-140}:\\ \;\;\;\;\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)\\ \mathbf{elif}\;c \leq 8.272481472784187 \cdot 10^{-130}:\\ \;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\ \mathbf{elif}\;c \leq 1.7908903707439708 \cdot 10^{+98}:\\ \;\;\;\;\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{d}{c}, b, a\right)}{\mathsf{hypot}\left(d, c\right)}\\ \end{array} \]

Reproduce

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