Average Error: 24.3 → 11.7
Time: 8.5s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{c \cdot b}{d}\\ \mathbf{if}\;d \leq -1.0001347671923885 \cdot 10^{+180}:\\ \;\;\;\;\frac{a - t_0}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_1 := c \cdot b - d \cdot a\\ t_2 := \frac{\frac{t_1}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{if}\;d \leq -1.0719267080027835 \cdot 10^{-77}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_3 := \frac{b}{c} - \frac{d \cdot a}{{c}^{2}}\\ \mathbf{if}\;d \leq -2.3624307415395805 \cdot 10^{-197}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;d \leq -7.746052639847127 \cdot 10^{-223}:\\ \;\;\;\;\frac{\frac{1}{\mathsf{hypot}\left(d, c\right)}}{\frac{\mathsf{hypot}\left(d, c\right)}{t_1}}\\ \mathbf{elif}\;d \leq -8.168380286508774 \cdot 10^{-280}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;d \leq 2.6508181390370694 \cdot 10^{+133}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0 - a}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\\ \end{array}\\ \end{array} \]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{c \cdot b}{d}\\
\mathbf{if}\;d \leq -1.0001347671923885 \cdot 10^{+180}:\\
\;\;\;\;\frac{a - t_0}{\mathsf{hypot}\left(d, c\right)}\\

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

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_3 := \frac{b}{c} - \frac{d \cdot a}{{c}^{2}}\\
\mathbf{if}\;d \leq -2.3624307415395805 \cdot 10^{-197}:\\
\;\;\;\;t_3\\

\mathbf{elif}\;d \leq -7.746052639847127 \cdot 10^{-223}:\\
\;\;\;\;\frac{\frac{1}{\mathsf{hypot}\left(d, c\right)}}{\frac{\mathsf{hypot}\left(d, c\right)}{t_1}}\\

\mathbf{elif}\;d \leq -8.168380286508774 \cdot 10^{-280}:\\
\;\;\;\;t_3\\

\mathbf{elif}\;d \leq 2.6508181390370694 \cdot 10^{+133}:\\
\;\;\;\;t_2\\

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


\end{array}\\


\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
 (let* ((t_0 (/ (* c b) d)))
   (if (<= d -1.0001347671923885e+180)
     (/ (- a t_0) (hypot d c))
     (let* ((t_1 (- (* c b) (* d a)))
            (t_2 (/ (/ t_1 (hypot d c)) (hypot d c))))
       (if (<= d -1.0719267080027835e-77)
         t_2
         (let* ((t_3 (- (/ b c) (/ (* d a) (pow c 2.0)))))
           (if (<= d -2.3624307415395805e-197)
             t_3
             (if (<= d -7.746052639847127e-223)
               (/ (/ 1.0 (hypot d c)) (/ (hypot d c) t_1))
               (if (<= d -8.168380286508774e-280)
                 t_3
                 (if (<= d 2.6508181390370694e+133)
                   t_2
                   (/ (- 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 t_0 = (c * b) / d;
	double tmp;
	if (d <= -1.0001347671923885e+180) {
		tmp = (a - t_0) / hypot(d, c);
	} else {
		double t_1 = (c * b) - (d * a);
		double t_2 = (t_1 / hypot(d, c)) / hypot(d, c);
		double tmp_1;
		if (d <= -1.0719267080027835e-77) {
			tmp_1 = t_2;
		} else {
			double t_3 = (b / c) - ((d * a) / pow(c, 2.0));
			double tmp_2;
			if (d <= -2.3624307415395805e-197) {
				tmp_2 = t_3;
			} else if (d <= -7.746052639847127e-223) {
				tmp_2 = (1.0 / hypot(d, c)) / (hypot(d, c) / t_1);
			} else if (d <= -8.168380286508774e-280) {
				tmp_2 = t_3;
			} else if (d <= 2.6508181390370694e+133) {
				tmp_2 = t_2;
			} else {
				tmp_2 = (t_0 - a) / hypot(d, c);
			}
			tmp_1 = tmp_2;
		}
		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.3
Target0.5
Herbie11.7
\[\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 5 regimes
  2. if d < -1.00013476719238851e180

    1. Initial program 42.5

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified42.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_binary6442.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_binary6442.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_binary6442.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. Simplified42.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.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/_binary6428.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. Simplified28.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 9.6

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

    if -1.00013476719238851e180 < d < -1.0719267080027835e-77 or -8.1683802865087739e-280 < d < 2.6508181390370694e133

    1. Initial program 19.1

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified19.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_binary6419.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_binary6419.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_binary6419.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. Simplified19.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 pow1_binary6411.9

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

    if -1.0719267080027835e-77 < d < -2.3624307415395805e-197 or -7.74605263984712723e-223 < d < -8.1683802865087739e-280

    1. Initial program 16.0

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

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

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

    if -2.3624307415395805e-197 < d < -7.74605263984712723e-223

    1. Initial program 20.0

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified20.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_binary6420.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_binary6420.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_binary6420.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. Simplified20.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. Simplified8.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/_binary648.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. Simplified8.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_binary648.8

      \[\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 associate-/r/_binary648.8

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

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

    if 2.6508181390370694e133 < d

    1. Initial program 40.7

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified40.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_binary6440.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_binary6440.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_binary6440.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. Simplified40.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. Simplified26.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. Applied associate-*l/_binary6426.0

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -1.0001347671923885 \cdot 10^{+180}:\\ \;\;\;\;\frac{a - \frac{c \cdot b}{d}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq -1.0719267080027835 \cdot 10^{-77}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq -2.3624307415395805 \cdot 10^{-197}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{{c}^{2}}\\ \mathbf{elif}\;d \leq -7.746052639847127 \cdot 10^{-223}:\\ \;\;\;\;\frac{\frac{1}{\mathsf{hypot}\left(d, c\right)}}{\frac{\mathsf{hypot}\left(d, c\right)}{c \cdot b - d \cdot a}}\\ \mathbf{elif}\;d \leq -8.168380286508774 \cdot 10^{-280}:\\ \;\;\;\;\frac{b}{c} - \frac{d \cdot a}{{c}^{2}}\\ \mathbf{elif}\;d \leq 2.6508181390370694 \cdot 10^{+133}:\\ \;\;\;\;\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 2022068 
(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))))