Average Error: 26.1 → 23.9
Time: 3.1s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -1.9497673481821607 \cdot 10^{115} \lor \neg \left(d \le 6.67079582011057737 \cdot 10^{138}\right):\\ \;\;\;\;\frac{-1 \cdot {b}^{2}}{a \cdot c - b \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{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}\;d \le -1.9497673481821607 \cdot 10^{115} \lor \neg \left(d \le 6.67079582011057737 \cdot 10^{138}\right):\\
\;\;\;\;\frac{-1 \cdot {b}^{2}}{a \cdot c - b \cdot d}\\

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

\end{array}
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 VAR;
	if (((d <= -1.9497673481821607e+115) || !(d <= 6.670795820110577e+138))) {
		VAR = ((-1.0 * pow(b, 2.0)) / ((a * c) - (b * d)));
	} else {
		VAR = (((a * c) + (b * d)) / ((c * c) + (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

Original26.1
Target0.4
Herbie23.9
\[\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 2 regimes
  2. if d < -1.9497673481821607e+115 or 6.670795820110577e+138 < d

    1. Initial program 41.7

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied clear-num41.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 flip-+48.6

      \[\leadsto \frac{1}{\frac{c \cdot c + d \cdot d}{\color{blue}{\frac{\left(a \cdot c\right) \cdot \left(a \cdot c\right) - \left(b \cdot d\right) \cdot \left(b \cdot d\right)}{a \cdot c - b \cdot d}}}}\]
    6. Applied associate-/r/48.7

      \[\leadsto \frac{1}{\color{blue}{\frac{c \cdot c + d \cdot d}{\left(a \cdot c\right) \cdot \left(a \cdot c\right) - \left(b \cdot d\right) \cdot \left(b \cdot d\right)} \cdot \left(a \cdot c - b \cdot d\right)}}\]
    7. Applied associate-/r*48.7

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

      \[\leadsto \frac{\color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right) \cdot \left(a \cdot c - b \cdot d\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}}}{a \cdot c - b \cdot d}\]
    9. Taylor expanded around 0 34.7

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

    if -1.9497673481821607e+115 < d < 6.670795820110577e+138

    1. Initial program 19.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification23.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le -1.9497673481821607 \cdot 10^{115} \lor \neg \left(d \le 6.67079582011057737 \cdot 10^{138}\right):\\ \;\;\;\;\frac{-1 \cdot {b}^{2}}{a \cdot c - b \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \end{array}\]

Reproduce

herbie shell --seed 2020071 +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))))