Average Error: 26.7 → 12.9
Time: 10.0s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \leq -4.32818473896838 \cdot 10^{+105}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;c \leq -9.478533032926856 \cdot 10^{-118}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq 1.4206205443431834 \cdot 10^{-192}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 1.0829748826702354 \cdot 10^{-189}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d \cdot b}{c}}{c}\\ \mathbf{elif}\;c \leq 4.186640121113003 \cdot 10^{-153}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 1.0217963801675215 \cdot 10^{+22}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \leq -4.32818473896838 \cdot 10^{+105}:\\
\;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\

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

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

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

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

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

\mathbf{else}:\\
\;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{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 -4.32818473896838e+105)
   (+ (/ a c) (* (/ d c) (/ b c)))
   (if (<= c -9.478533032926856e-118)
     (/
      (/ (+ (* c a) (* d b)) (sqrt (+ (* c c) (* d d))))
      (sqrt (+ (* c c) (* d d))))
     (if (<= c 1.4206205443431834e-192)
       (+ (/ b d) (/ (* c a) (pow d 2.0)))
       (if (<= c 1.0829748826702354e-189)
         (+ (/ a c) (/ (/ (* d b) c) c))
         (if (<= c 4.186640121113003e-153)
           (+ (/ b d) (/ (* c a) (pow d 2.0)))
           (if (<= c 1.0217963801675215e+22)
             (/
              (/ (+ (* c a) (* d b)) (sqrt (+ (* c c) (* d d))))
              (sqrt (+ (* c c) (* d d))))
             (+ (/ a c) (* (/ d c) (/ b 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 <= -4.32818473896838e+105) {
		tmp = (a / c) + ((d / c) * (b / c));
	} else if (c <= -9.478533032926856e-118) {
		tmp = (((c * a) + (d * b)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else if (c <= 1.4206205443431834e-192) {
		tmp = (b / d) + ((c * a) / pow(d, 2.0));
	} else if (c <= 1.0829748826702354e-189) {
		tmp = (a / c) + (((d * b) / c) / c);
	} else if (c <= 4.186640121113003e-153) {
		tmp = (b / d) + ((c * a) / pow(d, 2.0));
	} else if (c <= 1.0217963801675215e+22) {
		tmp = (((c * a) + (d * b)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else {
		tmp = (a / c) + ((d / c) * (b / 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.7
Target0.5
Herbie12.9
\[\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 4 regimes
  2. if c < -4.3281847389683799e105 or 1.02179638016752149e22 < c

    1. Initial program 37.2

      \[\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} + \frac{d \cdot b}{{c}^{2}}}\]
    3. Simplified17.5

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{d \cdot b}{c \cdot c}}\]
    4. Using strategy rm
    5. Applied times-frac_binary64_417612.5

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

    if -4.3281847389683799e105 < c < -9.4785330329268561e-118 or 4.1866401211130029e-153 < c < 1.02179638016752149e22

    1. Initial program 15.5

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

      \[\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_411415.4

      \[\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 -9.4785330329268561e-118 < c < 1.4206205443431834e-192 or 1.08297488267023536e-189 < c < 4.1866401211130029e-153

    1. Initial program 24.1

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

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

    if 1.4206205443431834e-192 < c < 1.08297488267023536e-189

    1. Initial program 30.3

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

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

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{d \cdot b}{c \cdot c}}\]
    4. Using strategy rm
    5. Applied associate-/r*_binary64_411451.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -4.32818473896838 \cdot 10^{+105}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;c \leq -9.478533032926856 \cdot 10^{-118}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq 1.4206205443431834 \cdot 10^{-192}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 1.0829748826702354 \cdot 10^{-189}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d \cdot b}{c}}{c}\\ \mathbf{elif}\;c \leq 4.186640121113003 \cdot 10^{-153}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 1.0217963801675215 \cdot 10^{+22}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \end{array}\]

Reproduce

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