Average Error: 25.9 → 13.1
Time: 6.8s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} \mathbf{if}\;c \leq -5.4169787017492545 \cdot 10^{+162}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_0 := c \cdot b - d \cdot a\\ t_1 := \frac{\frac{t_0}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{if}\;c \leq -2.1677196728423193 \cdot 10^{-219}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 3.987912938734293 \cdot 10^{-250}:\\ \;\;\;\;\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 4.047882704981183 \cdot 10^{-149}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{t_0}}}}\\ \mathbf{elif}\;c \leq 2.788697051011863 \cdot 10^{-72}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{d}, \frac{b}{d}, \frac{a \cdot \left(c \cdot c\right)}{{d}^{3}}\right) - \frac{a}{d}\\ \mathbf{elif}\;c \leq 1.3632044865862293 \cdot 10^{+39}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{b - \frac{d \cdot a}{c}}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\\ \end{array} \]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \leq -5.4169787017492545 \cdot 10^{+162}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\

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

\mathbf{elif}\;c \leq 3.987912938734293 \cdot 10^{-250}:\\
\;\;\;\;\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}\\

\mathbf{elif}\;c \leq 4.047882704981183 \cdot 10^{-149}:\\
\;\;\;\;\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{t_0}}}}\\

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

\mathbf{elif}\;c \leq 1.3632044865862293 \cdot 10^{+39}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;\frac{b - \frac{d \cdot a}{c}}{\mathsf{hypot}\left(d, c\right)}\\


\end{array}\\


\end{array}
(FPCore (a b c d)
 :precision binary64
 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (if (<= c -5.4169787017492545e+162)
   (/ (- b) (hypot d c))
   (let* ((t_0 (- (* c b) (* d a))) (t_1 (/ (/ t_0 (hypot d c)) (hypot d c))))
     (if (<= c -2.1677196728423193e-219)
       t_1
       (if (<= c 3.987912938734293e-250)
         (- (/ (* c b) (pow d 2.0)) (/ a d))
         (if (<= c 4.047882704981183e-149)
           (/ 1.0 (/ (hypot d c) (/ 1.0 (/ (hypot d c) t_0))))
           (if (<= c 2.788697051011863e-72)
             (- (fma (/ c d) (/ b d) (/ (* a (* c c)) (pow d 3.0))) (/ a d))
             (if (<= c 1.3632044865862293e+39)
               t_1
               (/ (- b (/ (* d a) c)) (hypot d c))))))))))
