Average Error: 26.1 → 15.7
Time: 9.1s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -1.7122153695293727 \cdot 10^{+103}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -6.029624906319389 \cdot 10^{-162}:\\ \;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{c \cdot a + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;d \leq 2.7322757867875484 \cdot 10^{-28}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 2.4603004612766966 \cdot 10^{+68}:\\ \;\;\;\;\frac{c \cdot a}{{c}^{2} + {d}^{2}} + \frac{d \cdot b}{{c}^{2} + {d}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \leq -1.7122153695293727 \cdot 10^{+103}:\\
\;\;\;\;\frac{b}{d}\\

\mathbf{elif}\;d \leq -6.029624906319389 \cdot 10^{-162}:\\
\;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{c \cdot a + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}\\

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

\mathbf{elif}\;d \leq 2.4603004612766966 \cdot 10^{+68}:\\
\;\;\;\;\frac{c \cdot a}{{c}^{2} + {d}^{2}} + \frac{d \cdot b}{{c}^{2} + {d}^{2}}\\

\mathbf{else}:\\
\;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{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 (<= d -1.7122153695293727e+103)
   (/ b d)
   (if (<= d -6.029624906319389e-162)
     (*
      (/ 1.0 (sqrt (+ (* c c) (* d d))))
      (/ (+ (* c a) (* d b)) (sqrt (+ (* c c) (* d d)))))
     (if (<= d 2.7322757867875484e-28)
       (+ (/ a c) (/ (* d b) (pow c 2.0)))
       (if (<= d 2.4603004612766966e+68)
         (+
          (/ (* c a) (+ (pow c 2.0) (pow d 2.0)))
          (/ (* d b) (+ (pow c 2.0) (pow d 2.0))))
         (+ (/ b d) (/ (* c a) (pow d 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 (d <= -1.7122153695293727e+103) {
		tmp = b / d;
	} else if (d <= -6.029624906319389e-162) {
		tmp = (1.0 / sqrt((c * c) + (d * d))) * (((c * a) + (d * b)) / sqrt((c * c) + (d * d)));
	} else if (d <= 2.7322757867875484e-28) {
		tmp = (a / c) + ((d * b) / pow(c, 2.0));
	} else if (d <= 2.4603004612766966e+68) {
		tmp = ((c * a) / (pow(c, 2.0) + pow(d, 2.0))) + ((d * b) / (pow(c, 2.0) + pow(d, 2.0)));
	} else {
		tmp = (b / d) + ((c * a) / pow(d, 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.1
Target0.4
Herbie15.7
\[\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 5 regimes
  2. if d < -1.71221536952937269e103

    1. Initial program 40.9

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

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

    if -1.71221536952937269e103 < d < -6.02962490631938915e-162

    1. Initial program 15.3

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

      \[\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 *-un-lft-identity_binary64_280615.3

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

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

    if -6.02962490631938915e-162 < d < 2.7322757867875484e-28

    1. Initial program 21.1

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

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

    if 2.7322757867875484e-28 < d < 2.46030046127669663e68

    1. Initial program 15.4

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

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

    if 2.46030046127669663e68 < d

    1. Initial program 36.9

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -1.7122153695293727 \cdot 10^{+103}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -6.029624906319389 \cdot 10^{-162}:\\ \;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{c \cdot a + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;d \leq 2.7322757867875484 \cdot 10^{-28}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 2.4603004612766966 \cdot 10^{+68}:\\ \;\;\;\;\frac{c \cdot a}{{c}^{2} + {d}^{2}} + \frac{d \cdot b}{{c}^{2} + {d}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \end{array}\]

Reproduce

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