Average Error: 25.8 → 14.5
Time: 9.3s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -3.6836303645104744 \cdot 10^{+137}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.005001047695393 \cdot 10^{-161}:\\ \;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{d \cdot b + c \cdot a}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;d \leq 1.1429473994431096 \cdot 10^{-145}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\ \mathbf{elif}\;d \leq 4.621378501818419 \cdot 10^{+44}:\\ \;\;\;\;\frac{\frac{d \cdot b + c \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \leq -3.6836303645104744 \cdot 10^{+137}:\\
\;\;\;\;\frac{b}{d}\\

\mathbf{elif}\;d \leq -1.005001047695393 \cdot 10^{-161}:\\
\;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{d \cdot b + c \cdot a}{\sqrt{c \cdot c + d \cdot d}}\\

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

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

\mathbf{else}:\\
\;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\

\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 -3.6836303645104744e+137)
   (/ b d)
   (if (<= d -1.005001047695393e-161)
     (*
      (/ 1.0 (sqrt (+ (* c c) (* d d))))
      (/ (+ (* d b) (* c a)) (sqrt (+ (* c c) (* d d)))))
     (if (<= d 1.1429473994431096e-145)
       (+ (/ a c) (/ (* d b) (* c c)))
       (if (<= d 4.621378501818419e+44)
         (/
          (/ (+ (* d b) (* c a)) (sqrt (+ (* c c) (* d d))))
          (sqrt (+ (* c c) (* d d))))
         (+ (/ b d) (/ (* c a) (* d d))))))))
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 <= -3.6836303645104744e+137) {
		tmp = b / d;
	} else if (d <= -1.005001047695393e-161) {
		tmp = (1.0 / sqrt((c * c) + (d * d))) * (((d * b) + (c * a)) / sqrt((c * c) + (d * d)));
	} else if (d <= 1.1429473994431096e-145) {
		tmp = (a / c) + ((d * b) / (c * c));
	} else if (d <= 4.621378501818419e+44) {
		tmp = (((d * b) + (c * a)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else {
		tmp = (b / d) + ((c * a) / (d * d));
	}
	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

Original25.8
Target0.4
Herbie14.5
\[\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 5 regimes
  2. if d < -3.6836303645104744e137

    1. Initial program 43.5

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

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

    if -3.6836303645104744e137 < d < -1.00500104769539303e-161

    1. Initial program 16.5

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

      \[\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 *-un-lft-identity_binary64_178316.5

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

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified16.5

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

    if -1.00500104769539303e-161 < d < 1.1429473994431096e-145

    1. Initial program 23.6

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

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

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

    if 1.1429473994431096e-145 < d < 4.6213785018184189e44

    1. Initial program 14.1

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

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

      \[\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 4.6213785018184189e44 < d

    1. Initial program 34.6

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

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

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c \cdot a}{d \cdot d}}\]
  3. Recombined 5 regimes into one program.
  4. Final simplification14.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -3.6836303645104744 \cdot 10^{+137}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.005001047695393 \cdot 10^{-161}:\\ \;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{d \cdot b + c \cdot a}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;d \leq 1.1429473994431096 \cdot 10^{-145}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\ \mathbf{elif}\;d \leq 4.621378501818419 \cdot 10^{+44}:\\ \;\;\;\;\frac{\frac{d \cdot b + c \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\ \end{array}\]

Reproduce

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