double code(double a, double b, double c, double d) {
	return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
	double tmp;
	if (c <= -5.4169787017492545e+162) {
		tmp = -b / hypot(d, c);
	} else {
		double t_0 = (c * b) - (d * a);
		double t_1 = (t_0 / hypot(d, c)) / hypot(d, c);
		double tmp_1;
		if (c <= -2.1677196728423193e-219) {
			tmp_1 = t_1;
		} else if (c <= 3.987912938734293e-250) {
			tmp_1 = ((c * b) / pow(d, 2.0)) - (a / d);
		} else if (c <= 4.047882704981183e-149) {
			tmp_1 = 1.0 / (hypot(d, c) / (1.0 / (hypot(d, c) / t_0)));
		} else if (c <= 2.788697051011863e-72) {
			tmp_1 = fma((c / d), (b / d), ((a * (c * c)) / pow(d, 3.0))) - (a / d);
		} else if (c <= 1.3632044865862293e+39) {
			tmp_1 = t_1;
		} else {
			tmp_1 = (b - ((d * a) / c)) / 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

Original25.9
Target0.5
Herbie13.1
\[\begin{array}{l} \mathbf{if}\;\left|d\right| < \left|c\right|:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-a\right) + b \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array} \]

Derivation

  1. Split input into 6 regimes
  2. if c < -5.41697870174925451e162

    1. Initial program 43.5

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

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\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_binary6443.5

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \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_binary6443.5

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}} \]
    6. Simplified43.5

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \color{blue}{\frac{c \cdot b - a \cdot d}{\mathsf{hypot}\left(d, c\right)}} \]
    8. Applied associate-*l/_binary6428.6

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

      \[\leadsto \frac{\color{blue}{\frac{c \cdot b - a \cdot d}{\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}{-1 \cdot b}}{\mathsf{hypot}\left(d, c\right)} \]

    if -5.41697870174925451e162 < c < -2.16771967284231934e-219 or 2.7886970510118628e-72 < c < 1.3632044865862293e39

    1. Initial program 18.1

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

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\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_binary6418.1

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \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_binary6418.1

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}} \]
    6. Simplified18.1

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \color{blue}{\frac{c \cdot b - a \cdot d}{\mathsf{hypot}\left(d, c\right)}} \]
    8. Applied associate-*l/_binary6411.9

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

      \[\leadsto \frac{\color{blue}{\frac{c \cdot b - a \cdot d}{\mathsf{hypot}\left(d, c\right)}}}{\mathsf{hypot}\left(d, c\right)} \]
    10. Applied clear-num_binary6412.0

      \[\leadsto \frac{\color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{c \cdot b - a \cdot d}}}}{\mathsf{hypot}\left(d, c\right)} \]
    11. Applied *-un-lft-identity_binary6412.0

      \[\leadsto \frac{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{c \cdot b - a \cdot d}}}{\color{blue}{1 \cdot \mathsf{hypot}\left(d, c\right)}} \]
    12. Applied associate-/r*_binary6412.0

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

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

    if -2.16771967284231934e-219 < c < 3.98791293873429297e-250

    1. Initial program 22.7

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

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\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_binary6422.7

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \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_binary6422.7

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}} \]
    6. Simplified22.7

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

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

      \[\leadsto \color{blue}{\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}} \]

    if 3.98791293873429297e-250 < c < 4.047882704981183e-149

    1. Initial program 23.6

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

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\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_binary6423.6

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \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_binary6423.6

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}} \]
    6. Simplified23.6

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

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

      \[\leadsto \frac{\color{blue}{\frac{c \cdot b - a \cdot d}{\mathsf{hypot}\left(d, c\right)}}}{\mathsf{hypot}\left(d, c\right)} \]
    10. Applied clear-num_binary6411.7

      \[\leadsto \frac{\color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{c \cdot b - a \cdot d}}}}{\mathsf{hypot}\left(d, c\right)} \]
    11. Applied *-un-lft-identity_binary6411.7

      \[\leadsto \frac{\frac{1}{\color{blue}{1 \cdot \frac{\mathsf{hypot}\left(d, c\right)}{c \cdot b - a \cdot d}}}}{\mathsf{hypot}\left(d, c\right)} \]
    12. Applied *-un-lft-identity_binary6411.7

      \[\leadsto \frac{\frac{\color{blue}{1 \cdot 1}}{1 \cdot \frac{\mathsf{hypot}\left(d, c\right)}{c \cdot b - a \cdot d}}}{\mathsf{hypot}\left(d, c\right)} \]
    13. Applied times-frac_binary6411.7

      \[\leadsto \frac{\color{blue}{\frac{1}{1} \cdot \frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{c \cdot b - a \cdot d}}}}{\mathsf{hypot}\left(d, c\right)} \]
    14. Applied associate-/l*_binary6412.1

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

    if 4.047882704981183e-149 < c < 2.7886970510118628e-72

    1. Initial program 15.4

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

      \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
    3. Taylor expanded in c around 0 21.1

      \[\leadsto \color{blue}{\left(\frac{c \cdot b}{{d}^{2}} + \frac{{c}^{2} \cdot a}{{d}^{3}}\right) - \frac{a}{d}} \]
    4. Simplified20.4

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

    if 1.3632044865862293e39 < c

    1. Initial program 34.8

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified34.8

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\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_binary6434.8

      \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \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_binary6434.8

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}} \]
    6. Simplified34.8

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(d, c\right)} \cdot \color{blue}{\frac{c \cdot b - a \cdot d}{\mathsf{hypot}\left(d, c\right)}} \]
    8. Applied associate-*l/_binary6423.9

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

      \[\leadsto \frac{\color{blue}{\frac{c \cdot b - a \cdot d}{\mathsf{hypot}\left(d, c\right)}}}{\mathsf{hypot}\left(d, c\right)} \]
    10. Taylor expanded in c around inf 16.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -5.4169787017492545 \cdot 10^{+162}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \leq -2.1677196728423193 \cdot 10^{-219}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \leq 3.987912938734293 \cdot 10^{-250}:\\ \;\;\;\;\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 4.047882704981183 \cdot 10^{-149}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{\frac{1}{\frac{\mathsf{hypot}\left(d, c\right)}{c \cdot b - d \cdot a}}}}\\ \mathbf{elif}\;c \leq 2.788697051011863 \cdot 10^{-72}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{d}, \frac{b}{d}, \frac{a \cdot \left(c \cdot c\right)}{{d}^{3}}\right) - \frac{a}{d}\\ \mathbf{elif}\;c \leq 1.3632044865862293 \cdot 10^{+39}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - \frac{d \cdot a}{c}}{\mathsf{hypot}\left(d, c\right)}\\ \end{array} \]

Reproduce

herbie shell --seed 2022101 
(FPCore (a b c d)
  :name "Complex division, imag part"
  :precision binary64

  :herbie-target
  (if (< (fabs d) (fabs c)) (/ (- b (* a (/ d c))) (+ c (* d (/ d c)))) (/ (+ (- a) (* b (/ c d))) (+ d (* c (/ c d)))))

  (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))