Average Error: 26.2 → 12.1
Time: 6.8s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} \mathbf{if}\;d \leq -4.988426382296112 \cdot 10^{+108}:\\ \;\;\;\;\frac{a - \frac{c \cdot b}{d}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_0 := \frac{\frac{\mathsf{fma}\left(c, b, d \cdot \left(-a\right)\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{if}\;d \leq -1.851745546982872 \cdot 10^{-226}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.2315960820976547 \cdot 10^{-167}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 2.5261196808138677 \cdot 10^{+149}:\\ \;\;\;\;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.988426382296112 \cdot 10^{+108}:\\
\;\;\;\;\frac{a - \frac{c \cdot b}{d}}{\mathsf{hypot}\left(d, c\right)}\\

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

\mathbf{elif}\;d \leq 1.2315960820976547 \cdot 10^{-167}:\\
\;\;\;\;\frac{b}{c}\\

\mathbf{elif}\;d \leq 2.5261196808138677 \cdot 10^{+149}:\\
\;\;\;\;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.988426382296112e+108)
   (/ (- a (/ (* c b) d)) (hypot d c))
   (let* ((t_0 (/ (/ (fma c b (* d (- a))) (hypot d c)) (hypot d c))))
     (if (<= d -1.851745546982872e-226)
       t_0
       (if (<= d 1.2315960820976547e-167)
         (/ b c)
         (if (<= d 2.5261196808138677e+149) 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.988426382296112e+108) {
		tmp = (a - ((c * b) / d)) / hypot(d, c);
	} else {
		double t_0 = (fma(c, b, (d * -a)) / hypot(d, c)) / hypot(d, c);
		double tmp_1;
		if (d <= -1.851745546982872e-226) {
			tmp_1 = t_0;
		} else if (d <= 1.2315960820976547e-167) {
			tmp_1 = b / c;
		} else if (d <= 2.5261196808138677e+149) {
			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

Target

Original26.2
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.98842638229611199e108

    1. Initial program 40.0

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

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

      \[\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_binary6440.0

      \[\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_binary6440.0

      \[\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. Simplified40.0

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

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

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

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

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

    if -4.98842638229611199e108 < d < -1.8517455469828719e-226 or 1.2315960820976547e-167 < d < 2.5261196808138677e149

    1. Initial program 17.1

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified17.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_binary6417.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_binary6417.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_binary6417.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. Simplified17.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. 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.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. Simplified11.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. Applied fma-neg_binary6411.6

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

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

    if -1.8517455469828719e-226 < d < 1.2315960820976547e-167

    1. Initial program 24.7

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

      \[\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 inf 11.6

      \[\leadsto \color{blue}{\frac{b}{c}} \]

    if 2.5261196808138677e149 < d

    1. Initial program 45.3

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified45.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_binary6445.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_binary6445.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_binary6445.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. Simplified45.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. Simplified30.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/_binary6430.4

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

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

      \[\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.988426382296112 \cdot 10^{+108}:\\ \;\;\;\;\frac{a - \frac{c \cdot b}{d}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq -1.851745546982872 \cdot 10^{-226}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(c, b, d \cdot \left(-a\right)\right)}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq 1.2315960820976547 \cdot 10^{-167}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 2.5261196808138677 \cdot 10^{+149}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(c, b, d \cdot \left(-a\right)\right)}{\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 2021275 
(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))))