Average Error: 26.0 → 14.5
Time: 5.8s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -1.9684429409544687 \cdot 10^{+147}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -2.5745863487788416 \cdot 10^{-111}:\\ \;\;\;\;\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 8.461756974617389 \cdot 10^{-119}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\ \mathbf{elif}\;d \leq 4.6122899397645205 \cdot 10^{+110}:\\ \;\;\;\;\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 -1.9684429409544687 \cdot 10^{+147}:\\
\;\;\;\;\frac{b}{d}\\

\mathbf{elif}\;d \leq -2.5745863487788416 \cdot 10^{-111}:\\
\;\;\;\;\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 8.461756974617389 \cdot 10^{-119}:\\
\;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\

\mathbf{elif}\;d \leq 4.6122899397645205 \cdot 10^{+110}:\\
\;\;\;\;\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 -1.9684429409544687e+147)
   (/ b d)
   (if (<= d -2.5745863487788416e-111)
     (/
      (/ (+ (* a c) (* d b)) (sqrt (+ (* c c) (* d d))))
      (sqrt (+ (* c c) (* d d))))
     (if (<= d 8.461756974617389e-119)
       (+ (/ a c) (/ (* d b) (* c c)))
       (if (<= d 4.6122899397645205e+110)
         (/
          (/ (+ (* 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 <= -1.9684429409544687e+147) {
		tmp = b / d;
	} else if (d <= -2.5745863487788416e-111) {
		tmp = (((a * c) + (d * b)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else if (d <= 8.461756974617389e-119) {
		tmp = (a / c) + ((d * b) / (c * c));
	} else if (d <= 4.6122899397645205e+110) {
		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.0
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 < -1.96844294095446872e147 or 4.61228993976452051e110 < d

    1. Initial program 42.5

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

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

    if -1.96844294095446872e147 < d < -2.5745863487788416e-111 or 8.4617569746173886e-119 < d < 4.61228993976452051e110

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

    if -2.5745863487788416e-111 < d < 8.4617569746173886e-119

    1. Initial program 21.9

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

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

      \[\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 -1.9684429409544687 \cdot 10^{+147}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -2.5745863487788416 \cdot 10^{-111}:\\ \;\;\;\;\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 8.461756974617389 \cdot 10^{-119}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{c \cdot c}\\ \mathbf{elif}\;d \leq 4.6122899397645205 \cdot 10^{+110}:\\ \;\;\;\;\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}\]

Alternatives

Reproduce

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