Average Error: 26.6 → 13.3
Time: 47.4s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.999293010764536 \cdot 10^{152}:\\ \;\;\;\;\frac{-1 \cdot a}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\\ \mathbf{elif}\;c \le 1.23638593134364678 \cdot 10^{107}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -1.999293010764536 \cdot 10^{152}:\\
\;\;\;\;\frac{-1 \cdot a}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\\

\mathbf{elif}\;c \le 1.23638593134364678 \cdot 10^{107}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\\

\mathbf{else}:\\
\;\;\;\;\frac{a}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\\

\end{array}
double code(double a, double b, double c, double d) {
	return ((double) (((double) (((double) (a * c)) + ((double) (b * d)))) / ((double) (((double) (c * c)) + ((double) (d * d))))));
}
double code(double a, double b, double c, double d) {
	double VAR;
	if ((c <= -1.999293010764536e+152)) {
		VAR = ((double) (((double) (-1.0 * a)) / ((double) (((double) (((double) hypot(c, d)) / 1.0)) / ((double) sqrt(1.0))))));
	} else {
		double VAR_1;
		if ((c <= 1.2363859313436468e+107)) {
			VAR_1 = ((double) (((double) (((double) fma(a, c, ((double) (b * d)))) / ((double) hypot(c, d)))) / ((double) (((double) (((double) hypot(c, d)) / 1.0)) / ((double) sqrt(1.0))))));
		} else {
			VAR_1 = ((double) (a / ((double) (((double) (((double) hypot(c, d)) / 1.0)) / ((double) sqrt(1.0))))));
		}
		VAR = VAR_1;
	}
	return VAR;
}

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.6
Target0.5
Herbie13.3
\[\begin{array}{l} \mathbf{if}\;\left|d\right| \lt \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 3 regimes
  2. if c < -1.999293010764536e+152

    1. Initial program 45.7

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

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

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

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

      \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{1}}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified29.0

      \[\leadsto \frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{1}} \cdot \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}}\]
    8. Using strategy rm
    9. Applied add-sqr-sqrt29.0

      \[\leadsto \frac{\color{blue}{\sqrt{1} \cdot \sqrt{1}}}{\frac{\mathsf{hypot}\left(c, d\right)}{1}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    10. Applied associate-/l*29.0

      \[\leadsto \color{blue}{\frac{\sqrt{1}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    11. Applied associate-*l/28.9

      \[\leadsto \color{blue}{\frac{\sqrt{1} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}}\]
    12. Simplified28.9

      \[\leadsto \frac{\color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\]
    13. Taylor expanded around -inf 13.5

      \[\leadsto \frac{\color{blue}{-1 \cdot a}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\]

    if -1.999293010764536e+152 < c < 1.2363859313436468e+107

    1. Initial program 19.5

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

      \[\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-frac19.5

      \[\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. Simplified19.5

      \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{1}}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified12.8

      \[\leadsto \frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{1}} \cdot \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}}\]
    8. Using strategy rm
    9. Applied add-sqr-sqrt12.8

      \[\leadsto \frac{\color{blue}{\sqrt{1} \cdot \sqrt{1}}}{\frac{\mathsf{hypot}\left(c, d\right)}{1}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    10. Applied associate-/l*12.8

      \[\leadsto \color{blue}{\frac{\sqrt{1}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    11. Applied associate-*l/12.6

      \[\leadsto \color{blue}{\frac{\sqrt{1} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}}\]
    12. Simplified12.6

      \[\leadsto \frac{\color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\]

    if 1.2363859313436468e+107 < c

    1. Initial program 40.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt40.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-identity40.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-frac40.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. Simplified40.0

      \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{1}}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified26.9

      \[\leadsto \frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{1}} \cdot \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}}\]
    8. Using strategy rm
    9. Applied add-sqr-sqrt26.9

      \[\leadsto \frac{\color{blue}{\sqrt{1} \cdot \sqrt{1}}}{\frac{\mathsf{hypot}\left(c, d\right)}{1}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    10. Applied associate-/l*26.9

      \[\leadsto \color{blue}{\frac{\sqrt{1}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    11. Applied associate-*l/26.8

      \[\leadsto \color{blue}{\frac{\sqrt{1} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}}\]
    12. Simplified26.8

      \[\leadsto \frac{\color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\]
    13. Taylor expanded around inf 15.9

      \[\leadsto \frac{\color{blue}{a}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification13.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.999293010764536 \cdot 10^{152}:\\ \;\;\;\;\frac{-1 \cdot a}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\\ \mathbf{elif}\;c \le 1.23638593134364678 \cdot 10^{107}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{1}}{\sqrt{1}}}\\ \end{array}\]

Reproduce

herbie shell --seed 2020114 +o rules:numerics
(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))))