Average Error: 26.3 → 14.9
Time: 8.7s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -2.9682609400537914 \cdot 10^{+110}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\ \mathbf{elif}\;d \leq -3.1632983967499654 \cdot 10^{-102}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \leq 3.7484479560584536 \cdot 10^{-146}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\ \mathbf{elif}\;d \leq 1.3239207115659564 \cdot 10^{+69}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{d}{b}}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \leq -2.9682609400537914 \cdot 10^{+110}:\\
\;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\

\mathbf{elif}\;d \leq -3.1632983967499654 \cdot 10^{-102}:\\
\;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\

\mathbf{elif}\;d \leq 3.7484479560584536 \cdot 10^{-146}:\\
\;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\

\mathbf{elif}\;d \leq 1.3239207115659564 \cdot 10^{+69}:\\
\;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{d}{b}}\\

\end{array}
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (if (<= d -2.9682609400537914e+110)
   (+ (/ b d) (/ (* c a) (* d d)))
   (if (<= d -3.1632983967499654e-102)
     (/
      (/ (+ (* c a) (* d b)) (sqrt (+ (* d d) (* c c))))
      (sqrt (+ (* d d) (* c c))))
     (if (<= d 3.7484479560584536e-146)
       (+ (/ a c) (/ (* d b) (* c c)))
       (if (<= d 1.3239207115659564e+69)
         (/
          (/ (+ (* c a) (* d b)) (sqrt (+ (* d d) (* c c))))
          (sqrt (+ (* d d) (* c c))))
         (/ 1.0 (/ d b)))))))
double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
	double tmp;
	if (d <= -2.9682609400537914e+110) {
		tmp = (b / d) + ((c * a) / (d * d));
	} else if (d <= -3.1632983967499654e-102) {
		tmp = (((c * a) + (d * b)) / sqrt((d * d) + (c * c))) / sqrt((d * d) + (c * c));
	} else if (d <= 3.7484479560584536e-146) {
		tmp = (a / c) + ((d * b) / (c * c));
	} else if (d <= 1.3239207115659564e+69) {
		tmp = (((c * a) + (d * b)) / sqrt((d * d) + (c * c))) / sqrt((d * d) + (c * c));
	} else {
		tmp = 1.0 / (d / b);
	}
	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

Original26.3
Target0.4
Herbie14.9
\[\begin{array}{l} \mathbf{if}\;\left|d\right| < \left|c\right|:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array}\]

Derivation

  1. Split input into 4 regimes
  2. if d < -2.9682609400537914e110

    1. Initial program 40.4

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

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{a \cdot c}{{d}^{2}}}\]
    3. Simplified15.3

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

    if -2.9682609400537914e110 < d < -3.16329839674996542e-102 or 3.74844795605845357e-146 < d < 1.32392071156595641e69

    1. Initial program 15.7

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt_binary64_316915.7

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied associate-/r*_binary64_309115.6

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

    if -3.16329839674996542e-102 < d < 3.74844795605845357e-146

    1. Initial program 22.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Taylor expanded around inf 10.9

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}}\]
    3. Simplified10.9

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

    if 1.32392071156595641e69 < d

    1. Initial program 37.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied clear-num_binary64_314637.6

      \[\leadsto \color{blue}{\frac{1}{\frac{c \cdot c + d \cdot d}{a \cdot c + b \cdot d}}}\]
    4. Simplified37.6

      \[\leadsto \frac{1}{\color{blue}{\frac{c \cdot c + d \cdot d}{d \cdot b + c \cdot a}}}\]
    5. Taylor expanded around 0 18.5

      \[\leadsto \frac{1}{\color{blue}{\frac{d}{b}}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification14.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -2.9682609400537914 \cdot 10^{+110}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\ \mathbf{elif}\;d \leq -3.1632983967499654 \cdot 10^{-102}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \leq 3.7484479560584536 \cdot 10^{-146}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\ \mathbf{elif}\;d \leq 1.3239207115659564 \cdot 10^{+69}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{d}{b}}\\ \end{array}\]

Reproduce

herbie shell --seed 2021043 
(FPCore (a b c d)
  :name "Complex division, real part"
  :precision binary64

  :herbie-target
  (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d)))))

  (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))