Average Error: 26.3 → 15.3
Time: 9.0s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -2.5033965365909377 \cdot 10^{+145}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{\frac{d \cdot d}{c}}\\ \mathbf{elif}\;d \leq -1.1669216222378627 \cdot 10^{-135}:\\ \;\;\;\;\frac{\frac{d \cdot b + a \cdot c}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \leq 1.0481161816497777 \cdot 10^{-159}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\ \mathbf{elif}\;d \leq 5.454863883134953 \cdot 10^{+45}:\\ \;\;\;\;\frac{\frac{d \cdot b + a \cdot c}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \leq 4.762697302129139 \cdot 10^{+91}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{c \cdot c}{d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{\frac{d \cdot d}{c}}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \leq -2.5033965365909377 \cdot 10^{+145}:\\
\;\;\;\;\frac{b}{d} + \frac{a}{\frac{d \cdot d}{c}}\\

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

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

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

\mathbf{elif}\;d \leq 4.762697302129139 \cdot 10^{+91}:\\
\;\;\;\;\frac{a}{c} + \frac{b}{\frac{c \cdot c}{d}}\\

\mathbf{else}:\\
\;\;\;\;\frac{b}{d} + \frac{a}{\frac{d \cdot d}{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 (<= d -2.5033965365909377e+145)
   (+ (/ b d) (/ a (/ (* d d) c)))
   (if (<= d -1.1669216222378627e-135)
     (/
      (/ (+ (* d b) (* a c)) (sqrt (+ (* d d) (* c c))))
      (sqrt (+ (* d d) (* c c))))
     (if (<= d 1.0481161816497777e-159)
       (+ (/ a c) (/ (* d b) (* c c)))
       (if (<= d 5.454863883134953e+45)
         (/
          (/ (+ (* d b) (* a c)) (sqrt (+ (* d d) (* c c))))
          (sqrt (+ (* d d) (* c c))))
         (if (<= d 4.762697302129139e+91)
           (+ (/ a c) (/ b (/ (* c c) d)))
           (+ (/ b d) (/ a (/ (* d d) 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 (d <= -2.5033965365909377e+145) {
		tmp = (b / d) + (a / ((d * d) / c));
	} else if (d <= -1.1669216222378627e-135) {
		tmp = (((d * b) + (a * c)) / sqrt((d * d) + (c * c))) / sqrt((d * d) + (c * c));
	} else if (d <= 1.0481161816497777e-159) {
		tmp = (a / c) + ((d * b) / (c * c));
	} else if (d <= 5.454863883134953e+45) {
		tmp = (((d * b) + (a * c)) / sqrt((d * d) + (c * c))) / sqrt((d * d) + (c * c));
	} else if (d <= 4.762697302129139e+91) {
		tmp = (a / c) + (b / ((c * c) / d));
	} else {
		tmp = (b / d) + (a / ((d * d) / 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

Original26.3
Target0.5
Herbie15.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 d < -2.5033965365909377e145 or 4.76269730212913932e91 < d

    1. Initial program 40.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied div-inv_binary64_212140.9

      \[\leadsto \color{blue}{\left(a \cdot c + b \cdot d\right) \cdot \frac{1}{c \cdot c + d \cdot d}}\]
    4. Using strategy rm
    5. Applied add-sqr-sqrt_binary64_214640.9

      \[\leadsto \left(a \cdot c + b \cdot d\right) \cdot \frac{1}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    6. Applied add-cube-cbrt_binary64_215940.9

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

      \[\leadsto \left(a \cdot c + b \cdot d\right) \cdot \color{blue}{\left(\frac{\sqrt[3]{1} \cdot \sqrt[3]{1}}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{\sqrt[3]{1}}{\sqrt{c \cdot c + d \cdot d}}\right)}\]
    8. Applied associate-*r*_binary64_206440.9

      \[\leadsto \color{blue}{\left(\left(a \cdot c + b \cdot d\right) \cdot \frac{\sqrt[3]{1} \cdot \sqrt[3]{1}}{\sqrt{c \cdot c + d \cdot d}}\right) \cdot \frac{\sqrt[3]{1}}{\sqrt{c \cdot c + d \cdot d}}}\]
    9. Simplified40.9

      \[\leadsto \color{blue}{\left(\left(d \cdot b + c \cdot a\right) \cdot \frac{\sqrt[3]{1} \cdot \sqrt[3]{1}}{\sqrt{c \cdot c + d \cdot d}}\right)} \cdot \frac{\sqrt[3]{1}}{\sqrt{c \cdot c + d \cdot d}}\]
    10. Taylor expanded around inf 16.4

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

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

    if -2.5033965365909377e145 < d < -1.16692162223786273e-135 or 1.0481161816497777e-159 < d < 5.4548638831349529e45

    1. Initial program 16.7

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt_binary64_214616.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_206816.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}}}\]
    5. Simplified16.6

      \[\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}}\]

    if -1.16692162223786273e-135 < d < 1.0481161816497777e-159

    1. Initial program 23.2

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

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

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

    if 5.4548638831349529e45 < d < 4.76269730212913932e91

    1. Initial program 19.7

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied div-inv_binary64_212119.8

      \[\leadsto \color{blue}{\left(a \cdot c + b \cdot d\right) \cdot \frac{1}{c \cdot c + d \cdot d}}\]
    4. Using strategy rm
    5. Applied add-sqr-sqrt_binary64_214619.8

      \[\leadsto \left(a \cdot c + b \cdot d\right) \cdot \frac{1}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    6. Applied add-cube-cbrt_binary64_215919.8

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

      \[\leadsto \left(a \cdot c + b \cdot d\right) \cdot \color{blue}{\left(\frac{\sqrt[3]{1} \cdot \sqrt[3]{1}}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{\sqrt[3]{1}}{\sqrt{c \cdot c + d \cdot d}}\right)}\]
    8. Applied associate-*r*_binary64_206419.8

      \[\leadsto \color{blue}{\left(\left(a \cdot c + b \cdot d\right) \cdot \frac{\sqrt[3]{1} \cdot \sqrt[3]{1}}{\sqrt{c \cdot c + d \cdot d}}\right) \cdot \frac{\sqrt[3]{1}}{\sqrt{c \cdot c + d \cdot d}}}\]
    9. Simplified19.8

      \[\leadsto \color{blue}{\left(\left(d \cdot b + c \cdot a\right) \cdot \frac{\sqrt[3]{1} \cdot \sqrt[3]{1}}{\sqrt{c \cdot c + d \cdot d}}\right)} \cdot \frac{\sqrt[3]{1}}{\sqrt{c \cdot c + d \cdot d}}\]
    10. Taylor expanded around 0 38.8

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -2.5033965365909377 \cdot 10^{+145}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{\frac{d \cdot d}{c}}\\ \mathbf{elif}\;d \leq -1.1669216222378627 \cdot 10^{-135}:\\ \;\;\;\;\frac{\frac{d \cdot b + a \cdot c}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \leq 1.0481161816497777 \cdot 10^{-159}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\ \mathbf{elif}\;d \leq 5.454863883134953 \cdot 10^{+45}:\\ \;\;\;\;\frac{\frac{d \cdot b + a \cdot c}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \leq 4.762697302129139 \cdot 10^{+91}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{\frac{c \cdot c}{d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{\frac{d \cdot d}{c}}\\ \end{array}\]

Reproduce

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