Average Error: 26.3 → 14.3
Time: 10.9s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \leq -2.7839182926984815 \cdot 10^{+79}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -8.849808015292707 \cdot 10^{-110}:\\ \;\;\;\;\frac{\frac{c \cdot a + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq 2.1472644582174967 \cdot 10^{-123}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 1.315316451044619 \cdot 10^{+86}:\\ \;\;\;\;\frac{\frac{c \cdot a + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot d}{{c}^{2}}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \leq -2.7839182926984815 \cdot 10^{+79}:\\
\;\;\;\;\frac{a}{c}\\

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

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

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

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

\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 -2.7839182926984815e+79)
   (/ a c)
   (if (<= c -8.849808015292707e-110)
     (/
      (/ (+ (* c a) (* b d)) (sqrt (+ (* c c) (* d d))))
      (sqrt (+ (* c c) (* d d))))
     (if (<= c 2.1472644582174967e-123)
       (+ (/ b d) (/ (* c a) (pow d 2.0)))
       (if (<= c 1.315316451044619e+86)
         (/
          (/ (+ (* c a) (* b d)) (sqrt (+ (* c c) (* d d))))
          (sqrt (+ (* c c) (* d d))))
         (+ (/ a c) (/ (* b d) (pow c 2.0))))))))
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 <= -2.7839182926984815e+79) {
		tmp = a / c;
	} else if (c <= -8.849808015292707e-110) {
		tmp = (((c * a) + (b * d)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else if (c <= 2.1472644582174967e-123) {
		tmp = (b / d) + ((c * a) / pow(d, 2.0));
	} else if (c <= 1.315316451044619e+86) {
		tmp = (((c * a) + (b * d)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else {
		tmp = (a / c) + ((b * d) / pow(c, 2.0));
	}
	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.3
\[\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 c < -2.7839182926984815e79

    1. Initial program 39.7

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

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

    if -2.7839182926984815e79 < c < -8.84980801529270659e-110 or 2.1472644582174967e-123 < c < 1.3153164510446189e86

    1. Initial program 15.0

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

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

      \[\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 -8.84980801529270659e-110 < c < 2.1472644582174967e-123

    1. Initial program 21.1

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

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

    if 1.3153164510446189e86 < c

    1. Initial program 38.5

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -2.7839182926984815 \cdot 10^{+79}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -8.849808015292707 \cdot 10^{-110}:\\ \;\;\;\;\frac{\frac{c \cdot a + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq 2.1472644582174967 \cdot 10^{-123}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 1.315316451044619 \cdot 10^{+86}:\\ \;\;\;\;\frac{\frac{c \cdot a + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot d}{{c}^{2}}\\ \end{array}\]

Reproduce

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