Average Error: 25.9 → 15.2
Time: 6.4s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \leq -7.666814711380057 \cdot 10^{+78}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -2.17846808821293 \cdot 10^{-124}:\\ \;\;\;\;\frac{\frac{d \cdot b + c \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq 7.205564063930503 \cdot 10^{-187}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 8.456689833717296 \cdot 10^{+141}:\\ \;\;\;\;\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{a}{c}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \leq -7.666814711380057 \cdot 10^{+78}:\\
\;\;\;\;\frac{a}{c}\\

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

\mathbf{elif}\;c \leq 7.205564063930503 \cdot 10^{-187}:\\
\;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\

\mathbf{elif}\;c \leq 8.456689833717296 \cdot 10^{+141}:\\
\;\;\;\;\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{a}{c}\\

\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 (<= c -7.666814711380057e+78)
   (/ a c)
   (if (<= c -2.17846808821293e-124)
     (/
      (/ (+ (* d b) (* c a)) (sqrt (+ (* c c) (* d d))))
      (sqrt (+ (* c c) (* d d))))
     (if (<= c 7.205564063930503e-187)
       (+ (/ b d) (/ (* c a) (pow d 2.0)))
       (if (<= c 8.456689833717296e+141)
         (/
          (/ (+ (* d b) (* c a)) (sqrt (+ (* c c) (* d d))))
          (sqrt (+ (* c c) (* d d))))
         (/ a c))))))
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 (c <= -7.666814711380057e+78) {
		tmp = a / c;
	} else if (c <= -2.17846808821293e-124) {
		tmp = (((d * b) + (c * a)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else if (c <= 7.205564063930503e-187) {
		tmp = (b / d) + ((c * a) / pow(d, 2.0));
	} else if (c <= 8.456689833717296e+141) {
		tmp = (((d * b) + (c * a)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else {
		tmp = a / c;
	}
	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.9
Target0.5
Herbie15.2
\[\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 3 regimes
  2. if c < -7.6668147113800567e78 or 8.4566898337172956e141 < c

    1. Initial program 39.6

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

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

    if -7.6668147113800567e78 < c < -2.17846808821293002e-124 or 7.20556406393050255e-187 < c < 8.4566898337172956e141

    1. Initial program 16.2

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

      \[\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*_binary6416.1

      \[\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}}}\]
    5. Simplified16.1

      \[\leadsto \frac{\color{blue}{\frac{d \cdot b + c \cdot a}{\sqrt{c \cdot c + d \cdot d}}}}{\sqrt{c \cdot c + d \cdot d}}\]
    6. Using strategy rm
    7. Applied associate-/l/_binary6416.2

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

      \[\leadsto \frac{d \cdot b + c \cdot a}{\color{blue}{c \cdot c + d \cdot d}}\]
    9. Using strategy rm
    10. Applied add-sqr-sqrt_binary6416.2

      \[\leadsto \frac{d \cdot b + c \cdot a}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    11. Applied associate-/r*_binary6416.1

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

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

    if -2.17846808821293002e-124 < c < 7.20556406393050255e-187

    1. Initial program 24.0

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -7.666814711380057 \cdot 10^{+78}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -2.17846808821293 \cdot 10^{-124}:\\ \;\;\;\;\frac{\frac{d \cdot b + c \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq 7.205564063930503 \cdot 10^{-187}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 8.456689833717296 \cdot 10^{+141}:\\ \;\;\;\;\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{a}{c}\\ \end{array}\]

Reproduce

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