Average Error: 25.0 → 10.0
Time: 6.3s
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 -1.2126922600481525 \cdot 10^{+99}:\\ \;\;\;\;\frac{-t_0}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq -4.171294695866961 \cdot 10^{-113}:\\ \;\;\;\;\begin{array}{l} t_1 := \mathsf{fma}\left(c, c, d \cdot d\right)\\ \mathsf{fma}\left(\frac{c}{t_1}, a, \frac{d \cdot b}{t_1}\right) \end{array}\\ \mathbf{elif}\;d \leq -1.657834802134664 \cdot 10^{-306}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\ \mathbf{elif}\;d \leq 4.637490032372509 \cdot 10^{+77}:\\ \;\;\;\;\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{c}{d}, a, b\right)\\
\mathbf{if}\;d \leq -1.2126922600481525 \cdot 10^{+99}:\\
\;\;\;\;\frac{-t_0}{\mathsf{hypot}\left(d, c\right)}\\

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

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

\mathbf{elif}\;d \leq 4.637490032372509 \cdot 10^{+77}:\\
\;\;\;\;\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 (/ c d) a b)))
   (if (<= d -1.2126922600481525e+99)
     (/ (- t_0) (hypot d c))
     (if (<= d -4.171294695866961e-113)
       (let* ((t_1 (fma c c (* d d)))) (fma (/ c t_1) a (/ (* d b) t_1)))
       (if (<= d -1.657834802134664e-306)
         (fma (/ d c) (/ b c) (/ a c))
         (if (<= d 4.637490032372509e+77)
           (/ (/ (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((c / d), a, b);
	double tmp;
	if (d <= -1.2126922600481525e+99) {
		tmp = -t_0 / hypot(d, c);
	} else if (d <= -4.171294695866961e-113) {
		double t_1 = fma(c, c, (d * d));
		tmp = fma((c / t_1), a, ((d * b) / t_1));
	} else if (d <= -1.657834802134664e-306) {
		tmp = fma((d / c), (b / c), (a / c));
	} else if (d <= 4.637490032372509e+77) {
		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

Original25.0
Target0.3
Herbie10.0
\[\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 d < -1.21269226004815246e99

    1. Initial program 38.7

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied add-sqr-sqrt_binary6438.7

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} \]
    3. Applied *-un-lft-identity_binary6438.7

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}} \]
    4. Applied times-frac_binary6438.7

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}} \]
    5. Simplified38.7

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(d, c\right)}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}} \]
    6. Simplified25.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)}} \]
    7. Applied associate-*l/_binary6424.9

      \[\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)}} \]
    8. Simplified24.9

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

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

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

    if -1.21269226004815246e99 < d < -4.17129469586696093e-113

    1. Initial program 15.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied add-sqr-sqrt_binary6415.1

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} \]
    3. Applied *-un-lft-identity_binary6415.1

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}} \]
    4. Applied times-frac_binary6415.1

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}} \]
    5. Simplified15.1

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(d, c\right)}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}} \]
    6. Simplified10.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)}} \]
    7. Applied add-sqr-sqrt_binary6410.6

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

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

      \[\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)} \]

    if -4.17129469586696093e-113 < d < -1.6578348021346639e-306

    1. Initial program 20.7

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in c around inf 12.3

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

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

    if -1.6578348021346639e-306 < d < 4.6374900323725094e77

    1. Initial program 16.2

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied add-sqr-sqrt_binary6416.2

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} \]
    3. Applied *-un-lft-identity_binary6416.2

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}} \]
    4. Applied times-frac_binary6416.2

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}} \]
    5. Simplified16.2

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(d, c\right)}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}} \]
    6. 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)}} \]
    7. Applied associate-*l/_binary649.5

      \[\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)}} \]
    8. Simplified9.5

      \[\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 4.6374900323725094e77 < d

    1. Initial program 39.5

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied add-sqr-sqrt_binary6439.5

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} \]
    3. Applied *-un-lft-identity_binary6439.5

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}} \]
    4. Applied times-frac_binary6439.5

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}} \]
    5. Simplified39.5

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(d, c\right)}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}} \]
    6. Simplified26.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)}} \]
    7. Applied associate-*l/_binary6426.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)}} \]
    8. Simplified26.3

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -1.2126922600481525 \cdot 10^{+99}:\\ \;\;\;\;\frac{-\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq -4.171294695866961 \cdot 10^{-113}:\\ \;\;\;\;\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)\\ \mathbf{elif}\;d \leq -1.657834802134664 \cdot 10^{-306}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\ \mathbf{elif}\;d \leq 4.637490032372509 \cdot 10^{+77}:\\ \;\;\;\;\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 2022088 
(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))))