Average Error: 26.1 → 26.1
Time: 26.9s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\frac{\frac{b \cdot d + a \cdot c}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\]
double f(double a, double b, double c, double d) {
        double r18018318 = a;
        double r18018319 = c;
        double r18018320 = r18018318 * r18018319;
        double r18018321 = b;
        double r18018322 = d;
        double r18018323 = r18018321 * r18018322;
        double r18018324 = r18018320 + r18018323;
        double r18018325 = r18018319 * r18018319;
        double r18018326 = r18018322 * r18018322;
        double r18018327 = r18018325 + r18018326;
        double r18018328 = r18018324 / r18018327;
        return r18018328;
}

double f(double a, double b, double c, double d) {
        double r18018329 = b;
        double r18018330 = d;
        double r18018331 = r18018329 * r18018330;
        double r18018332 = a;
        double r18018333 = c;
        double r18018334 = r18018332 * r18018333;
        double r18018335 = r18018331 + r18018334;
        double r18018336 = r18018333 * r18018333;
        double r18018337 = r18018330 * r18018330;
        double r18018338 = r18018336 + r18018337;
        double r18018339 = sqrt(r18018338);
        double r18018340 = r18018335 / r18018339;
        double r18018341 = r18018340 / r18018339;
        return r18018341;
}

\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\frac{\frac{b \cdot d + a \cdot c}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.1
Target0.4
Herbie26.1
\[\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. Initial program 26.1

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

    \[\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. Final simplification26.1

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

Reproduce

herbie shell --seed 2019102 
(FPCore (a b c d)
  :name "Complex division, real part"

  :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))))