Average Error: 27.0 → 25.6
Time: 6.7s
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} = -inf.0:\\ \;\;\;\;\frac{a}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \le 3.7505355971518835 \cdot 10^{302}:\\ \;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\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} = -inf.0:\\
\;\;\;\;\frac{a}{\sqrt{c \cdot c + d \cdot d}}\\

\mathbf{elif}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \le 3.7505355971518835 \cdot 10^{302}:\\
\;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\\

\mathbf{else}:\\
\;\;\;\;\frac{b}{\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))))) <= -inf.0)) {
		VAR = (a / ((double) sqrt(((double) (((double) (c * c)) + ((double) (d * d)))))));
	} else {
		double VAR_1;
		if (((((double) (((double) (a * c)) + ((double) (b * d)))) / ((double) (((double) (c * c)) + ((double) (d * d))))) <= 3.7505355971518835e+302)) {
			VAR_1 = ((double) ((1.0 / ((double) sqrt(((double) (((double) (c * c)) + ((double) (d * d))))))) * (((double) (((double) (a * c)) + ((double) (b * d)))) / ((double) sqrt(((double) (((double) (c * c)) + ((double) (d * d)))))))));
		} else {
			VAR_1 = (b / ((double) sqrt(((double) (((double) (c * c)) + ((double) (d * d)))))));
		}
		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

Original27.0
Target0.5
Herbie25.6
\[\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 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) < -inf.0

    1. Initial program 64.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt64.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*64.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 51.9

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

    if -inf.0 < (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) < 3.7505355971518835e302

    1. Initial program 12.0

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt12.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-identity12.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-frac12.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}}}\]

    if 3.7505355971518835e302 < (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))

    1. Initial program 63.8

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

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

      \[\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 0 60.2

      \[\leadsto \frac{\color{blue}{b}}{\sqrt{c \cdot c + d \cdot d}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification25.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} = -inf.0:\\ \;\;\;\;\frac{a}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \le 3.7505355971518835 \cdot 10^{302}:\\ \;\;\;\;\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\sqrt{c \cdot c + d \cdot d}}\\ \end{array}\]

Reproduce

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