Average Error: 26.4 → 14.5
Time: 7.9s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -2.0933323313511348 \cdot 10^{+116}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.2582924314342352 \cdot 10^{-145}:\\ \;\;\;\;\frac{\frac{a \cdot c + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;d \leq 3.6077882260244803 \cdot 10^{-71}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\ \mathbf{elif}\;d \leq 7.763217840319456 \cdot 10^{+153}:\\ \;\;\;\;\frac{\frac{a \cdot c + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{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 -2.0933323313511348 \cdot 10^{+116}:\\
\;\;\;\;\frac{b}{d}\\

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

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

\mathbf{elif}\;d \leq 7.763217840319456 \cdot 10^{+153}:\\
\;\;\;\;\frac{\frac{a \cdot c + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{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 -2.0933323313511348e+116)
   (/ b d)
   (if (<= d -1.2582924314342352e-145)
     (/
      (/ (+ (* a c) (* d b)) (sqrt (+ (* c c) (* d d))))
      (sqrt (+ (* c c) (* d d))))
     (if (<= d 3.6077882260244803e-71)
       (+ (/ a c) (/ (* d b) (* c c)))
       (if (<= d 7.763217840319456e+153)
         (/
          (/ (+ (* a c) (* d b)) (sqrt (+ (* c c) (* d d))))
          (sqrt (+ (* 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 <= -2.0933323313511348e+116) {
		tmp = b / d;
	} else if (d <= -1.2582924314342352e-145) {
		tmp = (((a * c) + (d * b)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else if (d <= 3.6077882260244803e-71) {
		tmp = (a / c) + ((d * b) / (c * c));
	} else if (d <= 7.763217840319456e+153) {
		tmp = (((a * c) + (d * b)) / sqrt((c * c) + (d * d))) / sqrt((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

Original26.4
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 3 regimes
  2. if d < -2.09333233135113479e116 or 7.76321784031945626e153 < d

    1. Initial program 43.8

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

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

    if -2.09333233135113479e116 < d < -1.25829243143423524e-145 or 3.60778822602448034e-71 < d < 7.76321784031945626e153

    1. Initial program 16.4

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

      \[\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_309116.3

      \[\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 -1.25829243143423524e-145 < d < 3.60778822602448034e-71

    1. Initial program 22.5

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -2.0933323313511348 \cdot 10^{+116}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.2582924314342352 \cdot 10^{-145}:\\ \;\;\;\;\frac{\frac{a \cdot c + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;d \leq 3.6077882260244803 \cdot 10^{-71}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\ \mathbf{elif}\;d \leq 7.763217840319456 \cdot 10^{+153}:\\ \;\;\;\;\frac{\frac{a \cdot c + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array}\]

Reproduce

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