Average Error: 26.1 → 11.1
Time: 6.7s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} \mathbf{if}\;d \leq -1.6659650814561475 \cdot 10^{+159} \lor \neg \left(d \leq 1.2030428421309446 \cdot 10^{+113}\right):\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\frac{{d}^{3}}{c}}, a, \frac{b}{d} \cdot \left(\frac{c}{d} - {\left(\frac{c}{d}\right)}^{3}\right)\right) - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \end{array} \]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \leq -1.6659650814561475 \cdot 10^{+159} \lor \neg \left(d \leq 1.2030428421309446 \cdot 10^{+113}\right):\\
\;\;\;\;\mathsf{fma}\left(\frac{c}{\frac{{d}^{3}}{c}}, a, \frac{b}{d} \cdot \left(\frac{c}{d} - {\left(\frac{c}{d}\right)}^{3}\right)\right) - \frac{a}{d}\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(d, c\right)}}{\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
 (if (or (<= d -1.6659650814561475e+159) (not (<= d 1.2030428421309446e+113)))
   (-
    (fma (/ c (/ (pow d 3.0) c)) a (* (/ b d) (- (/ c d) (pow (/ c d) 3.0))))
    (/ a d))
   (/ (/ (- (* c b) (* d a)) (hypot d 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 ((d <= -1.6659650814561475e+159) || !(d <= 1.2030428421309446e+113)) {
		tmp = fma((c / (pow(d, 3.0) / c)), a, ((b / d) * ((c / d) - pow((c / d), 3.0)))) - (a / d);
	} else {
		tmp = (((c * b) - (d * a)) / hypot(d, c)) / hypot(d, c);
	}
	return tmp;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.1
Target0.5
Herbie11.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 2 regimes
  2. if d < -1.6659650814561475e159 or 1.2030428421309446e113 < d

    1. Initial program 42.2

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in c around 0 26.3

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

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

    if -1.6659650814561475e159 < d < 1.2030428421309446e113

    1. Initial program 19.3

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -1.6659650814561475 \cdot 10^{+159} \lor \neg \left(d \leq 1.2030428421309446 \cdot 10^{+113}\right):\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\frac{{d}^{3}}{c}}, a, \frac{b}{d} \cdot \left(\frac{c}{d} - {\left(\frac{c}{d}\right)}^{3}\right)\right) - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \end{array} \]

Reproduce

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