Average Error: 25.8 → 12.2
Time: 8.9s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -7.553879560626287 \cdot 10^{+93}:\\ \;\;\;\;\frac{b}{d} + \frac{1}{d} \cdot \frac{a}{\frac{d}{c}}\\ \mathbf{elif}\;d \leq -7.875830746956647 \cdot 10^{-107}:\\ \;\;\;\;\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 2.5365321914959593 \cdot 10^{-134}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 7.162264487896466 \cdot 10^{+81}:\\ \;\;\;\;\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} + \frac{\frac{a}{d}}{\frac{d}{c}}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \leq -7.553879560626287 \cdot 10^{+93}:\\
\;\;\;\;\frac{b}{d} + \frac{1}{d} \cdot \frac{a}{\frac{d}{c}}\\

\mathbf{elif}\;d \leq -7.875830746956647 \cdot 10^{-107}:\\
\;\;\;\;\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 2.5365321914959593 \cdot 10^{-134}:\\
\;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\

\mathbf{elif}\;d \leq 7.162264487896466 \cdot 10^{+81}:\\
\;\;\;\;\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} + \frac{\frac{a}{d}}{\frac{d}{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 (<= d -7.553879560626287e+93)
   (+ (/ b d) (* (/ 1.0 d) (/ a (/ d c))))
   (if (<= d -7.875830746956647e-107)
     (/
      (/ (+ (* a c) (* d b)) (sqrt (+ (* c c) (* d d))))
      (sqrt (+ (* c c) (* d d))))
     (if (<= d 2.5365321914959593e-134)
       (+ (/ a c) (/ (* d b) (pow c 2.0)))
       (if (<= d 7.162264487896466e+81)
         (/
          (/ (+ (* a c) (* d b)) (sqrt (+ (* c c) (* d d))))
          (sqrt (+ (* c c) (* d d))))
         (+ (/ b d) (/ (/ a d) (/ d 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 (d <= -7.553879560626287e+93) {
		tmp = (b / d) + ((1.0 / d) * (a / (d / c)));
	} else if (d <= -7.875830746956647e-107) {
		tmp = (((a * c) + (d * b)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else if (d <= 2.5365321914959593e-134) {
		tmp = (a / c) + ((d * b) / pow(c, 2.0));
	} else if (d <= 7.162264487896466e+81) {
		tmp = (((a * c) + (d * b)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else {
		tmp = (b / d) + ((a / d) / (d / 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

Original25.8
Target0.5
Herbie12.2
\[\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 d < -7.5538795606262866e93

    1. Initial program 40.4

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

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

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

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

      \[\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. Taylor expanded around 0 16.4

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

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{a}{\frac{d \cdot d}{c}}}\]
    9. Using strategy rm
    10. Applied *-un-lft-identity_binary64_110115.7

      \[\leadsto \frac{b}{d} + \frac{a}{\frac{d \cdot d}{\color{blue}{1 \cdot c}}}\]
    11. Applied times-frac_binary64_110713.6

      \[\leadsto \frac{b}{d} + \frac{a}{\color{blue}{\frac{d}{1} \cdot \frac{d}{c}}}\]
    12. Applied *-un-lft-identity_binary64_110113.6

      \[\leadsto \frac{b}{d} + \frac{\color{blue}{1 \cdot a}}{\frac{d}{1} \cdot \frac{d}{c}}\]
    13. Applied times-frac_binary64_110710.3

      \[\leadsto \frac{b}{d} + \color{blue}{\frac{1}{\frac{d}{1}} \cdot \frac{a}{\frac{d}{c}}}\]
    14. Simplified10.3

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

    if -7.5538795606262866e93 < d < -7.87583074695664708e-107 or 2.5365321914959593e-134 < d < 7.16226448789646589e81

    1. Initial program 15.1

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

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

      \[\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.87583074695664708e-107 < d < 2.5365321914959593e-134

    1. Initial program 21.9

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

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

    if 7.16226448789646589e81 < d

    1. Initial program 37.4

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

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

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

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

      \[\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. Taylor expanded around 0 16.7

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

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{a}{\frac{d \cdot d}{c}}}\]
    9. Using strategy rm
    10. Applied *-un-lft-identity_binary64_110116.1

      \[\leadsto \frac{b}{d} + \frac{a}{\frac{d \cdot d}{\color{blue}{1 \cdot c}}}\]
    11. Applied times-frac_binary64_110714.5

      \[\leadsto \frac{b}{d} + \frac{a}{\color{blue}{\frac{d}{1} \cdot \frac{d}{c}}}\]
    12. Applied associate-/r*_binary64_104511.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -7.553879560626287 \cdot 10^{+93}:\\ \;\;\;\;\frac{b}{d} + \frac{1}{d} \cdot \frac{a}{\frac{d}{c}}\\ \mathbf{elif}\;d \leq -7.875830746956647 \cdot 10^{-107}:\\ \;\;\;\;\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 2.5365321914959593 \cdot 10^{-134}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 7.162264487896466 \cdot 10^{+81}:\\ \;\;\;\;\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} + \frac{\frac{a}{d}}{\frac{d}{c}}\\ \end{array}\]

Reproduce

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