Average Error: 26.3 → 14.3
Time: 9.6s
Precision: binary64
Cost: 15556
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \leq -2.7839182926984815 \cdot 10^{+79}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -8.849808015292707 \cdot 10^{-110}:\\ \;\;\;\;\frac{\frac{c \cdot a + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq 2.1472644582174967 \cdot 10^{-123}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\ \mathbf{elif}\;c \leq 1.315316451044619 \cdot 10^{+86}:\\ \;\;\;\;\frac{\frac{c \cdot a + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot d}{c \cdot c}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \leq -2.7839182926984815 \cdot 10^{+79}:\\
\;\;\;\;\frac{a}{c}\\

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

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

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

\mathbf{else}:\\
\;\;\;\;\frac{a}{c} + \frac{b \cdot d}{c \cdot 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 (<= c -2.7839182926984815e+79)
   (/ a c)
   (if (<= c -8.849808015292707e-110)
     (/
      (/ (+ (* c a) (* b d)) (sqrt (+ (* c c) (* d d))))
      (sqrt (+ (* c c) (* d d))))
     (if (<= c 2.1472644582174967e-123)
       (+ (/ b d) (/ (* c a) (* d d)))
       (if (<= c 1.315316451044619e+86)
         (/
          (/ (+ (* c a) (* b d)) (sqrt (+ (* c c) (* d d))))
          (sqrt (+ (* c c) (* d d))))
         (+ (/ a c) (/ (* b d) (* c 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 (c <= -2.7839182926984815e+79) {
		tmp = a / c;
	} else if (c <= -8.849808015292707e-110) {
		tmp = (((c * a) + (b * d)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else if (c <= 2.1472644582174967e-123) {
		tmp = (b / d) + ((c * a) / (d * d));
	} else if (c <= 1.315316451044619e+86) {
		tmp = (((c * a) + (b * d)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else {
		tmp = (a / c) + ((b * d) / (c * 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.4
Herbie14.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}\]

Alternatives

Alternative 1
Error14.3
Cost2372
\[\begin{array}{l} \mathbf{if}\;c \leq -3.88489195189713 \cdot 10^{+80}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -3.697868456727824 \cdot 10^{-105}:\\ \;\;\;\;\frac{c \cdot a + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 4.231028026572649 \cdot 10^{-123}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\ \mathbf{elif}\;c \leq 4.534169458994563 \cdot 10^{+85}:\\ \;\;\;\;\left(c \cdot a + b \cdot d\right) \cdot \frac{1}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot d}{c \cdot c}\\ \end{array}\]
Alternative 2
Error14.4
Cost2244
\[\begin{array}{l} \mathbf{if}\;c \leq -3.067174421205907 \cdot 10^{+78}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -2.5452377900992097 \cdot 10^{-111}:\\ \;\;\;\;\frac{c \cdot a + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 2.7738332462896228 \cdot 10^{-123}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\ \mathbf{elif}\;c \leq 7.627044215435255 \cdot 10^{+80}:\\ \;\;\;\;\frac{c \cdot a + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot d}{c \cdot c}\\ \end{array}\]
Alternative 3
Error19.1
Cost2630
\[\begin{array}{l} \mathbf{if}\;c \leq -6.7235157637121674 \cdot 10^{+41}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot d}{c \cdot c}\\ \mathbf{elif}\;c \leq -933.8271277034492:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\ \mathbf{elif}\;c \leq -1.4560731490103253 \cdot 10^{-74}:\\ \;\;\;\;\frac{c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 4.851168696310362 \cdot 10^{-53}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\ \mathbf{elif}\;c \leq 1.8421711922152538 \cdot 10^{+44}:\\ \;\;\;\;\frac{c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 9.194438870773511 \cdot 10^{+79}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot d}{c \cdot c}\\ \end{array}\]
Alternative 4
Error20.2
Cost1750
\[\begin{array}{l} \mathbf{if}\;d \leq -1.5705045420008371 \cdot 10^{+84}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -2.3261484249870466 \cdot 10^{+64} \lor \neg \left(d \leq -0.0023436085176820323\right) \land \left(d \leq -3.6577455230706303 \cdot 10^{-100} \lor \neg \left(d \leq -9.76721819496008 \cdot 10^{-144}\right) \land d \leq 3.477343742624138 \cdot 10^{-29}\right):\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot d}{c \cdot c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\ \end{array}\]
Alternative 5
Error22.5
Cost1562
\[\begin{array}{l} \mathbf{if}\;d \leq -1.4197209839973271 \cdot 10^{+84} \lor \neg \left(d \leq -1.2310890621845736 \cdot 10^{+63} \lor \neg \left(d \leq -0.003450368105936221\right) \land \left(d \leq -3.6577455230706303 \cdot 10^{-100} \lor \neg \left(d \leq -1.711980249881788 \cdot 10^{-170}\right) \land d \leq 1.3472407996180275 \cdot 10^{-28}\right)\right):\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot d}{c \cdot c}\\ \end{array}\]
Alternative 6
Error23.6
Cost785
\[\begin{array}{l} \mathbf{if}\;c \leq -1.73245955427787 \cdot 10^{+42} \lor \neg \left(c \leq -26.275331922204646 \lor \neg \left(c \leq -1.4560731490103253 \cdot 10^{-74}\right) \land c \leq 5.330723836123918 \cdot 10^{+79}\right):\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array}\]
Alternative 7
Error35.3
Cost513
\[\begin{array}{l} \mathbf{if}\;d \leq -7.8654638730470845 \cdot 10^{+130}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array}\]
Alternative 8
Error52.3
Cost64
\[0\]
Alternative 9
Error61.7
Cost64
\[1\]

Error

Derivation

  1. Split input into 4 regimes
  2. if c < -2.7839182926984815e79

    1. Initial program 39.7

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

      \[\leadsto \color{blue}{\frac{a}{c}}\]
    3. Simplified17.5

      \[\leadsto \color{blue}{\frac{a}{c}}\]

    if -2.7839182926984815e79 < c < -8.84980801529270659e-110 or 2.1472644582174967e-123 < c < 1.3153164510446189e86

    1. Initial program 15.0

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

      \[\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_377314.9

      \[\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. Simplified14.9

      \[\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 -8.84980801529270659e-110 < c < 2.1472644582174967e-123

    1. Initial program 21.1

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

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

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

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

    if 1.3153164510446189e86 < c

    1. Initial program 38.5

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

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

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{b \cdot d}{c \cdot c}}\]
    4. Simplified15.6

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -2.7839182926984815 \cdot 10^{+79}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -8.849808015292707 \cdot 10^{-110}:\\ \;\;\;\;\frac{\frac{c \cdot a + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq 2.1472644582174967 \cdot 10^{-123}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\ \mathbf{elif}\;c \leq 1.315316451044619 \cdot 10^{+86}:\\ \;\;\;\;\frac{\frac{c \cdot a + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot d}{c \cdot c}\\ \end{array}\]

Reproduce

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