Average Error: 24.8 → 12.1
Time: 4.6s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} \mathbf{if}\;d \leq -4.331249049106995 \cdot 10^{+81}:\\ \;\;\;\;\frac{a - \frac{c \cdot b}{d}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_0 := \frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{if}\;d \leq -5.787984791490119 \cdot 10^{-300}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 8.114742458283298 \cdot 10^{-103}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\ \mathbf{elif}\;d \leq 2.8401337548471847 \cdot 10^{+128}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{\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}\;d \leq -4.331249049106995 \cdot 10^{+81}:\\
\;\;\;\;\frac{a - \frac{c \cdot b}{d}}{\mathsf{hypot}\left(d, c\right)}\\

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

\mathbf{elif}\;d \leq 8.114742458283298 \cdot 10^{-103}:\\
\;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\

\mathbf{elif}\;d \leq 2.8401337548471847 \cdot 10^{+128}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\frac{-a}{\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 (<= d -4.331249049106995e+81)
   (/ (- a (/ (* c b) d)) (hypot d c))
   (let* ((t_0 (/ (/ (- (* c b) (* d a)) (hypot d c)) (hypot d c))))
     (if (<= d -5.787984791490119e-300)
       t_0
       (if (<= d 8.114742458283298e-103)
         (- (/ b c) (/ (* d a) (* c c)))
         (if (<= d 2.8401337548471847e+128) t_0 (/ (- a) (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 (d <= -4.331249049106995e+81) {
		tmp = (a - ((c * b) / d)) / hypot(d, c);
	} else {
		double t_0 = (((c * b) - (d * a)) / hypot(d, c)) / hypot(d, c);
		double tmp_1;
		if (d <= -5.787984791490119e-300) {
			tmp_1 = t_0;
		} else if (d <= 8.114742458283298e-103) {
			tmp_1 = (b / c) - ((d * a) / (c * c));
		} else if (d <= 2.8401337548471847e+128) {
			tmp_1 = t_0;
		} else {
			tmp_1 = -a / 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

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original24.8
Target0.5
Herbie12.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 4 regimes
  2. if d < -4.3312490491069954e81

    1. Initial program 38.2

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

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

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

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

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

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

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

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

      \[\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 d around -inf 12.3

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

    if -4.3312490491069954e81 < d < -5.7879847914901187e-300 or 8.11474245828329776e-103 < d < 2.8401337548471847e128

    1. Initial program 16.3

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

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

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

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

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

      \[\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. Simplified10.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. Applied associate-*l/_binary6410.5

      \[\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. Simplified10.5

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

    if -5.7879847914901187e-300 < d < 8.11474245828329776e-103

    1. Initial program 18.9

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

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

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

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

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

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

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

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

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

    if 2.8401337548471847e128 < d

    1. Initial program 43.4

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -4.331249049106995 \cdot 10^{+81}:\\ \;\;\;\;\frac{a - \frac{c \cdot b}{d}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq -5.787984791490119 \cdot 10^{-300}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq 8.114742458283298 \cdot 10^{-103}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\ \mathbf{elif}\;d \leq 2.8401337548471847 \cdot 10^{+128}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \end{array} \]

Reproduce

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