Average Error: 26.9 → 20.3
Time: 7.4s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \leq -1.3977594964147 \cdot 10^{+95}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 1.3087003290237327 \cdot 10^{-72}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 7.644444375681995 \cdot 10^{+23}:\\ \;\;\;\;\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 1.9371910220821266 \cdot 10^{+92}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 2.665235252998217 \cdot 10^{+132}:\\ \;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{c \cdot a + b \cdot d}{\sqrt{\sqrt{c \cdot c + d \cdot d}} \cdot \sqrt{\sqrt{c \cdot c + d \cdot d}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \leq -1.3977594964147 \cdot 10^{+95}:\\
\;\;\;\;\frac{a}{c}\\

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

\mathbf{elif}\;c \leq 7.644444375681995 \cdot 10^{+23}:\\
\;\;\;\;\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 1.9371910220821266 \cdot 10^{+92}:\\
\;\;\;\;\frac{b}{d}\\

\mathbf{elif}\;c \leq 2.665235252998217 \cdot 10^{+132}:\\
\;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{c \cdot a + b \cdot d}{\sqrt{\sqrt{c \cdot c + d \cdot d}} \cdot \sqrt{\sqrt{c \cdot c + d \cdot d}}}\\

\mathbf{else}:\\
\;\;\;\;\frac{a}{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 -1.3977594964147e+95)
   (/ a c)
   (if (<= c 1.3087003290237327e-72)
     (+ (/ b d) (/ (* c a) (pow d 2.0)))
     (if (<= c 7.644444375681995e+23)
       (/
        (/ (+ (* c a) (* b d)) (sqrt (+ (* c c) (* d d))))
        (sqrt (+ (* c c) (* d d))))
       (if (<= c 1.9371910220821266e+92)
         (/ b d)
         (if (<= c 2.665235252998217e+132)
           (*
            (/ 1.0 (sqrt (+ (* c c) (* d d))))
            (/
             (+ (* c a) (* b d))
             (*
              (sqrt (sqrt (+ (* c c) (* d d))))
              (sqrt (sqrt (+ (* c c) (* d d)))))))
           (/ a 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 <= -1.3977594964147e+95) {
		tmp = a / c;
	} else if (c <= 1.3087003290237327e-72) {
		tmp = (b / d) + ((c * a) / pow(d, 2.0));
	} else if (c <= 7.644444375681995e+23) {
		tmp = (((c * a) + (b * d)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else if (c <= 1.9371910220821266e+92) {
		tmp = b / d;
	} else if (c <= 2.665235252998217e+132) {
		tmp = (1.0 / sqrt((c * c) + (d * d))) * (((c * a) + (b * d)) / (sqrt(sqrt((c * c) + (d * d))) * sqrt(sqrt((c * c) + (d * d)))));
	} else {
		tmp = a / 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.9
Target0.5
Herbie20.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 5 regimes
  2. if c < -1.3977594964146999e95 or 2.6652352529982171e132 < c

    1. Initial program 41.9

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

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

    if -1.3977594964146999e95 < c < 1.30870032902373269e-72

    1. Initial program 19.8

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

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

    if 1.30870032902373269e-72 < c < 7.6444443756819951e23

    1. Initial program 14.8

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

      \[\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_309114.7

      \[\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 7.6444443756819951e23 < c < 1.9371910220821266e92

    1. Initial program 20.6

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

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

    if 1.9371910220821266e92 < c < 2.6652352529982171e132

    1. Initial program 24.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt_binary64_316924.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 *-un-lft-identity_binary64_314724.0

      \[\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_315324.0

      \[\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}}}\]
    6. Simplified24.0

      \[\leadsto \frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \color{blue}{\frac{d \cdot b + c \cdot a}{\sqrt{c \cdot c + d \cdot d}}}\]
    7. Using strategy rm
    8. Applied add-sqr-sqrt_binary64_316924.2

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -1.3977594964147 \cdot 10^{+95}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 1.3087003290237327 \cdot 10^{-72}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 7.644444375681995 \cdot 10^{+23}:\\ \;\;\;\;\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 1.9371910220821266 \cdot 10^{+92}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 2.665235252998217 \cdot 10^{+132}:\\ \;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{c \cdot a + b \cdot d}{\sqrt{\sqrt{c \cdot c + d \cdot d}} \cdot \sqrt{\sqrt{c \cdot c + d \cdot d}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array}\]

Reproduce

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