Average Error: 26.0 → 14.1
Time: 7.4s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\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.124136587792355 \cdot 10^{+226}:\\ \;\;\;\;\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}\;d \leq -1.9738142544677371 \cdot 10^{-143}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -9.658402922807776 \cdot 10^{-195}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\ \mathbf{elif}\;d \leq 3.0970906241597073 \cdot 10^{+36}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{\mathsf{hypot}\left(d, c\right)}\\ \end{array} \]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\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.124136587792355 \cdot 10^{+226}:\\
\;\;\;\;\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}\;d \leq -1.9738142544677371 \cdot 10^{-143}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;d \leq 3.0970906241597073 \cdot 10^{+36}:\\
\;\;\;\;t_0\\

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


\end{array}
(FPCore (a b c d)
 :precision binary64
 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ (/ (- (* c b) (* d a)) (hypot d c)) (hypot d c))))
   (if (<= d -5.124136587792355e+226)
     (- (fma (/ c d) (/ b d) (/ (* a (* c c)) (pow d 3.0))) (/ a d))
     (if (<= d -1.9738142544677371e-143)
       t_0
       (if (<= d -9.658402922807776e-195)
         (- (/ b c) (/ (* d a) (* c c)))
         (if (<= d 3.0970906241597073e+36)
           t_0
           (/ (- (/ (* c b) d) 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 t_0 = (((c * b) - (d * a)) / hypot(d, c)) / hypot(d, c);
	double tmp;
	if (d <= -5.124136587792355e+226) {
		tmp = fma((c / d), (b / d), ((a * (c * c)) / pow(d, 3.0))) - (a / d);
	} else if (d <= -1.9738142544677371e-143) {
		tmp = t_0;
	} else if (d <= -9.658402922807776e-195) {
		tmp = (b / c) - ((d * a) / (c * c));
	} else if (d <= 3.0970906241597073e+36) {
		tmp = t_0;
	} else {
		tmp = (((c * b) / d) - a) / hypot(d, c);
	}
	return tmp;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.0
Target0.5
Herbie14.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 < -5.1241365877923551e226

    1. Initial program 41.8

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

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

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

      \[\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 -5.1241365877923551e226 < d < -1.9738142544677371e-143 or -9.6584029228077763e-195 < d < 3.09709062415970734e36

    1. Initial program 21.3

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified21.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_binary6421.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_binary6421.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_binary6421.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. Simplified21.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. Simplified12.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/_binary6412.8

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

      \[\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 add-sqr-sqrt_binary6413.1

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -1.9738142544677371e-143 < d < -9.6584029228077763e-195

    1. Initial program 23.2

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified23.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_binary6423.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_binary6423.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_binary6423.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. Simplified23.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. Simplified12.1

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

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

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

    if 3.09709062415970734e36 < d

    1. Initial program 35.1

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified35.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_binary6435.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_binary6435.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_binary6435.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. Simplified35.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. Simplified24.4

      \[\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/_binary6424.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. Simplified24.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 16.2

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -5.124136587792355 \cdot 10^{+226}:\\ \;\;\;\;\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}\;d \leq -1.9738142544677371 \cdot 10^{-143}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq -9.658402922807776 \cdot 10^{-195}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{c \cdot c}\\ \mathbf{elif}\;d \leq 3.0970906241597073 \cdot 10^{+36}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{\mathsf{hypot}\left(d, c\right)}\\ \end{array} \]

Reproduce

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