Average Error: 25.7 → 14.0
Time: 7.2s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -1.3528976528371084 \cdot 10^{+154}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -4.054012907204699 \cdot 10^{-28}:\\ \;\;\;\;\frac{d}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b}{\sqrt{c \cdot c + d \cdot d}} + \frac{c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 1.7453019913485308 \cdot 10^{-09}:\\ \;\;\;\;\frac{d}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b}{\sqrt{c \cdot c + d \cdot d}} + \frac{a}{c}\\ \mathbf{elif}\;d \leq 5.929583732205677 \cdot 10^{+150}:\\ \;\;\;\;\frac{d}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b}{\sqrt{c \cdot c + d \cdot d}} + \frac{c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \leq -1.3528976528371084 \cdot 10^{+154}:\\
\;\;\;\;\frac{b}{d}\\

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

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

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

\mathbf{else}:\\
\;\;\;\;\frac{b}{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 -1.3528976528371084e+154)
   (/ b d)
   (if (<= d -4.054012907204699e-28)
     (+
      (* (/ d (sqrt (+ (* c c) (* d d)))) (/ b (sqrt (+ (* c c) (* d d)))))
      (/ (* c a) (+ (* c c) (* d d))))
     (if (<= d 1.7453019913485308e-09)
       (+
        (* (/ d (sqrt (+ (* c c) (* d d)))) (/ b (sqrt (+ (* c c) (* d d)))))
        (/ a c))
       (if (<= d 5.929583732205677e+150)
         (+
          (* (/ d (sqrt (+ (* c c) (* d d)))) (/ b (sqrt (+ (* c c) (* d d)))))
          (/ (* c a) (+ (* c c) (* d d))))
         (/ b 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 <= -1.3528976528371084e+154) {
		tmp = b / d;
	} else if (d <= -4.054012907204699e-28) {
		tmp = ((d / sqrt((c * c) + (d * d))) * (b / sqrt((c * c) + (d * d)))) + ((c * a) / ((c * c) + (d * d)));
	} else if (d <= 1.7453019913485308e-09) {
		tmp = ((d / sqrt((c * c) + (d * d))) * (b / sqrt((c * c) + (d * d)))) + (a / c);
	} else if (d <= 5.929583732205677e+150) {
		tmp = ((d / sqrt((c * c) + (d * d))) * (b / sqrt((c * c) + (d * d)))) + ((c * a) / ((c * c) + (d * d)));
	} else {
		tmp = b / 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.7
Target0.4
Herbie14.0
\[\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 d < -1.3528976528371084e154 or 5.92958373220567682e150 < d

    1. Initial program 43.7

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

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

    if -1.3528976528371084e154 < d < -4.0540129072046988e-28 or 1.7453019913485308e-9 < d < 5.92958373220567682e150

    1. Initial program 19.1

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

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

      \[\leadsto \color{blue}{\frac{d \cdot b}{c \cdot c + d \cdot d} + \frac{c \cdot a}{c \cdot c + d \cdot d}}\]
    4. Using strategy rm
    5. Applied add-sqr-sqrt_binary64_316919.1

      \[\leadsto \frac{d \cdot b}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} + \frac{c \cdot a}{c \cdot c + d \cdot d}\]
    6. Applied times-frac_binary64_315313.8

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

    if -4.0540129072046988e-28 < d < 1.7453019913485308e-9

    1. Initial program 19.5

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

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

      \[\leadsto \color{blue}{\frac{d \cdot b}{c \cdot c + d \cdot d} + \frac{c \cdot a}{c \cdot c + d \cdot d}}\]
    4. Using strategy rm
    5. Applied add-sqr-sqrt_binary64_316919.5

      \[\leadsto \frac{d \cdot b}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} + \frac{c \cdot a}{c \cdot c + d \cdot d}\]
    6. Applied times-frac_binary64_315320.2

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

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

      \[\leadsto \frac{d}{\sqrt{d \cdot d + c \cdot c}} \cdot \color{blue}{\frac{b}{\sqrt{d \cdot d + c \cdot c}}} + \frac{c \cdot a}{c \cdot c + d \cdot d}\]
    9. Taylor expanded around inf 14.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -1.3528976528371084 \cdot 10^{+154}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -4.054012907204699 \cdot 10^{-28}:\\ \;\;\;\;\frac{d}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b}{\sqrt{c \cdot c + d \cdot d}} + \frac{c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 1.7453019913485308 \cdot 10^{-09}:\\ \;\;\;\;\frac{d}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b}{\sqrt{c \cdot c + d \cdot d}} + \frac{a}{c}\\ \mathbf{elif}\;d \leq 5.929583732205677 \cdot 10^{+150}:\\ \;\;\;\;\frac{d}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b}{\sqrt{c \cdot c + d \cdot d}} + \frac{c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array}\]

Reproduce

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