Average Error: 27.0 → 26.4
Time: 3.5s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq 6.971051725145218 \cdot 10^{+287}:\\ \;\;\;\;\frac{\frac{1}{\sqrt{c \cdot c + d \cdot d}}}{\frac{\sqrt{c \cdot c + d \cdot d}}{a \cdot c + b \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{\sqrt{c \cdot c + d \cdot d}}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq 6.971051725145218 \cdot 10^{+287}:\\
\;\;\;\;\frac{\frac{1}{\sqrt{c \cdot c + d \cdot d}}}{\frac{\sqrt{c \cdot c + d \cdot d}}{a \cdot c + b \cdot d}}\\

\mathbf{else}:\\
\;\;\;\;\frac{-a}{\sqrt{c \cdot c + d \cdot d}}\\

\end{array}
double code(double a, double b, double c, double d) {
	return (((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 (((((double) (((double) (a * c)) + ((double) (b * d)))) / ((double) (((double) (c * c)) + ((double) (d * d))))) <= 6.971051725145218e+287)) {
		VAR = ((1.0 / ((double) sqrt(((double) (((double) (c * c)) + ((double) (d * d))))))) / (((double) sqrt(((double) (((double) (c * c)) + ((double) (d * d)))))) / ((double) (((double) (a * c)) + ((double) (b * d))))));
	} else {
		VAR = (((double) -(a)) / ((double) sqrt(((double) (((double) (c * c)) + ((double) (d * d)))))));
	}
	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

Original27.0
Target0.4
Herbie26.4
\[\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 2 regimes
  2. if (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) < 6.9710517251452183e287

    1. Initial program 14.6

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

      \[\leadsto \color{blue}{\frac{1}{\frac{c \cdot c + d \cdot d}{a \cdot c + b \cdot d}}}\]
    4. Using strategy rm
    5. Applied *-un-lft-identity14.8

      \[\leadsto \frac{1}{\frac{c \cdot c + d \cdot d}{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}}\]
    6. Applied add-sqr-sqrt14.8

      \[\leadsto \frac{1}{\frac{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}{1 \cdot \left(a \cdot c + b \cdot d\right)}}\]
    7. Applied times-frac14.8

      \[\leadsto \frac{1}{\color{blue}{\frac{\sqrt{c \cdot c + d \cdot d}}{1} \cdot \frac{\sqrt{c \cdot c + d \cdot d}}{a \cdot c + b \cdot d}}}\]
    8. Applied associate-/r*14.6

      \[\leadsto \color{blue}{\frac{\frac{1}{\frac{\sqrt{c \cdot c + d \cdot d}}{1}}}{\frac{\sqrt{c \cdot c + d \cdot d}}{a \cdot c + b \cdot d}}}\]
    9. Simplified14.6

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

    if 6.9710517251452183e287 < (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))

    1. Initial program 63.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt63.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*63.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}}}\]
    5. Taylor expanded around -inf 60.3

      \[\leadsto \frac{\color{blue}{-1 \cdot a}}{\sqrt{c \cdot c + d \cdot d}}\]
    6. Simplified60.3

      \[\leadsto \frac{\color{blue}{-a}}{\sqrt{c \cdot c + d \cdot d}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification26.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq 6.971051725145218 \cdot 10^{+287}:\\ \;\;\;\;\frac{\frac{1}{\sqrt{c \cdot c + d \cdot d}}}{\frac{\sqrt{c \cdot c + d \cdot d}}{a \cdot c + b \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{\sqrt{c \cdot c + d \cdot d}}\\ \end{array}\]

Reproduce

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